cros_ec_dev.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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_class_release(struct device *dev)
  217. {
  218. kfree(to_cros_ec_dev(dev));
  219. }
  220. static void cros_ec_sensors_register(struct cros_ec_dev *ec)
  221. {
  222. /*
  223. * Issue a command to get the number of sensor reported.
  224. * Build an array of sensors driver and register them all.
  225. */
  226. int ret, i, id, sensor_num;
  227. struct mfd_cell *sensor_cells;
  228. struct cros_ec_sensor_platform *sensor_platforms;
  229. int sensor_type[MOTIONSENSE_TYPE_MAX];
  230. struct ec_params_motion_sense *params;
  231. struct ec_response_motion_sense *resp;
  232. struct cros_ec_command *msg;
  233. msg = kzalloc(sizeof(struct cros_ec_command) +
  234. max(sizeof(*params), sizeof(*resp)), GFP_KERNEL);
  235. if (msg == NULL)
  236. return;
  237. msg->version = 2;
  238. msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
  239. msg->outsize = sizeof(*params);
  240. msg->insize = sizeof(*resp);
  241. params = (struct ec_params_motion_sense *)msg->data;
  242. params->cmd = MOTIONSENSE_CMD_DUMP;
  243. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  244. if (ret < 0 || msg->result != EC_RES_SUCCESS) {
  245. dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n",
  246. ret, msg->result);
  247. goto error;
  248. }
  249. resp = (struct ec_response_motion_sense *)msg->data;
  250. sensor_num = resp->dump.sensor_count;
  251. /* Allocate 1 extra sensors in FIFO are needed */
  252. sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
  253. GFP_KERNEL);
  254. if (sensor_cells == NULL)
  255. goto error;
  256. sensor_platforms = kcalloc(sensor_num + 1,
  257. sizeof(struct cros_ec_sensor_platform),
  258. GFP_KERNEL);
  259. if (sensor_platforms == NULL)
  260. goto error_platforms;
  261. memset(sensor_type, 0, sizeof(sensor_type));
  262. id = 0;
  263. for (i = 0; i < sensor_num; i++) {
  264. params->cmd = MOTIONSENSE_CMD_INFO;
  265. params->info.sensor_num = i;
  266. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  267. if (ret < 0 || msg->result != EC_RES_SUCCESS) {
  268. dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n",
  269. i, ret, msg->result);
  270. continue;
  271. }
  272. switch (resp->info.type) {
  273. case MOTIONSENSE_TYPE_ACCEL:
  274. sensor_cells[id].name = "cros-ec-accel";
  275. break;
  276. case MOTIONSENSE_TYPE_BARO:
  277. sensor_cells[id].name = "cros-ec-baro";
  278. break;
  279. case MOTIONSENSE_TYPE_GYRO:
  280. sensor_cells[id].name = "cros-ec-gyro";
  281. break;
  282. case MOTIONSENSE_TYPE_MAG:
  283. sensor_cells[id].name = "cros-ec-mag";
  284. break;
  285. case MOTIONSENSE_TYPE_PROX:
  286. sensor_cells[id].name = "cros-ec-prox";
  287. break;
  288. case MOTIONSENSE_TYPE_LIGHT:
  289. sensor_cells[id].name = "cros-ec-light";
  290. break;
  291. case MOTIONSENSE_TYPE_ACTIVITY:
  292. sensor_cells[id].name = "cros-ec-activity";
  293. break;
  294. default:
  295. dev_warn(ec->dev, "unknown type %d\n", resp->info.type);
  296. continue;
  297. }
  298. sensor_platforms[id].sensor_num = i;
  299. sensor_cells[id].id = sensor_type[resp->info.type];
  300. sensor_cells[id].platform_data = &sensor_platforms[id];
  301. sensor_cells[id].pdata_size =
  302. sizeof(struct cros_ec_sensor_platform);
  303. sensor_type[resp->info.type]++;
  304. id++;
  305. }
  306. if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
  307. ec->has_kb_wake_angle = true;
  308. if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
  309. sensor_cells[id].name = "cros-ec-ring";
  310. id++;
  311. }
  312. ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
  313. NULL, 0, NULL);
  314. if (ret)
  315. dev_err(ec->dev, "failed to add EC sensors\n");
  316. kfree(sensor_platforms);
  317. error_platforms:
  318. kfree(sensor_cells);
  319. error:
  320. kfree(msg);
  321. }
  322. static const struct mfd_cell cros_ec_cec_cells[] = {
  323. { .name = "cros-ec-cec" }
  324. };
  325. static const struct mfd_cell cros_ec_rtc_cells[] = {
  326. { .name = "cros-ec-rtc" }
  327. };
  328. static const struct mfd_cell cros_usbpd_charger_cells[] = {
  329. { .name = "cros-usbpd-charger" }
  330. };
  331. static int ec_device_probe(struct platform_device *pdev)
  332. {
  333. int retval = -ENOMEM;
  334. struct device *dev = &pdev->dev;
  335. struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
  336. struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
  337. if (!ec)
  338. return retval;
  339. dev_set_drvdata(dev, ec);
  340. ec->ec_dev = dev_get_drvdata(dev->parent);
  341. ec->dev = dev;
  342. ec->cmd_offset = ec_platform->cmd_offset;
  343. ec->features[0] = -1U; /* Not cached yet */
  344. ec->features[1] = -1U; /* Not cached yet */
  345. device_initialize(&ec->class_dev);
  346. cdev_init(&ec->cdev, &fops);
  347. /*
  348. * Add the class device
  349. * Link to the character device for creating the /dev entry
  350. * in devtmpfs.
  351. */
  352. ec->class_dev.devt = MKDEV(ec_major, pdev->id);
  353. ec->class_dev.class = &cros_class;
  354. ec->class_dev.parent = dev;
  355. ec->class_dev.release = cros_ec_class_release;
  356. retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
  357. if (retval) {
  358. dev_err(dev, "dev_set_name failed => %d\n", retval);
  359. goto failed;
  360. }
  361. /* check whether this EC is a sensor hub. */
  362. if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE))
  363. cros_ec_sensors_register(ec);
  364. /* Check whether this EC instance has CEC host command support */
  365. if (cros_ec_check_features(ec, EC_FEATURE_CEC)) {
  366. retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
  367. cros_ec_cec_cells,
  368. ARRAY_SIZE(cros_ec_cec_cells),
  369. NULL, 0, NULL);
  370. if (retval)
  371. dev_err(ec->dev,
  372. "failed to add cros-ec-cec device: %d\n",
  373. retval);
  374. }
  375. /* Check whether this EC instance has RTC host command support */
  376. if (cros_ec_check_features(ec, EC_FEATURE_RTC)) {
  377. retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
  378. cros_ec_rtc_cells,
  379. ARRAY_SIZE(cros_ec_rtc_cells),
  380. NULL, 0, NULL);
  381. if (retval)
  382. dev_err(ec->dev,
  383. "failed to add cros-ec-rtc device: %d\n",
  384. retval);
  385. }
  386. /* Check whether this EC instance has the PD charge manager */
  387. if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) {
  388. retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
  389. cros_usbpd_charger_cells,
  390. ARRAY_SIZE(cros_usbpd_charger_cells),
  391. NULL, 0, NULL);
  392. if (retval)
  393. dev_err(ec->dev,
  394. "failed to add cros-usbpd-charger device: %d\n",
  395. retval);
  396. }
  397. /* Take control of the lightbar from the EC. */
  398. lb_manual_suspend_ctrl(ec, 1);
  399. /* We can now add the sysfs class, we know which parameter to show */
  400. retval = cdev_device_add(&ec->cdev, &ec->class_dev);
  401. if (retval) {
  402. dev_err(dev, "cdev_device_add failed => %d\n", retval);
  403. goto failed;
  404. }
  405. if (cros_ec_debugfs_init(ec))
  406. dev_warn(dev, "failed to create debugfs directory\n");
  407. return 0;
  408. failed:
  409. put_device(&ec->class_dev);
  410. return retval;
  411. }
  412. static int ec_device_remove(struct platform_device *pdev)
  413. {
  414. struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
  415. /* Let the EC take over the lightbar again. */
  416. lb_manual_suspend_ctrl(ec, 0);
  417. cros_ec_debugfs_remove(ec);
  418. mfd_remove_devices(ec->dev);
  419. cdev_del(&ec->cdev);
  420. device_unregister(&ec->class_dev);
  421. return 0;
  422. }
  423. static void ec_device_shutdown(struct platform_device *pdev)
  424. {
  425. struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
  426. /* Be sure to clear up debugfs delayed works */
  427. cros_ec_debugfs_remove(ec);
  428. }
  429. static const struct platform_device_id cros_ec_id[] = {
  430. { DRV_NAME, 0 },
  431. { /* sentinel */ }
  432. };
  433. MODULE_DEVICE_TABLE(platform, cros_ec_id);
  434. static __maybe_unused int ec_device_suspend(struct device *dev)
  435. {
  436. struct cros_ec_dev *ec = dev_get_drvdata(dev);
  437. cros_ec_debugfs_suspend(ec);
  438. lb_suspend(ec);
  439. return 0;
  440. }
  441. static __maybe_unused int ec_device_resume(struct device *dev)
  442. {
  443. struct cros_ec_dev *ec = dev_get_drvdata(dev);
  444. cros_ec_debugfs_resume(ec);
  445. lb_resume(ec);
  446. return 0;
  447. }
  448. static const struct dev_pm_ops cros_ec_dev_pm_ops = {
  449. #ifdef CONFIG_PM_SLEEP
  450. .suspend = ec_device_suspend,
  451. .resume = ec_device_resume,
  452. #endif
  453. };
  454. static struct platform_driver cros_ec_dev_driver = {
  455. .driver = {
  456. .name = DRV_NAME,
  457. .pm = &cros_ec_dev_pm_ops,
  458. },
  459. .probe = ec_device_probe,
  460. .remove = ec_device_remove,
  461. .shutdown = ec_device_shutdown,
  462. };
  463. static int __init cros_ec_dev_init(void)
  464. {
  465. int ret;
  466. dev_t dev = 0;
  467. ret = class_register(&cros_class);
  468. if (ret) {
  469. pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
  470. return ret;
  471. }
  472. /* Get a range of minor numbers (starting with 0) to work with */
  473. ret = alloc_chrdev_region(&dev, 0, CROS_MAX_DEV, CROS_EC_DEV_NAME);
  474. if (ret < 0) {
  475. pr_err(CROS_EC_DEV_NAME ": alloc_chrdev_region() failed\n");
  476. goto failed_chrdevreg;
  477. }
  478. ec_major = MAJOR(dev);
  479. /* Register the driver */
  480. ret = platform_driver_register(&cros_ec_dev_driver);
  481. if (ret < 0) {
  482. pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
  483. goto failed_devreg;
  484. }
  485. return 0;
  486. failed_devreg:
  487. unregister_chrdev_region(MKDEV(ec_major, 0), CROS_MAX_DEV);
  488. failed_chrdevreg:
  489. class_unregister(&cros_class);
  490. return ret;
  491. }
  492. static void __exit cros_ec_dev_exit(void)
  493. {
  494. platform_driver_unregister(&cros_ec_dev_driver);
  495. unregister_chrdev(ec_major, CROS_EC_DEV_NAME);
  496. class_unregister(&cros_class);
  497. }
  498. module_init(cros_ec_dev_init);
  499. module_exit(cros_ec_dev_exit);
  500. MODULE_ALIAS("platform:" DRV_NAME);
  501. MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
  502. MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
  503. MODULE_VERSION("1.0");
  504. MODULE_LICENSE("GPL");