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 20 #include <linux/fs.h> 21 #include <linux/mfd/core.h> 22 #include <linux/module.h> 23 #include <linux/mod_devicetable.h> 24 #include <linux/of_platform.h> 25 #include <linux/platform_device.h> 26 #include <linux/pm.h> 27 #include <linux/slab.h> 28 #include <linux/uaccess.h> 29 30 #include "cros_ec_dev.h" 31 32 #define DRV_NAME "cros-ec-dev" 33 34 /* Device variables */ 35 #define CROS_MAX_DEV 128 36 static int ec_major; 37 38 static struct class cros_class = { 39 .owner = THIS_MODULE, 40 .name = "chromeos", 41 }; 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 53 msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL); 54 if (!msg) 55 return -ENOMEM; 56 57 msg->version = 0; 58 msg->command = EC_CMD_GET_VERSION + ec->cmd_offset; 59 msg->insize = sizeof(*resp); 60 msg->outsize = 0; 61 62 ret = cros_ec_cmd_xfer(ec->ec_dev, msg); 63 if (ret < 0) 64 goto exit; 65 66 if (msg->result != EC_RES_SUCCESS) { 67 snprintf(str, maxlen, 68 "%s\nUnknown EC version: EC returned %d\n", 69 CROS_EC_DEV_VERSION, msg->result); 70 ret = -EINVAL; 71 goto exit; 72 } 73 74 resp = (struct ec_response_get_version *)msg->data; 75 if (resp->current_image >= ARRAY_SIZE(current_image_name)) 76 resp->current_image = 3; /* invalid */ 77 78 snprintf(str, maxlen, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION, 79 resp->version_string_ro, resp->version_string_rw, 80 current_image_name[resp->current_image]); 81 82 ret = 0; 83 exit: 84 kfree(msg); 85 return ret; 86 } 87 88 static int cros_ec_check_features(struct cros_ec_dev *ec, int feature) 89 { 90 struct cros_ec_command *msg; 91 int ret; 92 93 if (ec->features[0] == -1U && ec->features[1] == -1U) { 94 /* features bitmap not read yet */ 95 96 msg = kmalloc(sizeof(*msg) + sizeof(ec->features), GFP_KERNEL); 97 if (!msg) 98 return -ENOMEM; 99 100 msg->version = 0; 101 msg->command = EC_CMD_GET_FEATURES + ec->cmd_offset; 102 msg->insize = sizeof(ec->features); 103 msg->outsize = 0; 104 105 ret = cros_ec_cmd_xfer(ec->ec_dev, msg); 106 if (ret < 0 || msg->result != EC_RES_SUCCESS) { 107 dev_warn(ec->dev, "cannot get EC features: %d/%d\n", 108 ret, msg->result); 109 memset(ec->features, 0, sizeof(ec->features)); 110 } else { 111 memcpy(ec->features, msg->data, sizeof(ec->features)); 112 } 113 114 dev_dbg(ec->dev, "EC features %08x %08x\n", 115 ec->features[0], ec->features[1]); 116 117 kfree(msg); 118 } 119 120 return ec->features[feature / 32] & EC_FEATURE_MASK_0(feature); 121 } 122 123 /* Device file ops */ 124 static int ec_device_open(struct inode *inode, struct file *filp) 125 { 126 struct cros_ec_dev *ec = container_of(inode->i_cdev, 127 struct cros_ec_dev, cdev); 128 filp->private_data = ec; 129 nonseekable_open(inode, filp); 130 return 0; 131 } 132 133 static int ec_device_release(struct inode *inode, struct file *filp) 134 { 135 return 0; 136 } 137 138 static ssize_t ec_device_read(struct file *filp, char __user *buffer, 139 size_t length, loff_t *offset) 140 { 141 struct cros_ec_dev *ec = filp->private_data; 142 char msg[sizeof(struct ec_response_get_version) + 143 sizeof(CROS_EC_DEV_VERSION)]; 144 size_t count; 145 int ret; 146 147 if (*offset != 0) 148 return 0; 149 150 ret = ec_get_version(ec, msg, sizeof(msg)); 151 if (ret) 152 return ret; 153 154 count = min(length, strlen(msg)); 155 156 if (copy_to_user(buffer, msg, count)) 157 return -EFAULT; 158 159 *offset = count; 160 return count; 161 } 162 163 /* Ioctls */ 164 static long ec_device_ioctl_xcmd(struct cros_ec_dev *ec, void __user *arg) 165 { 166 long ret; 167 struct cros_ec_command u_cmd; 168 struct cros_ec_command *s_cmd; 169 170 if (copy_from_user(&u_cmd, arg, sizeof(u_cmd))) 171 return -EFAULT; 172 173 if ((u_cmd.outsize > EC_MAX_MSG_BYTES) || 174 (u_cmd.insize > EC_MAX_MSG_BYTES)) 175 return -EINVAL; 176 177 s_cmd = kmalloc(sizeof(*s_cmd) + max(u_cmd.outsize, u_cmd.insize), 178 GFP_KERNEL); 179 if (!s_cmd) 180 return -ENOMEM; 181 182 if (copy_from_user(s_cmd, arg, sizeof(*s_cmd) + u_cmd.outsize)) { 183 ret = -EFAULT; 184 goto exit; 185 } 186 187 if (u_cmd.outsize != s_cmd->outsize || 188 u_cmd.insize != s_cmd->insize) { 189 ret = -EINVAL; 190 goto exit; 191 } 192 193 s_cmd->command += ec->cmd_offset; 194 ret = cros_ec_cmd_xfer(ec->ec_dev, s_cmd); 195 /* Only copy data to userland if data was received. */ 196 if (ret < 0) 197 goto exit; 198 199 if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + s_cmd->insize)) 200 ret = -EFAULT; 201 exit: 202 kfree(s_cmd); 203 return ret; 204 } 205 206 static long ec_device_ioctl_readmem(struct cros_ec_dev *ec, void __user *arg) 207 { 208 struct cros_ec_device *ec_dev = ec->ec_dev; 209 struct cros_ec_readmem s_mem = { }; 210 long num; 211 212 /* Not every platform supports direct reads */ 213 if (!ec_dev->cmd_readmem) 214 return -ENOTTY; 215 216 if (copy_from_user(&s_mem, arg, sizeof(s_mem))) 217 return -EFAULT; 218 219 num = ec_dev->cmd_readmem(ec_dev, s_mem.offset, s_mem.bytes, 220 s_mem.buffer); 221 if (num <= 0) 222 return num; 223 224 if (copy_to_user((void __user *)arg, &s_mem, sizeof(s_mem))) 225 return -EFAULT; 226 227 return num; 228 } 229 230 static long ec_device_ioctl(struct file *filp, unsigned int cmd, 231 unsigned long arg) 232 { 233 struct cros_ec_dev *ec = filp->private_data; 234 235 if (_IOC_TYPE(cmd) != CROS_EC_DEV_IOC) 236 return -ENOTTY; 237 238 switch (cmd) { 239 case CROS_EC_DEV_IOCXCMD: 240 return ec_device_ioctl_xcmd(ec, (void __user *)arg); 241 case CROS_EC_DEV_IOCRDMEM: 242 return ec_device_ioctl_readmem(ec, (void __user *)arg); 243 } 244 245 return -ENOTTY; 246 } 247 248 /* Module initialization */ 249 static const struct file_operations fops = { 250 .open = ec_device_open, 251 .release = ec_device_release, 252 .read = ec_device_read, 253 .unlocked_ioctl = ec_device_ioctl, 254 #ifdef CONFIG_COMPAT 255 .compat_ioctl = ec_device_ioctl, 256 #endif 257 }; 258 259 static void cros_ec_class_release(struct device *dev) 260 { 261 kfree(to_cros_ec_dev(dev)); 262 } 263 264 static void cros_ec_sensors_register(struct cros_ec_dev *ec) 265 { 266 /* 267 * Issue a command to get the number of sensor reported. 268 * Build an array of sensors driver and register them all. 269 */ 270 int ret, i, id, sensor_num; 271 struct mfd_cell *sensor_cells; 272 struct cros_ec_sensor_platform *sensor_platforms; 273 int sensor_type[MOTIONSENSE_TYPE_MAX]; 274 struct ec_params_motion_sense *params; 275 struct ec_response_motion_sense *resp; 276 struct cros_ec_command *msg; 277 278 msg = kzalloc(sizeof(struct cros_ec_command) + 279 max(sizeof(*params), sizeof(*resp)), GFP_KERNEL); 280 if (msg == NULL) 281 return; 282 283 msg->version = 2; 284 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset; 285 msg->outsize = sizeof(*params); 286 msg->insize = sizeof(*resp); 287 288 params = (struct ec_params_motion_sense *)msg->data; 289 params->cmd = MOTIONSENSE_CMD_DUMP; 290 291 ret = cros_ec_cmd_xfer(ec->ec_dev, msg); 292 if (ret < 0 || msg->result != EC_RES_SUCCESS) { 293 dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n", 294 ret, msg->result); 295 goto error; 296 } 297 298 resp = (struct ec_response_motion_sense *)msg->data; 299 sensor_num = resp->dump.sensor_count; 300 /* Allocate 1 extra sensors in FIFO are needed */ 301 sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell), 302 GFP_KERNEL); 303 if (sensor_cells == NULL) 304 goto error; 305 306 sensor_platforms = kcalloc(sensor_num + 1, 307 sizeof(struct cros_ec_sensor_platform), 308 GFP_KERNEL); 309 if (sensor_platforms == NULL) 310 goto error_platforms; 311 312 memset(sensor_type, 0, sizeof(sensor_type)); 313 id = 0; 314 for (i = 0; i < sensor_num; i++) { 315 params->cmd = MOTIONSENSE_CMD_INFO; 316 params->info.sensor_num = i; 317 ret = cros_ec_cmd_xfer(ec->ec_dev, msg); 318 if (ret < 0 || msg->result != EC_RES_SUCCESS) { 319 dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n", 320 i, ret, msg->result); 321 continue; 322 } 323 switch (resp->info.type) { 324 case MOTIONSENSE_TYPE_ACCEL: 325 sensor_cells[id].name = "cros-ec-accel"; 326 break; 327 case MOTIONSENSE_TYPE_BARO: 328 sensor_cells[id].name = "cros-ec-baro"; 329 break; 330 case MOTIONSENSE_TYPE_GYRO: 331 sensor_cells[id].name = "cros-ec-gyro"; 332 break; 333 case MOTIONSENSE_TYPE_MAG: 334 sensor_cells[id].name = "cros-ec-mag"; 335 break; 336 case MOTIONSENSE_TYPE_PROX: 337 sensor_cells[id].name = "cros-ec-prox"; 338 break; 339 case MOTIONSENSE_TYPE_LIGHT: 340 sensor_cells[id].name = "cros-ec-light"; 341 break; 342 case MOTIONSENSE_TYPE_ACTIVITY: 343 sensor_cells[id].name = "cros-ec-activity"; 344 break; 345 default: 346 dev_warn(ec->dev, "unknown type %d\n", resp->info.type); 347 continue; 348 } 349 sensor_platforms[id].sensor_num = i; 350 sensor_cells[id].id = sensor_type[resp->info.type]; 351 sensor_cells[id].platform_data = &sensor_platforms[id]; 352 sensor_cells[id].pdata_size = 353 sizeof(struct cros_ec_sensor_platform); 354 355 sensor_type[resp->info.type]++; 356 id++; 357 } 358 359 if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2) 360 ec->has_kb_wake_angle = true; 361 362 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) { 363 sensor_cells[id].name = "cros-ec-ring"; 364 id++; 365 } 366 367 ret = mfd_add_devices(ec->dev, 0, sensor_cells, id, 368 NULL, 0, NULL); 369 if (ret) 370 dev_err(ec->dev, "failed to add EC sensors\n"); 371 372 kfree(sensor_platforms); 373 error_platforms: 374 kfree(sensor_cells); 375 error: 376 kfree(msg); 377 } 378 379 static const struct mfd_cell cros_ec_cec_cells[] = { 380 { .name = "cros-ec-cec" } 381 }; 382 383 static const struct mfd_cell cros_ec_rtc_cells[] = { 384 { .name = "cros-ec-rtc" } 385 }; 386 387 static const struct mfd_cell cros_usbpd_charger_cells[] = { 388 { .name = "cros-usbpd-charger" }, 389 { .name = "cros-usbpd-logger" }, 390 }; 391 392 static const struct mfd_cell cros_ec_platform_cells[] = { 393 { .name = "cros-ec-debugfs" }, 394 { .name = "cros-ec-lightbar" }, 395 { .name = "cros-ec-sysfs" }, 396 }; 397 398 static const struct mfd_cell cros_ec_vbc_cells[] = { 399 { .name = "cros-ec-vbc" } 400 }; 401 402 static int ec_device_probe(struct platform_device *pdev) 403 { 404 int retval = -ENOMEM; 405 struct device_node *node; 406 struct device *dev = &pdev->dev; 407 struct cros_ec_platform *ec_platform = dev_get_platdata(dev); 408 struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL); 409 410 if (!ec) 411 return retval; 412 413 dev_set_drvdata(dev, ec); 414 ec->ec_dev = dev_get_drvdata(dev->parent); 415 ec->dev = dev; 416 ec->cmd_offset = ec_platform->cmd_offset; 417 ec->features[0] = -1U; /* Not cached yet */ 418 ec->features[1] = -1U; /* Not cached yet */ 419 device_initialize(&ec->class_dev); 420 cdev_init(&ec->cdev, &fops); 421 422 /* Check whether this is actually a Fingerprint MCU rather than an EC */ 423 if (cros_ec_check_features(ec, EC_FEATURE_FINGERPRINT)) { 424 dev_info(dev, "CrOS Fingerprint MCU detected.\n"); 425 /* 426 * Help userspace differentiating ECs from FP MCU, 427 * regardless of the probing order. 428 */ 429 ec_platform->ec_name = CROS_EC_DEV_FP_NAME; 430 } 431 432 /* 433 * Check whether this is actually an Integrated Sensor Hub (ISH) 434 * rather than an EC. 435 */ 436 if (cros_ec_check_features(ec, EC_FEATURE_ISH)) { 437 dev_info(dev, "CrOS ISH MCU detected.\n"); 438 /* 439 * Help userspace differentiating ECs from ISH MCU, 440 * regardless of the probing order. 441 */ 442 ec_platform->ec_name = CROS_EC_DEV_ISH_NAME; 443 } 444 445 /* Check whether this is actually a Touchpad MCU rather than an EC */ 446 if (cros_ec_check_features(ec, EC_FEATURE_TOUCHPAD)) { 447 dev_info(dev, "CrOS Touchpad MCU detected.\n"); 448 /* 449 * Help userspace differentiating ECs from TP MCU, 450 * regardless of the probing order. 451 */ 452 ec_platform->ec_name = CROS_EC_DEV_TP_NAME; 453 } 454 455 /* 456 * Add the class device 457 * Link to the character device for creating the /dev entry 458 * in devtmpfs. 459 */ 460 ec->class_dev.devt = MKDEV(ec_major, pdev->id); 461 ec->class_dev.class = &cros_class; 462 ec->class_dev.parent = dev; 463 ec->class_dev.release = cros_ec_class_release; 464 465 retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name); 466 if (retval) { 467 dev_err(dev, "dev_set_name failed => %d\n", retval); 468 goto failed; 469 } 470 471 /* check whether this EC is a sensor hub. */ 472 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE)) 473 cros_ec_sensors_register(ec); 474 475 /* Check whether this EC instance has CEC host command support */ 476 if (cros_ec_check_features(ec, EC_FEATURE_CEC)) { 477 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO, 478 cros_ec_cec_cells, 479 ARRAY_SIZE(cros_ec_cec_cells), 480 NULL, 0, NULL); 481 if (retval) 482 dev_err(ec->dev, 483 "failed to add cros-ec-cec device: %d\n", 484 retval); 485 } 486 487 /* Check whether this EC instance has RTC host command support */ 488 if (cros_ec_check_features(ec, EC_FEATURE_RTC)) { 489 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO, 490 cros_ec_rtc_cells, 491 ARRAY_SIZE(cros_ec_rtc_cells), 492 NULL, 0, NULL); 493 if (retval) 494 dev_err(ec->dev, 495 "failed to add cros-ec-rtc device: %d\n", 496 retval); 497 } 498 499 /* Check whether this EC instance has the PD charge manager */ 500 if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) { 501 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO, 502 cros_usbpd_charger_cells, 503 ARRAY_SIZE(cros_usbpd_charger_cells), 504 NULL, 0, NULL); 505 if (retval) 506 dev_err(ec->dev, 507 "failed to add cros-usbpd-charger device: %d\n", 508 retval); 509 } 510 511 /* We can now add the sysfs class, we know which parameter to show */ 512 retval = cdev_device_add(&ec->cdev, &ec->class_dev); 513 if (retval) { 514 dev_err(dev, "cdev_device_add failed => %d\n", retval); 515 goto failed; 516 } 517 518 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO, 519 cros_ec_platform_cells, 520 ARRAY_SIZE(cros_ec_platform_cells), 521 NULL, 0, NULL); 522 if (retval) 523 dev_warn(ec->dev, 524 "failed to add cros-ec platform devices: %d\n", 525 retval); 526 527 /* Check whether this EC instance has a VBC NVRAM */ 528 node = ec->ec_dev->dev->of_node; 529 if (of_property_read_bool(node, "google,has-vbc-nvram")) { 530 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO, 531 cros_ec_vbc_cells, 532 ARRAY_SIZE(cros_ec_vbc_cells), 533 NULL, 0, NULL); 534 if (retval) 535 dev_warn(ec->dev, "failed to add VBC devices: %d\n", 536 retval); 537 } 538 539 return 0; 540 541 failed: 542 put_device(&ec->class_dev); 543 return retval; 544 } 545 546 static int ec_device_remove(struct platform_device *pdev) 547 { 548 struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev); 549 550 mfd_remove_devices(ec->dev); 551 cdev_del(&ec->cdev); 552 device_unregister(&ec->class_dev); 553 return 0; 554 } 555 556 static const struct platform_device_id cros_ec_id[] = { 557 { DRV_NAME, 0 }, 558 { /* sentinel */ } 559 }; 560 MODULE_DEVICE_TABLE(platform, cros_ec_id); 561 562 static struct platform_driver cros_ec_dev_driver = { 563 .driver = { 564 .name = DRV_NAME, 565 }, 566 .id_table = cros_ec_id, 567 .probe = ec_device_probe, 568 .remove = ec_device_remove, 569 }; 570 571 static int __init cros_ec_dev_init(void) 572 { 573 int ret; 574 dev_t dev = 0; 575 576 ret = class_register(&cros_class); 577 if (ret) { 578 pr_err(CROS_EC_DEV_NAME ": failed to register device class\n"); 579 return ret; 580 } 581 582 /* Get a range of minor numbers (starting with 0) to work with */ 583 ret = alloc_chrdev_region(&dev, 0, CROS_MAX_DEV, CROS_EC_DEV_NAME); 584 if (ret < 0) { 585 pr_err(CROS_EC_DEV_NAME ": alloc_chrdev_region() failed\n"); 586 goto failed_chrdevreg; 587 } 588 ec_major = MAJOR(dev); 589 590 /* Register the driver */ 591 ret = platform_driver_register(&cros_ec_dev_driver); 592 if (ret < 0) { 593 pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret); 594 goto failed_devreg; 595 } 596 return 0; 597 598 failed_devreg: 599 unregister_chrdev_region(MKDEV(ec_major, 0), CROS_MAX_DEV); 600 failed_chrdevreg: 601 class_unregister(&cros_class); 602 return ret; 603 } 604 605 static void __exit cros_ec_dev_exit(void) 606 { 607 platform_driver_unregister(&cros_ec_dev_driver); 608 unregister_chrdev(ec_major, CROS_EC_DEV_NAME); 609 class_unregister(&cros_class); 610 } 611 612 module_init(cros_ec_dev_init); 613 module_exit(cros_ec_dev_exit); 614 615 MODULE_ALIAS("platform:" DRV_NAME); 616 MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>"); 617 MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller"); 618 MODULE_VERSION("1.0"); 619 MODULE_LICENSE("GPL"); 620