cros_ec_dev.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <linux/uaccess.h>
  25. #include "cros_ec_dev.h"
  26. /* Device variables */
  27. #define CROS_MAX_DEV 128
  28. static int ec_major;
  29. static const struct attribute_group *cros_ec_groups[] = {
  30. &cros_ec_attr_group,
  31. &cros_ec_lightbar_attr_group,
  32. &cros_ec_vbc_attr_group,
  33. NULL,
  34. };
  35. static struct class cros_class = {
  36. .owner = THIS_MODULE,
  37. .name = "chromeos",
  38. .dev_groups = cros_ec_groups,
  39. };
  40. /* Basic communication */
  41. static int ec_get_version(struct cros_ec_dev *ec, char *str, int maxlen)
  42. {
  43. struct ec_response_get_version *resp;
  44. static const char * const current_image_name[] = {
  45. "unknown", "read-only", "read-write", "invalid",
  46. };
  47. struct cros_ec_command *msg;
  48. int ret;
  49. msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
  50. if (!msg)
  51. return -ENOMEM;
  52. msg->version = 0;
  53. msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
  54. msg->insize = sizeof(*resp);
  55. msg->outsize = 0;
  56. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  57. if (ret < 0)
  58. goto exit;
  59. if (msg->result != EC_RES_SUCCESS) {
  60. snprintf(str, maxlen,
  61. "%s\nUnknown EC version: EC returned %d\n",
  62. CROS_EC_DEV_VERSION, msg->result);
  63. ret = -EINVAL;
  64. goto exit;
  65. }
  66. resp = (struct ec_response_get_version *)msg->data;
  67. if (resp->current_image >= ARRAY_SIZE(current_image_name))
  68. resp->current_image = 3; /* invalid */
  69. snprintf(str, maxlen, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION,
  70. resp->version_string_ro, resp->version_string_rw,
  71. current_image_name[resp->current_image]);
  72. ret = 0;
  73. exit:
  74. kfree(msg);
  75. return ret;
  76. }
  77. static int cros_ec_check_features(struct cros_ec_dev *ec, int feature)
  78. {
  79. struct cros_ec_command *msg;
  80. int ret;
  81. if (ec->features[0] == -1U && ec->features[1] == -1U) {
  82. /* features bitmap not read yet */
  83. msg = kmalloc(sizeof(*msg) + sizeof(ec->features), GFP_KERNEL);
  84. if (!msg)
  85. return -ENOMEM;
  86. msg->version = 0;
  87. msg->command = EC_CMD_GET_FEATURES + ec->cmd_offset;
  88. msg->insize = sizeof(ec->features);
  89. msg->outsize = 0;
  90. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  91. if (ret < 0 || msg->result != EC_RES_SUCCESS) {
  92. dev_warn(ec->dev, "cannot get EC features: %d/%d\n",
  93. ret, msg->result);
  94. memset(ec->features, 0, sizeof(ec->features));
  95. }
  96. memcpy(ec->features, msg->data, sizeof(ec->features));
  97. dev_dbg(ec->dev, "EC features %08x %08x\n",
  98. ec->features[0], ec->features[1]);
  99. kfree(msg);
  100. }
  101. return ec->features[feature / 32] & EC_FEATURE_MASK_0(feature);
  102. }
  103. /* Device file ops */
  104. static int ec_device_open(struct inode *inode, struct file *filp)
  105. {
  106. struct cros_ec_dev *ec = container_of(inode->i_cdev,
  107. struct cros_ec_dev, cdev);
  108. filp->private_data = ec;
  109. nonseekable_open(inode, filp);
  110. return 0;
  111. }
  112. static int ec_device_release(struct inode *inode, struct file *filp)
  113. {
  114. return 0;
  115. }
  116. static ssize_t ec_device_read(struct file *filp, char __user *buffer,
  117. size_t length, loff_t *offset)
  118. {
  119. struct cros_ec_dev *ec = filp->private_data;
  120. char msg[sizeof(struct ec_response_get_version) +
  121. sizeof(CROS_EC_DEV_VERSION)];
  122. size_t count;
  123. int ret;
  124. if (*offset != 0)
  125. return 0;
  126. ret = ec_get_version(ec, msg, sizeof(msg));
  127. if (ret)
  128. return ret;
  129. count = min(length, strlen(msg));
  130. if (copy_to_user(buffer, msg, count))
  131. return -EFAULT;
  132. *offset = count;
  133. return count;
  134. }
  135. /* Ioctls */
  136. static long ec_device_ioctl_xcmd(struct cros_ec_dev *ec, void __user *arg)
  137. {
  138. long ret;
  139. struct cros_ec_command u_cmd;
  140. struct cros_ec_command *s_cmd;
  141. if (copy_from_user(&u_cmd, arg, sizeof(u_cmd)))
  142. return -EFAULT;
  143. if ((u_cmd.outsize > EC_MAX_MSG_BYTES) ||
  144. (u_cmd.insize > EC_MAX_MSG_BYTES))
  145. return -EINVAL;
  146. s_cmd = kmalloc(sizeof(*s_cmd) + max(u_cmd.outsize, u_cmd.insize),
  147. GFP_KERNEL);
  148. if (!s_cmd)
  149. return -ENOMEM;
  150. if (copy_from_user(s_cmd, arg, sizeof(*s_cmd) + u_cmd.outsize)) {
  151. ret = -EFAULT;
  152. goto exit;
  153. }
  154. if (u_cmd.outsize != s_cmd->outsize ||
  155. u_cmd.insize != s_cmd->insize) {
  156. ret = -EINVAL;
  157. goto exit;
  158. }
  159. s_cmd->command += ec->cmd_offset;
  160. ret = cros_ec_cmd_xfer(ec->ec_dev, s_cmd);
  161. /* Only copy data to userland if data was received. */
  162. if (ret < 0)
  163. goto exit;
  164. if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + s_cmd->insize))
  165. ret = -EFAULT;
  166. exit:
  167. kfree(s_cmd);
  168. return ret;
  169. }
  170. static long ec_device_ioctl_readmem(struct cros_ec_dev *ec, void __user *arg)
  171. {
  172. struct cros_ec_device *ec_dev = ec->ec_dev;
  173. struct cros_ec_readmem s_mem = { };
  174. long num;
  175. /* Not every platform supports direct reads */
  176. if (!ec_dev->cmd_readmem)
  177. return -ENOTTY;
  178. if (copy_from_user(&s_mem, arg, sizeof(s_mem)))
  179. return -EFAULT;
  180. num = ec_dev->cmd_readmem(ec_dev, s_mem.offset, s_mem.bytes,
  181. s_mem.buffer);
  182. if (num <= 0)
  183. return num;
  184. if (copy_to_user((void __user *)arg, &s_mem, sizeof(s_mem)))
  185. return -EFAULT;
  186. return 0;
  187. }
  188. static long ec_device_ioctl(struct file *filp, unsigned int cmd,
  189. unsigned long arg)
  190. {
  191. struct cros_ec_dev *ec = filp->private_data;
  192. if (_IOC_TYPE(cmd) != CROS_EC_DEV_IOC)
  193. return -ENOTTY;
  194. switch (cmd) {
  195. case CROS_EC_DEV_IOCXCMD:
  196. return ec_device_ioctl_xcmd(ec, (void __user *)arg);
  197. case CROS_EC_DEV_IOCRDMEM:
  198. return ec_device_ioctl_readmem(ec, (void __user *)arg);
  199. }
  200. return -ENOTTY;
  201. }
  202. /* Module initialization */
  203. static const struct file_operations fops = {
  204. .open = ec_device_open,
  205. .release = ec_device_release,
  206. .read = ec_device_read,
  207. .unlocked_ioctl = ec_device_ioctl,
  208. #ifdef CONFIG_COMPAT
  209. .compat_ioctl = ec_device_ioctl,
  210. #endif
  211. };
  212. static void __remove(struct device *dev)
  213. {
  214. struct cros_ec_dev *ec = container_of(dev, struct cros_ec_dev,
  215. class_dev);
  216. kfree(ec);
  217. }
  218. static void cros_ec_sensors_register(struct cros_ec_dev *ec)
  219. {
  220. /*
  221. * Issue a command to get the number of sensor reported.
  222. * Build an array of sensors driver and register them all.
  223. */
  224. int ret, i, id, sensor_num;
  225. struct mfd_cell *sensor_cells;
  226. struct cros_ec_sensor_platform *sensor_platforms;
  227. int sensor_type[MOTIONSENSE_TYPE_MAX];
  228. struct ec_params_motion_sense *params;
  229. struct ec_response_motion_sense *resp;
  230. struct cros_ec_command *msg;
  231. msg = kzalloc(sizeof(struct cros_ec_command) +
  232. max(sizeof(*params), sizeof(*resp)), GFP_KERNEL);
  233. if (msg == NULL)
  234. return;
  235. msg->version = 2;
  236. msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
  237. msg->outsize = sizeof(*params);
  238. msg->insize = sizeof(*resp);
  239. params = (struct ec_params_motion_sense *)msg->data;
  240. params->cmd = MOTIONSENSE_CMD_DUMP;
  241. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  242. if (ret < 0 || msg->result != EC_RES_SUCCESS) {
  243. dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n",
  244. ret, msg->result);
  245. goto error;
  246. }
  247. resp = (struct ec_response_motion_sense *)msg->data;
  248. sensor_num = resp->dump.sensor_count;
  249. /* Allocate 2 extra sensors in case lid angle or FIFO are needed */
  250. sensor_cells = kzalloc(sizeof(struct mfd_cell) * (sensor_num + 2),
  251. GFP_KERNEL);
  252. if (sensor_cells == NULL)
  253. goto error;
  254. sensor_platforms = kzalloc(sizeof(struct cros_ec_sensor_platform) *
  255. (sensor_num + 1), GFP_KERNEL);
  256. if (sensor_platforms == NULL)
  257. goto error_platforms;
  258. memset(sensor_type, 0, sizeof(sensor_type));
  259. id = 0;
  260. for (i = 0; i < sensor_num; i++) {
  261. params->cmd = MOTIONSENSE_CMD_INFO;
  262. params->info.sensor_num = i;
  263. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  264. if (ret < 0 || msg->result != EC_RES_SUCCESS) {
  265. dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n",
  266. i, ret, msg->result);
  267. continue;
  268. }
  269. switch (resp->info.type) {
  270. case MOTIONSENSE_TYPE_ACCEL:
  271. sensor_cells[id].name = "cros-ec-accel";
  272. break;
  273. case MOTIONSENSE_TYPE_GYRO:
  274. sensor_cells[id].name = "cros-ec-gyro";
  275. break;
  276. case MOTIONSENSE_TYPE_MAG:
  277. sensor_cells[id].name = "cros-ec-mag";
  278. break;
  279. case MOTIONSENSE_TYPE_PROX:
  280. sensor_cells[id].name = "cros-ec-prox";
  281. break;
  282. case MOTIONSENSE_TYPE_LIGHT:
  283. sensor_cells[id].name = "cros-ec-light";
  284. break;
  285. case MOTIONSENSE_TYPE_ACTIVITY:
  286. sensor_cells[id].name = "cros-ec-activity";
  287. break;
  288. default:
  289. dev_warn(ec->dev, "unknown type %d\n", resp->info.type);
  290. continue;
  291. }
  292. sensor_platforms[id].sensor_num = i;
  293. sensor_cells[id].id = sensor_type[resp->info.type];
  294. sensor_cells[id].platform_data = &sensor_platforms[id];
  295. sensor_cells[id].pdata_size =
  296. sizeof(struct cros_ec_sensor_platform);
  297. sensor_type[resp->info.type]++;
  298. id++;
  299. }
  300. if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2) {
  301. sensor_platforms[id].sensor_num = sensor_num;
  302. sensor_cells[id].name = "cros-ec-angle";
  303. sensor_cells[id].id = 0;
  304. sensor_cells[id].platform_data = &sensor_platforms[id];
  305. sensor_cells[id].pdata_size =
  306. sizeof(struct cros_ec_sensor_platform);
  307. id++;
  308. }
  309. if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
  310. sensor_cells[id].name = "cros-ec-ring";
  311. id++;
  312. }
  313. ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
  314. NULL, 0, NULL);
  315. if (ret)
  316. dev_err(ec->dev, "failed to add EC sensors\n");
  317. kfree(sensor_platforms);
  318. error_platforms:
  319. kfree(sensor_cells);
  320. error:
  321. kfree(msg);
  322. }
  323. static int ec_device_probe(struct platform_device *pdev)
  324. {
  325. int retval = -ENOMEM;
  326. struct device *dev = &pdev->dev;
  327. struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
  328. dev_t devno = MKDEV(ec_major, pdev->id);
  329. struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
  330. if (!ec)
  331. return retval;
  332. dev_set_drvdata(dev, ec);
  333. ec->ec_dev = dev_get_drvdata(dev->parent);
  334. ec->dev = dev;
  335. ec->cmd_offset = ec_platform->cmd_offset;
  336. ec->features[0] = -1U; /* Not cached yet */
  337. ec->features[1] = -1U; /* Not cached yet */
  338. device_initialize(&ec->class_dev);
  339. cdev_init(&ec->cdev, &fops);
  340. /*
  341. * Add the character device
  342. * Link cdev to the class device to be sure device is not used
  343. * before unbinding it.
  344. */
  345. ec->cdev.kobj.parent = &ec->class_dev.kobj;
  346. retval = cdev_add(&ec->cdev, devno, 1);
  347. if (retval) {
  348. dev_err(dev, ": failed to add character device\n");
  349. goto cdev_add_failed;
  350. }
  351. /*
  352. * Add the class device
  353. * Link to the character device for creating the /dev entry
  354. * in devtmpfs.
  355. */
  356. ec->class_dev.devt = ec->cdev.dev;
  357. ec->class_dev.class = &cros_class;
  358. ec->class_dev.parent = dev;
  359. ec->class_dev.release = __remove;
  360. retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
  361. if (retval) {
  362. dev_err(dev, "dev_set_name failed => %d\n", retval);
  363. goto set_named_failed;
  364. }
  365. retval = device_add(&ec->class_dev);
  366. if (retval) {
  367. dev_err(dev, "device_register failed => %d\n", retval);
  368. goto dev_reg_failed;
  369. }
  370. /* check whether this EC is a sensor hub. */
  371. if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE))
  372. cros_ec_sensors_register(ec);
  373. return 0;
  374. dev_reg_failed:
  375. set_named_failed:
  376. dev_set_drvdata(dev, NULL);
  377. cdev_del(&ec->cdev);
  378. cdev_add_failed:
  379. kfree(ec);
  380. return retval;
  381. }
  382. static int ec_device_remove(struct platform_device *pdev)
  383. {
  384. struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
  385. cdev_del(&ec->cdev);
  386. device_unregister(&ec->class_dev);
  387. return 0;
  388. }
  389. static const struct platform_device_id cros_ec_id[] = {
  390. { "cros-ec-ctl", 0 },
  391. { /* sentinel */ },
  392. };
  393. MODULE_DEVICE_TABLE(platform, cros_ec_id);
  394. static struct platform_driver cros_ec_dev_driver = {
  395. .driver = {
  396. .name = "cros-ec-ctl",
  397. },
  398. .probe = ec_device_probe,
  399. .remove = ec_device_remove,
  400. };
  401. static int __init cros_ec_dev_init(void)
  402. {
  403. int ret;
  404. dev_t dev = 0;
  405. ret = class_register(&cros_class);
  406. if (ret) {
  407. pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
  408. return ret;
  409. }
  410. /* Get a range of minor numbers (starting with 0) to work with */
  411. ret = alloc_chrdev_region(&dev, 0, CROS_MAX_DEV, CROS_EC_DEV_NAME);
  412. if (ret < 0) {
  413. pr_err(CROS_EC_DEV_NAME ": alloc_chrdev_region() failed\n");
  414. goto failed_chrdevreg;
  415. }
  416. ec_major = MAJOR(dev);
  417. /* Register the driver */
  418. ret = platform_driver_register(&cros_ec_dev_driver);
  419. if (ret < 0) {
  420. pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
  421. goto failed_devreg;
  422. }
  423. return 0;
  424. failed_devreg:
  425. unregister_chrdev_region(MKDEV(ec_major, 0), CROS_MAX_DEV);
  426. failed_chrdevreg:
  427. class_unregister(&cros_class);
  428. return ret;
  429. }
  430. static void __exit cros_ec_dev_exit(void)
  431. {
  432. platform_driver_unregister(&cros_ec_dev_driver);
  433. unregister_chrdev(ec_major, CROS_EC_DEV_NAME);
  434. class_unregister(&cros_class);
  435. }
  436. module_init(cros_ec_dev_init);
  437. module_exit(cros_ec_dev_exit);
  438. MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
  439. MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
  440. MODULE_VERSION("1.0");
  441. MODULE_LICENSE("GPL");