cros_ec_dev.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * cros_ec_dev - expose the Chrome OS Embedded Controller to user-space
  3. *
  4. * Copyright (C) 2014 Google, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/fs.h>
  20. #include <linux/mfd/core.h>
  21. #include <linux/module.h>
  22. #include <linux/mod_devicetable.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm.h>
  25. #include <linux/slab.h>
  26. #include <linux/uaccess.h>
  27. #include "cros_ec_dev.h"
  28. #define DRV_NAME "cros-ec-dev"
  29. /* Device variables */
  30. #define CROS_MAX_DEV 128
  31. static int ec_major;
  32. static const struct attribute_group *cros_ec_groups[] = {
  33. &cros_ec_attr_group,
  34. &cros_ec_lightbar_attr_group,
  35. &cros_ec_vbc_attr_group,
  36. NULL,
  37. };
  38. static struct class cros_class = {
  39. .owner = THIS_MODULE,
  40. .name = "chromeos",
  41. .dev_groups = cros_ec_groups,
  42. };
  43. /* Basic communication */
  44. static int ec_get_version(struct cros_ec_dev *ec, char *str, int maxlen)
  45. {
  46. struct ec_response_get_version *resp;
  47. static const char * const current_image_name[] = {
  48. "unknown", "read-only", "read-write", "invalid",
  49. };
  50. struct cros_ec_command *msg;
  51. int ret;
  52. msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
  53. if (!msg)
  54. return -ENOMEM;
  55. msg->version = 0;
  56. msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
  57. msg->insize = sizeof(*resp);
  58. msg->outsize = 0;
  59. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  60. if (ret < 0)
  61. goto exit;
  62. if (msg->result != EC_RES_SUCCESS) {
  63. snprintf(str, maxlen,
  64. "%s\nUnknown EC version: EC returned %d\n",
  65. CROS_EC_DEV_VERSION, msg->result);
  66. ret = -EINVAL;
  67. goto exit;
  68. }
  69. resp = (struct ec_response_get_version *)msg->data;
  70. if (resp->current_image >= ARRAY_SIZE(current_image_name))
  71. resp->current_image = 3; /* invalid */
  72. snprintf(str, maxlen, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION,
  73. resp->version_string_ro, resp->version_string_rw,
  74. current_image_name[resp->current_image]);
  75. ret = 0;
  76. exit:
  77. kfree(msg);
  78. return ret;
  79. }
  80. static int cros_ec_check_features(struct cros_ec_dev *ec, int feature)
  81. {
  82. struct cros_ec_command *msg;
  83. int ret;
  84. if (ec->features[0] == -1U && ec->features[1] == -1U) {
  85. /* features bitmap not read yet */
  86. msg = kmalloc(sizeof(*msg) + sizeof(ec->features), GFP_KERNEL);
  87. if (!msg)
  88. return -ENOMEM;
  89. msg->version = 0;
  90. msg->command = EC_CMD_GET_FEATURES + ec->cmd_offset;
  91. msg->insize = sizeof(ec->features);
  92. msg->outsize = 0;
  93. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  94. if (ret < 0 || msg->result != EC_RES_SUCCESS) {
  95. dev_warn(ec->dev, "cannot get EC features: %d/%d\n",
  96. ret, msg->result);
  97. memset(ec->features, 0, sizeof(ec->features));
  98. } else {
  99. memcpy(ec->features, msg->data, sizeof(ec->features));
  100. }
  101. dev_dbg(ec->dev, "EC features %08x %08x\n",
  102. ec->features[0], ec->features[1]);
  103. kfree(msg);
  104. }
  105. return ec->features[feature / 32] & EC_FEATURE_MASK_0(feature);
  106. }
  107. /* Device file ops */
  108. static int ec_device_open(struct inode *inode, struct file *filp)
  109. {
  110. struct cros_ec_dev *ec = container_of(inode->i_cdev,
  111. struct cros_ec_dev, cdev);
  112. filp->private_data = ec;
  113. nonseekable_open(inode, filp);
  114. return 0;
  115. }
  116. static int ec_device_release(struct inode *inode, struct file *filp)
  117. {
  118. return 0;
  119. }
  120. static ssize_t ec_device_read(struct file *filp, char __user *buffer,
  121. size_t length, loff_t *offset)
  122. {
  123. struct cros_ec_dev *ec = filp->private_data;
  124. char msg[sizeof(struct ec_response_get_version) +
  125. sizeof(CROS_EC_DEV_VERSION)];
  126. size_t count;
  127. int ret;
  128. if (*offset != 0)
  129. return 0;
  130. ret = ec_get_version(ec, msg, sizeof(msg));
  131. if (ret)
  132. return ret;
  133. count = min(length, strlen(msg));
  134. if (copy_to_user(buffer, msg, count))
  135. return -EFAULT;
  136. *offset = count;
  137. return count;
  138. }
  139. /* Ioctls */
  140. static long ec_device_ioctl_xcmd(struct cros_ec_dev *ec, void __user *arg)
  141. {
  142. long ret;
  143. struct cros_ec_command u_cmd;
  144. struct cros_ec_command *s_cmd;
  145. if (copy_from_user(&u_cmd, arg, sizeof(u_cmd)))
  146. return -EFAULT;
  147. if ((u_cmd.outsize > EC_MAX_MSG_BYTES) ||
  148. (u_cmd.insize > EC_MAX_MSG_BYTES))
  149. return -EINVAL;
  150. s_cmd = kmalloc(sizeof(*s_cmd) + max(u_cmd.outsize, u_cmd.insize),
  151. GFP_KERNEL);
  152. if (!s_cmd)
  153. return -ENOMEM;
  154. if (copy_from_user(s_cmd, arg, sizeof(*s_cmd) + u_cmd.outsize)) {
  155. ret = -EFAULT;
  156. goto exit;
  157. }
  158. if (u_cmd.outsize != s_cmd->outsize ||
  159. u_cmd.insize != s_cmd->insize) {
  160. ret = -EINVAL;
  161. goto exit;
  162. }
  163. s_cmd->command += ec->cmd_offset;
  164. ret = cros_ec_cmd_xfer(ec->ec_dev, s_cmd);
  165. /* Only copy data to userland if data was received. */
  166. if (ret < 0)
  167. goto exit;
  168. if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + s_cmd->insize))
  169. ret = -EFAULT;
  170. exit:
  171. kfree(s_cmd);
  172. return ret;
  173. }
  174. static long ec_device_ioctl_readmem(struct cros_ec_dev *ec, void __user *arg)
  175. {
  176. struct cros_ec_device *ec_dev = ec->ec_dev;
  177. struct cros_ec_readmem s_mem = { };
  178. long num;
  179. /* Not every platform supports direct reads */
  180. if (!ec_dev->cmd_readmem)
  181. return -ENOTTY;
  182. if (copy_from_user(&s_mem, arg, sizeof(s_mem)))
  183. return -EFAULT;
  184. num = ec_dev->cmd_readmem(ec_dev, s_mem.offset, s_mem.bytes,
  185. s_mem.buffer);
  186. if (num <= 0)
  187. return num;
  188. if (copy_to_user((void __user *)arg, &s_mem, sizeof(s_mem)))
  189. return -EFAULT;
  190. return 0;
  191. }
  192. static long ec_device_ioctl(struct file *filp, unsigned int cmd,
  193. unsigned long arg)
  194. {
  195. struct cros_ec_dev *ec = filp->private_data;
  196. if (_IOC_TYPE(cmd) != CROS_EC_DEV_IOC)
  197. return -ENOTTY;
  198. switch (cmd) {
  199. case CROS_EC_DEV_IOCXCMD:
  200. return ec_device_ioctl_xcmd(ec, (void __user *)arg);
  201. case CROS_EC_DEV_IOCRDMEM:
  202. return ec_device_ioctl_readmem(ec, (void __user *)arg);
  203. }
  204. return -ENOTTY;
  205. }
  206. /* Module initialization */
  207. static const struct file_operations fops = {
  208. .open = ec_device_open,
  209. .release = ec_device_release,
  210. .read = ec_device_read,
  211. .unlocked_ioctl = ec_device_ioctl,
  212. #ifdef CONFIG_COMPAT
  213. .compat_ioctl = ec_device_ioctl,
  214. #endif
  215. };
  216. static void cros_ec_sensors_register(struct cros_ec_dev *ec)
  217. {
  218. /*
  219. * Issue a command to get the number of sensor reported.
  220. * Build an array of sensors driver and register them all.
  221. */
  222. int ret, i, id, sensor_num;
  223. struct mfd_cell *sensor_cells;
  224. struct cros_ec_sensor_platform *sensor_platforms;
  225. int sensor_type[MOTIONSENSE_TYPE_MAX];
  226. struct ec_params_motion_sense *params;
  227. struct ec_response_motion_sense *resp;
  228. struct cros_ec_command *msg;
  229. msg = kzalloc(sizeof(struct cros_ec_command) +
  230. max(sizeof(*params), sizeof(*resp)), GFP_KERNEL);
  231. if (msg == NULL)
  232. return;
  233. msg->version = 2;
  234. msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
  235. msg->outsize = sizeof(*params);
  236. msg->insize = sizeof(*resp);
  237. params = (struct ec_params_motion_sense *)msg->data;
  238. params->cmd = MOTIONSENSE_CMD_DUMP;
  239. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  240. if (ret < 0 || msg->result != EC_RES_SUCCESS) {
  241. dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n",
  242. ret, msg->result);
  243. goto error;
  244. }
  245. resp = (struct ec_response_motion_sense *)msg->data;
  246. sensor_num = resp->dump.sensor_count;
  247. /* Allocate 1 extra sensors in FIFO are needed */
  248. sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
  249. GFP_KERNEL);
  250. if (sensor_cells == NULL)
  251. goto error;
  252. sensor_platforms = kcalloc(sensor_num + 1,
  253. sizeof(struct cros_ec_sensor_platform),
  254. GFP_KERNEL);
  255. if (sensor_platforms == NULL)
  256. goto error_platforms;
  257. memset(sensor_type, 0, sizeof(sensor_type));
  258. id = 0;
  259. for (i = 0; i < sensor_num; i++) {
  260. params->cmd = MOTIONSENSE_CMD_INFO;
  261. params->info.sensor_num = i;
  262. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  263. if (ret < 0 || msg->result != EC_RES_SUCCESS) {
  264. dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n",
  265. i, ret, msg->result);
  266. continue;
  267. }
  268. switch (resp->info.type) {
  269. case MOTIONSENSE_TYPE_ACCEL:
  270. sensor_cells[id].name = "cros-ec-accel";
  271. break;
  272. case MOTIONSENSE_TYPE_BARO:
  273. sensor_cells[id].name = "cros-ec-baro";
  274. break;
  275. case MOTIONSENSE_TYPE_GYRO:
  276. sensor_cells[id].name = "cros-ec-gyro";
  277. break;
  278. case MOTIONSENSE_TYPE_MAG:
  279. sensor_cells[id].name = "cros-ec-mag";
  280. break;
  281. case MOTIONSENSE_TYPE_PROX:
  282. sensor_cells[id].name = "cros-ec-prox";
  283. break;
  284. case MOTIONSENSE_TYPE_LIGHT:
  285. sensor_cells[id].name = "cros-ec-light";
  286. break;
  287. case MOTIONSENSE_TYPE_ACTIVITY:
  288. sensor_cells[id].name = "cros-ec-activity";
  289. break;
  290. default:
  291. dev_warn(ec->dev, "unknown type %d\n", resp->info.type);
  292. continue;
  293. }
  294. sensor_platforms[id].sensor_num = i;
  295. sensor_cells[id].id = sensor_type[resp->info.type];
  296. sensor_cells[id].platform_data = &sensor_platforms[id];
  297. sensor_cells[id].pdata_size =
  298. sizeof(struct cros_ec_sensor_platform);
  299. sensor_type[resp->info.type]++;
  300. id++;
  301. }
  302. if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
  303. ec->has_kb_wake_angle = true;
  304. if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
  305. sensor_cells[id].name = "cros-ec-ring";
  306. id++;
  307. }
  308. ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
  309. NULL, 0, NULL);
  310. if (ret)
  311. dev_err(ec->dev, "failed to add EC sensors\n");
  312. kfree(sensor_platforms);
  313. error_platforms:
  314. kfree(sensor_cells);
  315. error:
  316. kfree(msg);
  317. }
  318. static const struct mfd_cell cros_ec_cec_cells[] = {
  319. { .name = "cros-ec-cec" }
  320. };
  321. static const struct mfd_cell cros_ec_rtc_cells[] = {
  322. { .name = "cros-ec-rtc" }
  323. };
  324. static const struct mfd_cell cros_usbpd_charger_cells[] = {
  325. { .name = "cros-usbpd-charger" }
  326. };
  327. static int ec_device_probe(struct platform_device *pdev)
  328. {
  329. int retval = -ENOMEM;
  330. struct device *dev = &pdev->dev;
  331. struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
  332. struct cros_ec_dev *ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL);
  333. if (!ec)
  334. return retval;
  335. dev_set_drvdata(dev, ec);
  336. ec->ec_dev = dev_get_drvdata(dev->parent);
  337. ec->dev = dev;
  338. ec->cmd_offset = ec_platform->cmd_offset;
  339. ec->features[0] = -1U; /* Not cached yet */
  340. ec->features[1] = -1U; /* Not cached yet */
  341. device_initialize(&ec->class_dev);
  342. cdev_init(&ec->cdev, &fops);
  343. /*
  344. * Add the class device
  345. * Link to the character device for creating the /dev entry
  346. * in devtmpfs.
  347. */
  348. ec->class_dev.devt = MKDEV(ec_major, pdev->id);
  349. ec->class_dev.class = &cros_class;
  350. ec->class_dev.parent = dev;
  351. retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
  352. if (retval) {
  353. dev_err(dev, "dev_set_name failed => %d\n", retval);
  354. goto failed;
  355. }
  356. /* check whether this EC is a sensor hub. */
  357. if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE))
  358. cros_ec_sensors_register(ec);
  359. /* Check whether this EC instance has CEC host command support */
  360. if (cros_ec_check_features(ec, EC_FEATURE_CEC)) {
  361. retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
  362. cros_ec_cec_cells,
  363. ARRAY_SIZE(cros_ec_cec_cells),
  364. NULL, 0, NULL);
  365. if (retval)
  366. dev_err(ec->dev,
  367. "failed to add cros-ec-cec device: %d\n",
  368. retval);
  369. }
  370. /* Check whether this EC instance has RTC host command support */
  371. if (cros_ec_check_features(ec, EC_FEATURE_RTC)) {
  372. retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
  373. cros_ec_rtc_cells,
  374. ARRAY_SIZE(cros_ec_rtc_cells),
  375. NULL, 0, NULL);
  376. if (retval)
  377. dev_err(ec->dev,
  378. "failed to add cros-ec-rtc device: %d\n",
  379. retval);
  380. }
  381. /* Check whether this EC instance has the PD charge manager */
  382. if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) {
  383. retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
  384. cros_usbpd_charger_cells,
  385. ARRAY_SIZE(cros_usbpd_charger_cells),
  386. NULL, 0, NULL);
  387. if (retval)
  388. dev_err(ec->dev,
  389. "failed to add cros-usbpd-charger device: %d\n",
  390. retval);
  391. }
  392. /* Take control of the lightbar from the EC. */
  393. lb_manual_suspend_ctrl(ec, 1);
  394. /* We can now add the sysfs class, we know which parameter to show */
  395. retval = cdev_device_add(&ec->cdev, &ec->class_dev);
  396. if (retval) {
  397. dev_err(dev, "cdev_device_add failed => %d\n", retval);
  398. goto failed;
  399. }
  400. if (cros_ec_debugfs_init(ec))
  401. dev_warn(dev, "failed to create debugfs directory\n");
  402. return 0;
  403. failed:
  404. put_device(&ec->class_dev);
  405. return retval;
  406. }
  407. static int ec_device_remove(struct platform_device *pdev)
  408. {
  409. struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
  410. /* Let the EC take over the lightbar again. */
  411. lb_manual_suspend_ctrl(ec, 0);
  412. cros_ec_debugfs_remove(ec);
  413. cdev_del(&ec->cdev);
  414. device_unregister(&ec->class_dev);
  415. return 0;
  416. }
  417. static void ec_device_shutdown(struct platform_device *pdev)
  418. {
  419. struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
  420. /* Be sure to clear up debugfs delayed works */
  421. cros_ec_debugfs_remove(ec);
  422. }
  423. static const struct platform_device_id cros_ec_id[] = {
  424. { DRV_NAME, 0 },
  425. { /* sentinel */ }
  426. };
  427. MODULE_DEVICE_TABLE(platform, cros_ec_id);
  428. static __maybe_unused int ec_device_suspend(struct device *dev)
  429. {
  430. struct cros_ec_dev *ec = dev_get_drvdata(dev);
  431. cros_ec_debugfs_suspend(ec);
  432. lb_suspend(ec);
  433. return 0;
  434. }
  435. static __maybe_unused int ec_device_resume(struct device *dev)
  436. {
  437. struct cros_ec_dev *ec = dev_get_drvdata(dev);
  438. cros_ec_debugfs_resume(ec);
  439. lb_resume(ec);
  440. return 0;
  441. }
  442. static const struct dev_pm_ops cros_ec_dev_pm_ops = {
  443. #ifdef CONFIG_PM_SLEEP
  444. .suspend = ec_device_suspend,
  445. .resume = ec_device_resume,
  446. #endif
  447. };
  448. static struct platform_driver cros_ec_dev_driver = {
  449. .driver = {
  450. .name = DRV_NAME,
  451. .pm = &cros_ec_dev_pm_ops,
  452. },
  453. .probe = ec_device_probe,
  454. .remove = ec_device_remove,
  455. .shutdown = ec_device_shutdown,
  456. };
  457. static int __init cros_ec_dev_init(void)
  458. {
  459. int ret;
  460. dev_t dev = 0;
  461. ret = class_register(&cros_class);
  462. if (ret) {
  463. pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
  464. return ret;
  465. }
  466. /* Get a range of minor numbers (starting with 0) to work with */
  467. ret = alloc_chrdev_region(&dev, 0, CROS_MAX_DEV, CROS_EC_DEV_NAME);
  468. if (ret < 0) {
  469. pr_err(CROS_EC_DEV_NAME ": alloc_chrdev_region() failed\n");
  470. goto failed_chrdevreg;
  471. }
  472. ec_major = MAJOR(dev);
  473. /* Register the driver */
  474. ret = platform_driver_register(&cros_ec_dev_driver);
  475. if (ret < 0) {
  476. pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
  477. goto failed_devreg;
  478. }
  479. return 0;
  480. failed_devreg:
  481. unregister_chrdev_region(MKDEV(ec_major, 0), CROS_MAX_DEV);
  482. failed_chrdevreg:
  483. class_unregister(&cros_class);
  484. return ret;
  485. }
  486. static void __exit cros_ec_dev_exit(void)
  487. {
  488. platform_driver_unregister(&cros_ec_dev_driver);
  489. unregister_chrdev(ec_major, CROS_EC_DEV_NAME);
  490. class_unregister(&cros_class);
  491. }
  492. module_init(cros_ec_dev_init);
  493. module_exit(cros_ec_dev_exit);
  494. MODULE_ALIAS("platform:" DRV_NAME);
  495. MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
  496. MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
  497. MODULE_VERSION("1.0");
  498. MODULE_LICENSE("GPL");