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