1 /* 2 * Copyright (c) 2015-2016, Linaro Limited 3 * 4 * This software is licensed under the terms of the GNU General Public 5 * License version 2, as published by the Free Software Foundation, and 6 * may be copied, distributed, and modified under those terms. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 */ 14 15 #define pr_fmt(fmt) "%s: " fmt, __func__ 16 17 #include <linux/cdev.h> 18 #include <linux/device.h> 19 #include <linux/fs.h> 20 #include <linux/idr.h> 21 #include <linux/module.h> 22 #include <linux/slab.h> 23 #include <linux/tee_drv.h> 24 #include <linux/uaccess.h> 25 #include "tee_private.h" 26 27 #define TEE_NUM_DEVICES 32 28 29 #define TEE_IOCTL_PARAM_SIZE(x) (sizeof(struct tee_param) * (x)) 30 31 /* 32 * Unprivileged devices in the lower half range and privileged devices in 33 * the upper half range. 34 */ 35 static DECLARE_BITMAP(dev_mask, TEE_NUM_DEVICES); 36 static DEFINE_SPINLOCK(driver_lock); 37 38 static struct class *tee_class; 39 static dev_t tee_devt; 40 41 static int tee_open(struct inode *inode, struct file *filp) 42 { 43 int rc; 44 struct tee_device *teedev; 45 struct tee_context *ctx; 46 47 teedev = container_of(inode->i_cdev, struct tee_device, cdev); 48 if (!tee_device_get(teedev)) 49 return -EINVAL; 50 51 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 52 if (!ctx) { 53 rc = -ENOMEM; 54 goto err; 55 } 56 57 kref_init(&ctx->refcount); 58 ctx->teedev = teedev; 59 INIT_LIST_HEAD(&ctx->list_shm); 60 filp->private_data = ctx; 61 rc = teedev->desc->ops->open(ctx); 62 if (rc) 63 goto err; 64 65 return 0; 66 err: 67 kfree(ctx); 68 tee_device_put(teedev); 69 return rc; 70 } 71 72 void teedev_ctx_get(struct tee_context *ctx) 73 { 74 if (ctx->releasing) 75 return; 76 77 kref_get(&ctx->refcount); 78 } 79 80 static void teedev_ctx_release(struct kref *ref) 81 { 82 struct tee_context *ctx = container_of(ref, struct tee_context, 83 refcount); 84 ctx->releasing = true; 85 ctx->teedev->desc->ops->release(ctx); 86 kfree(ctx); 87 } 88 89 void teedev_ctx_put(struct tee_context *ctx) 90 { 91 if (ctx->releasing) 92 return; 93 94 kref_put(&ctx->refcount, teedev_ctx_release); 95 } 96 97 static void teedev_close_context(struct tee_context *ctx) 98 { 99 tee_device_put(ctx->teedev); 100 teedev_ctx_put(ctx); 101 } 102 103 static int tee_release(struct inode *inode, struct file *filp) 104 { 105 teedev_close_context(filp->private_data); 106 return 0; 107 } 108 109 static int tee_ioctl_version(struct tee_context *ctx, 110 struct tee_ioctl_version_data __user *uvers) 111 { 112 struct tee_ioctl_version_data vers; 113 114 ctx->teedev->desc->ops->get_version(ctx->teedev, &vers); 115 116 if (ctx->teedev->desc->flags & TEE_DESC_PRIVILEGED) 117 vers.gen_caps |= TEE_GEN_CAP_PRIVILEGED; 118 119 if (copy_to_user(uvers, &vers, sizeof(vers))) 120 return -EFAULT; 121 122 return 0; 123 } 124 125 static int tee_ioctl_shm_alloc(struct tee_context *ctx, 126 struct tee_ioctl_shm_alloc_data __user *udata) 127 { 128 long ret; 129 struct tee_ioctl_shm_alloc_data data; 130 struct tee_shm *shm; 131 132 if (copy_from_user(&data, udata, sizeof(data))) 133 return -EFAULT; 134 135 /* Currently no input flags are supported */ 136 if (data.flags) 137 return -EINVAL; 138 139 shm = tee_shm_alloc(ctx, data.size, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF); 140 if (IS_ERR(shm)) 141 return PTR_ERR(shm); 142 143 data.id = shm->id; 144 data.flags = shm->flags; 145 data.size = shm->size; 146 147 if (copy_to_user(udata, &data, sizeof(data))) 148 ret = -EFAULT; 149 else 150 ret = tee_shm_get_fd(shm); 151 152 /* 153 * When user space closes the file descriptor the shared memory 154 * should be freed or if tee_shm_get_fd() failed then it will 155 * be freed immediately. 156 */ 157 tee_shm_put(shm); 158 return ret; 159 } 160 161 static int 162 tee_ioctl_shm_register(struct tee_context *ctx, 163 struct tee_ioctl_shm_register_data __user *udata) 164 { 165 long ret; 166 struct tee_ioctl_shm_register_data data; 167 struct tee_shm *shm; 168 169 if (copy_from_user(&data, udata, sizeof(data))) 170 return -EFAULT; 171 172 /* Currently no input flags are supported */ 173 if (data.flags) 174 return -EINVAL; 175 176 shm = tee_shm_register(ctx, data.addr, data.length, 177 TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED); 178 if (IS_ERR(shm)) 179 return PTR_ERR(shm); 180 181 data.id = shm->id; 182 data.flags = shm->flags; 183 data.length = shm->size; 184 185 if (copy_to_user(udata, &data, sizeof(data))) 186 ret = -EFAULT; 187 else 188 ret = tee_shm_get_fd(shm); 189 /* 190 * When user space closes the file descriptor the shared memory 191 * should be freed or if tee_shm_get_fd() failed then it will 192 * be freed immediately. 193 */ 194 tee_shm_put(shm); 195 return ret; 196 } 197 198 static int params_from_user(struct tee_context *ctx, struct tee_param *params, 199 size_t num_params, 200 struct tee_ioctl_param __user *uparams) 201 { 202 size_t n; 203 204 for (n = 0; n < num_params; n++) { 205 struct tee_shm *shm; 206 struct tee_ioctl_param ip; 207 208 if (copy_from_user(&ip, uparams + n, sizeof(ip))) 209 return -EFAULT; 210 211 /* All unused attribute bits has to be zero */ 212 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK) 213 return -EINVAL; 214 215 params[n].attr = ip.attr; 216 switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) { 217 case TEE_IOCTL_PARAM_ATTR_TYPE_NONE: 218 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT: 219 break; 220 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT: 221 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT: 222 params[n].u.value.a = ip.a; 223 params[n].u.value.b = ip.b; 224 params[n].u.value.c = ip.c; 225 break; 226 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT: 227 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: 228 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: 229 /* 230 * If we fail to get a pointer to a shared memory 231 * object (and increase the ref count) from an 232 * identifier we return an error. All pointers that 233 * has been added in params have an increased ref 234 * count. It's the callers responibility to do 235 * tee_shm_put() on all resolved pointers. 236 */ 237 shm = tee_shm_get_from_id(ctx, ip.c); 238 if (IS_ERR(shm)) 239 return PTR_ERR(shm); 240 241 params[n].u.memref.shm_offs = ip.a; 242 params[n].u.memref.size = ip.b; 243 params[n].u.memref.shm = shm; 244 break; 245 default: 246 /* Unknown attribute */ 247 return -EINVAL; 248 } 249 } 250 return 0; 251 } 252 253 static int params_to_user(struct tee_ioctl_param __user *uparams, 254 size_t num_params, struct tee_param *params) 255 { 256 size_t n; 257 258 for (n = 0; n < num_params; n++) { 259 struct tee_ioctl_param __user *up = uparams + n; 260 struct tee_param *p = params + n; 261 262 switch (p->attr) { 263 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT: 264 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT: 265 if (put_user(p->u.value.a, &up->a) || 266 put_user(p->u.value.b, &up->b) || 267 put_user(p->u.value.c, &up->c)) 268 return -EFAULT; 269 break; 270 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: 271 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: 272 if (put_user((u64)p->u.memref.size, &up->b)) 273 return -EFAULT; 274 default: 275 break; 276 } 277 } 278 return 0; 279 } 280 281 static int tee_ioctl_open_session(struct tee_context *ctx, 282 struct tee_ioctl_buf_data __user *ubuf) 283 { 284 int rc; 285 size_t n; 286 struct tee_ioctl_buf_data buf; 287 struct tee_ioctl_open_session_arg __user *uarg; 288 struct tee_ioctl_open_session_arg arg; 289 struct tee_ioctl_param __user *uparams = NULL; 290 struct tee_param *params = NULL; 291 bool have_session = false; 292 293 if (!ctx->teedev->desc->ops->open_session) 294 return -EINVAL; 295 296 if (copy_from_user(&buf, ubuf, sizeof(buf))) 297 return -EFAULT; 298 299 if (buf.buf_len > TEE_MAX_ARG_SIZE || 300 buf.buf_len < sizeof(struct tee_ioctl_open_session_arg)) 301 return -EINVAL; 302 303 uarg = u64_to_user_ptr(buf.buf_ptr); 304 if (copy_from_user(&arg, uarg, sizeof(arg))) 305 return -EFAULT; 306 307 if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len) 308 return -EINVAL; 309 310 if (arg.num_params) { 311 params = kcalloc(arg.num_params, sizeof(struct tee_param), 312 GFP_KERNEL); 313 if (!params) 314 return -ENOMEM; 315 uparams = uarg->params; 316 rc = params_from_user(ctx, params, arg.num_params, uparams); 317 if (rc) 318 goto out; 319 } 320 321 rc = ctx->teedev->desc->ops->open_session(ctx, &arg, params); 322 if (rc) 323 goto out; 324 have_session = true; 325 326 if (put_user(arg.session, &uarg->session) || 327 put_user(arg.ret, &uarg->ret) || 328 put_user(arg.ret_origin, &uarg->ret_origin)) { 329 rc = -EFAULT; 330 goto out; 331 } 332 rc = params_to_user(uparams, arg.num_params, params); 333 out: 334 /* 335 * If we've succeeded to open the session but failed to communicate 336 * it back to user space, close the session again to avoid leakage. 337 */ 338 if (rc && have_session && ctx->teedev->desc->ops->close_session) 339 ctx->teedev->desc->ops->close_session(ctx, arg.session); 340 341 if (params) { 342 /* Decrease ref count for all valid shared memory pointers */ 343 for (n = 0; n < arg.num_params; n++) 344 if (tee_param_is_memref(params + n) && 345 params[n].u.memref.shm) 346 tee_shm_put(params[n].u.memref.shm); 347 kfree(params); 348 } 349 350 return rc; 351 } 352 353 static int tee_ioctl_invoke(struct tee_context *ctx, 354 struct tee_ioctl_buf_data __user *ubuf) 355 { 356 int rc; 357 size_t n; 358 struct tee_ioctl_buf_data buf; 359 struct tee_ioctl_invoke_arg __user *uarg; 360 struct tee_ioctl_invoke_arg arg; 361 struct tee_ioctl_param __user *uparams = NULL; 362 struct tee_param *params = NULL; 363 364 if (!ctx->teedev->desc->ops->invoke_func) 365 return -EINVAL; 366 367 if (copy_from_user(&buf, ubuf, sizeof(buf))) 368 return -EFAULT; 369 370 if (buf.buf_len > TEE_MAX_ARG_SIZE || 371 buf.buf_len < sizeof(struct tee_ioctl_invoke_arg)) 372 return -EINVAL; 373 374 uarg = u64_to_user_ptr(buf.buf_ptr); 375 if (copy_from_user(&arg, uarg, sizeof(arg))) 376 return -EFAULT; 377 378 if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len) 379 return -EINVAL; 380 381 if (arg.num_params) { 382 params = kcalloc(arg.num_params, sizeof(struct tee_param), 383 GFP_KERNEL); 384 if (!params) 385 return -ENOMEM; 386 uparams = uarg->params; 387 rc = params_from_user(ctx, params, arg.num_params, uparams); 388 if (rc) 389 goto out; 390 } 391 392 rc = ctx->teedev->desc->ops->invoke_func(ctx, &arg, params); 393 if (rc) 394 goto out; 395 396 if (put_user(arg.ret, &uarg->ret) || 397 put_user(arg.ret_origin, &uarg->ret_origin)) { 398 rc = -EFAULT; 399 goto out; 400 } 401 rc = params_to_user(uparams, arg.num_params, params); 402 out: 403 if (params) { 404 /* Decrease ref count for all valid shared memory pointers */ 405 for (n = 0; n < arg.num_params; n++) 406 if (tee_param_is_memref(params + n) && 407 params[n].u.memref.shm) 408 tee_shm_put(params[n].u.memref.shm); 409 kfree(params); 410 } 411 return rc; 412 } 413 414 static int tee_ioctl_cancel(struct tee_context *ctx, 415 struct tee_ioctl_cancel_arg __user *uarg) 416 { 417 struct tee_ioctl_cancel_arg arg; 418 419 if (!ctx->teedev->desc->ops->cancel_req) 420 return -EINVAL; 421 422 if (copy_from_user(&arg, uarg, sizeof(arg))) 423 return -EFAULT; 424 425 return ctx->teedev->desc->ops->cancel_req(ctx, arg.cancel_id, 426 arg.session); 427 } 428 429 static int 430 tee_ioctl_close_session(struct tee_context *ctx, 431 struct tee_ioctl_close_session_arg __user *uarg) 432 { 433 struct tee_ioctl_close_session_arg arg; 434 435 if (!ctx->teedev->desc->ops->close_session) 436 return -EINVAL; 437 438 if (copy_from_user(&arg, uarg, sizeof(arg))) 439 return -EFAULT; 440 441 return ctx->teedev->desc->ops->close_session(ctx, arg.session); 442 } 443 444 static int params_to_supp(struct tee_context *ctx, 445 struct tee_ioctl_param __user *uparams, 446 size_t num_params, struct tee_param *params) 447 { 448 size_t n; 449 450 for (n = 0; n < num_params; n++) { 451 struct tee_ioctl_param ip; 452 struct tee_param *p = params + n; 453 454 ip.attr = p->attr; 455 switch (p->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) { 456 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT: 457 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT: 458 ip.a = p->u.value.a; 459 ip.b = p->u.value.b; 460 ip.c = p->u.value.c; 461 break; 462 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT: 463 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: 464 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: 465 ip.b = p->u.memref.size; 466 if (!p->u.memref.shm) { 467 ip.a = 0; 468 ip.c = (u64)-1; /* invalid shm id */ 469 break; 470 } 471 ip.a = p->u.memref.shm_offs; 472 ip.c = p->u.memref.shm->id; 473 break; 474 default: 475 ip.a = 0; 476 ip.b = 0; 477 ip.c = 0; 478 break; 479 } 480 481 if (copy_to_user(uparams + n, &ip, sizeof(ip))) 482 return -EFAULT; 483 } 484 485 return 0; 486 } 487 488 static int tee_ioctl_supp_recv(struct tee_context *ctx, 489 struct tee_ioctl_buf_data __user *ubuf) 490 { 491 int rc; 492 struct tee_ioctl_buf_data buf; 493 struct tee_iocl_supp_recv_arg __user *uarg; 494 struct tee_param *params; 495 u32 num_params; 496 u32 func; 497 498 if (!ctx->teedev->desc->ops->supp_recv) 499 return -EINVAL; 500 501 if (copy_from_user(&buf, ubuf, sizeof(buf))) 502 return -EFAULT; 503 504 if (buf.buf_len > TEE_MAX_ARG_SIZE || 505 buf.buf_len < sizeof(struct tee_iocl_supp_recv_arg)) 506 return -EINVAL; 507 508 uarg = u64_to_user_ptr(buf.buf_ptr); 509 if (get_user(num_params, &uarg->num_params)) 510 return -EFAULT; 511 512 if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) != buf.buf_len) 513 return -EINVAL; 514 515 params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL); 516 if (!params) 517 return -ENOMEM; 518 519 rc = params_from_user(ctx, params, num_params, uarg->params); 520 if (rc) 521 goto out; 522 523 rc = ctx->teedev->desc->ops->supp_recv(ctx, &func, &num_params, params); 524 if (rc) 525 goto out; 526 527 if (put_user(func, &uarg->func) || 528 put_user(num_params, &uarg->num_params)) { 529 rc = -EFAULT; 530 goto out; 531 } 532 533 rc = params_to_supp(ctx, uarg->params, num_params, params); 534 out: 535 kfree(params); 536 return rc; 537 } 538 539 static int params_from_supp(struct tee_param *params, size_t num_params, 540 struct tee_ioctl_param __user *uparams) 541 { 542 size_t n; 543 544 for (n = 0; n < num_params; n++) { 545 struct tee_param *p = params + n; 546 struct tee_ioctl_param ip; 547 548 if (copy_from_user(&ip, uparams + n, sizeof(ip))) 549 return -EFAULT; 550 551 /* All unused attribute bits has to be zero */ 552 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK) 553 return -EINVAL; 554 555 p->attr = ip.attr; 556 switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) { 557 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT: 558 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT: 559 /* Only out and in/out values can be updated */ 560 p->u.value.a = ip.a; 561 p->u.value.b = ip.b; 562 p->u.value.c = ip.c; 563 break; 564 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: 565 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: 566 /* 567 * Only the size of the memref can be updated. 568 * Since we don't have access to the original 569 * parameters here, only store the supplied size. 570 * The driver will copy the updated size into the 571 * original parameters. 572 */ 573 p->u.memref.shm = NULL; 574 p->u.memref.shm_offs = 0; 575 p->u.memref.size = ip.b; 576 break; 577 default: 578 memset(&p->u, 0, sizeof(p->u)); 579 break; 580 } 581 } 582 return 0; 583 } 584 585 static int tee_ioctl_supp_send(struct tee_context *ctx, 586 struct tee_ioctl_buf_data __user *ubuf) 587 { 588 long rc; 589 struct tee_ioctl_buf_data buf; 590 struct tee_iocl_supp_send_arg __user *uarg; 591 struct tee_param *params; 592 u32 num_params; 593 u32 ret; 594 595 /* Not valid for this driver */ 596 if (!ctx->teedev->desc->ops->supp_send) 597 return -EINVAL; 598 599 if (copy_from_user(&buf, ubuf, sizeof(buf))) 600 return -EFAULT; 601 602 if (buf.buf_len > TEE_MAX_ARG_SIZE || 603 buf.buf_len < sizeof(struct tee_iocl_supp_send_arg)) 604 return -EINVAL; 605 606 uarg = u64_to_user_ptr(buf.buf_ptr); 607 if (get_user(ret, &uarg->ret) || 608 get_user(num_params, &uarg->num_params)) 609 return -EFAULT; 610 611 if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) > buf.buf_len) 612 return -EINVAL; 613 614 params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL); 615 if (!params) 616 return -ENOMEM; 617 618 rc = params_from_supp(params, num_params, uarg->params); 619 if (rc) 620 goto out; 621 622 rc = ctx->teedev->desc->ops->supp_send(ctx, ret, num_params, params); 623 out: 624 kfree(params); 625 return rc; 626 } 627 628 static long tee_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 629 { 630 struct tee_context *ctx = filp->private_data; 631 void __user *uarg = (void __user *)arg; 632 633 switch (cmd) { 634 case TEE_IOC_VERSION: 635 return tee_ioctl_version(ctx, uarg); 636 case TEE_IOC_SHM_ALLOC: 637 return tee_ioctl_shm_alloc(ctx, uarg); 638 case TEE_IOC_SHM_REGISTER: 639 return tee_ioctl_shm_register(ctx, uarg); 640 case TEE_IOC_OPEN_SESSION: 641 return tee_ioctl_open_session(ctx, uarg); 642 case TEE_IOC_INVOKE: 643 return tee_ioctl_invoke(ctx, uarg); 644 case TEE_IOC_CANCEL: 645 return tee_ioctl_cancel(ctx, uarg); 646 case TEE_IOC_CLOSE_SESSION: 647 return tee_ioctl_close_session(ctx, uarg); 648 case TEE_IOC_SUPPL_RECV: 649 return tee_ioctl_supp_recv(ctx, uarg); 650 case TEE_IOC_SUPPL_SEND: 651 return tee_ioctl_supp_send(ctx, uarg); 652 default: 653 return -EINVAL; 654 } 655 } 656 657 static const struct file_operations tee_fops = { 658 .owner = THIS_MODULE, 659 .open = tee_open, 660 .release = tee_release, 661 .unlocked_ioctl = tee_ioctl, 662 .compat_ioctl = tee_ioctl, 663 }; 664 665 static void tee_release_device(struct device *dev) 666 { 667 struct tee_device *teedev = container_of(dev, struct tee_device, dev); 668 669 spin_lock(&driver_lock); 670 clear_bit(teedev->id, dev_mask); 671 spin_unlock(&driver_lock); 672 mutex_destroy(&teedev->mutex); 673 idr_destroy(&teedev->idr); 674 kfree(teedev); 675 } 676 677 /** 678 * tee_device_alloc() - Allocate a new struct tee_device instance 679 * @teedesc: Descriptor for this driver 680 * @dev: Parent device for this device 681 * @pool: Shared memory pool, NULL if not used 682 * @driver_data: Private driver data for this device 683 * 684 * Allocates a new struct tee_device instance. The device is 685 * removed by tee_device_unregister(). 686 * 687 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure 688 */ 689 struct tee_device *tee_device_alloc(const struct tee_desc *teedesc, 690 struct device *dev, 691 struct tee_shm_pool *pool, 692 void *driver_data) 693 { 694 struct tee_device *teedev; 695 void *ret; 696 int rc; 697 int offs = 0; 698 699 if (!teedesc || !teedesc->name || !teedesc->ops || 700 !teedesc->ops->get_version || !teedesc->ops->open || 701 !teedesc->ops->release || !pool) 702 return ERR_PTR(-EINVAL); 703 704 teedev = kzalloc(sizeof(*teedev), GFP_KERNEL); 705 if (!teedev) { 706 ret = ERR_PTR(-ENOMEM); 707 goto err; 708 } 709 710 if (teedesc->flags & TEE_DESC_PRIVILEGED) 711 offs = TEE_NUM_DEVICES / 2; 712 713 spin_lock(&driver_lock); 714 teedev->id = find_next_zero_bit(dev_mask, TEE_NUM_DEVICES, offs); 715 if (teedev->id < TEE_NUM_DEVICES) 716 set_bit(teedev->id, dev_mask); 717 spin_unlock(&driver_lock); 718 719 if (teedev->id >= TEE_NUM_DEVICES) { 720 ret = ERR_PTR(-ENOMEM); 721 goto err; 722 } 723 724 snprintf(teedev->name, sizeof(teedev->name), "tee%s%d", 725 teedesc->flags & TEE_DESC_PRIVILEGED ? "priv" : "", 726 teedev->id - offs); 727 728 teedev->dev.class = tee_class; 729 teedev->dev.release = tee_release_device; 730 teedev->dev.parent = dev; 731 732 teedev->dev.devt = MKDEV(MAJOR(tee_devt), teedev->id); 733 734 rc = dev_set_name(&teedev->dev, "%s", teedev->name); 735 if (rc) { 736 ret = ERR_PTR(rc); 737 goto err_devt; 738 } 739 740 cdev_init(&teedev->cdev, &tee_fops); 741 teedev->cdev.owner = teedesc->owner; 742 teedev->cdev.kobj.parent = &teedev->dev.kobj; 743 744 dev_set_drvdata(&teedev->dev, driver_data); 745 device_initialize(&teedev->dev); 746 747 /* 1 as tee_device_unregister() does one final tee_device_put() */ 748 teedev->num_users = 1; 749 init_completion(&teedev->c_no_users); 750 mutex_init(&teedev->mutex); 751 idr_init(&teedev->idr); 752 753 teedev->desc = teedesc; 754 teedev->pool = pool; 755 756 return teedev; 757 err_devt: 758 unregister_chrdev_region(teedev->dev.devt, 1); 759 err: 760 pr_err("could not register %s driver\n", 761 teedesc->flags & TEE_DESC_PRIVILEGED ? "privileged" : "client"); 762 if (teedev && teedev->id < TEE_NUM_DEVICES) { 763 spin_lock(&driver_lock); 764 clear_bit(teedev->id, dev_mask); 765 spin_unlock(&driver_lock); 766 } 767 kfree(teedev); 768 return ret; 769 } 770 EXPORT_SYMBOL_GPL(tee_device_alloc); 771 772 static ssize_t implementation_id_show(struct device *dev, 773 struct device_attribute *attr, char *buf) 774 { 775 struct tee_device *teedev = container_of(dev, struct tee_device, dev); 776 struct tee_ioctl_version_data vers; 777 778 teedev->desc->ops->get_version(teedev, &vers); 779 return scnprintf(buf, PAGE_SIZE, "%d\n", vers.impl_id); 780 } 781 static DEVICE_ATTR_RO(implementation_id); 782 783 static struct attribute *tee_dev_attrs[] = { 784 &dev_attr_implementation_id.attr, 785 NULL 786 }; 787 788 static const struct attribute_group tee_dev_group = { 789 .attrs = tee_dev_attrs, 790 }; 791 792 /** 793 * tee_device_register() - Registers a TEE device 794 * @teedev: Device to register 795 * 796 * tee_device_unregister() need to be called to remove the @teedev if 797 * this function fails. 798 * 799 * @returns < 0 on failure 800 */ 801 int tee_device_register(struct tee_device *teedev) 802 { 803 int rc; 804 805 if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) { 806 dev_err(&teedev->dev, "attempt to register twice\n"); 807 return -EINVAL; 808 } 809 810 rc = cdev_add(&teedev->cdev, teedev->dev.devt, 1); 811 if (rc) { 812 dev_err(&teedev->dev, 813 "unable to cdev_add() %s, major %d, minor %d, err=%d\n", 814 teedev->name, MAJOR(teedev->dev.devt), 815 MINOR(teedev->dev.devt), rc); 816 return rc; 817 } 818 819 rc = device_add(&teedev->dev); 820 if (rc) { 821 dev_err(&teedev->dev, 822 "unable to device_add() %s, major %d, minor %d, err=%d\n", 823 teedev->name, MAJOR(teedev->dev.devt), 824 MINOR(teedev->dev.devt), rc); 825 goto err_device_add; 826 } 827 828 rc = sysfs_create_group(&teedev->dev.kobj, &tee_dev_group); 829 if (rc) { 830 dev_err(&teedev->dev, 831 "failed to create sysfs attributes, err=%d\n", rc); 832 goto err_sysfs_create_group; 833 } 834 835 teedev->flags |= TEE_DEVICE_FLAG_REGISTERED; 836 return 0; 837 838 err_sysfs_create_group: 839 device_del(&teedev->dev); 840 err_device_add: 841 cdev_del(&teedev->cdev); 842 return rc; 843 } 844 EXPORT_SYMBOL_GPL(tee_device_register); 845 846 void tee_device_put(struct tee_device *teedev) 847 { 848 mutex_lock(&teedev->mutex); 849 /* Shouldn't put in this state */ 850 if (!WARN_ON(!teedev->desc)) { 851 teedev->num_users--; 852 if (!teedev->num_users) { 853 teedev->desc = NULL; 854 complete(&teedev->c_no_users); 855 } 856 } 857 mutex_unlock(&teedev->mutex); 858 } 859 860 bool tee_device_get(struct tee_device *teedev) 861 { 862 mutex_lock(&teedev->mutex); 863 if (!teedev->desc) { 864 mutex_unlock(&teedev->mutex); 865 return false; 866 } 867 teedev->num_users++; 868 mutex_unlock(&teedev->mutex); 869 return true; 870 } 871 872 /** 873 * tee_device_unregister() - Removes a TEE device 874 * @teedev: Device to unregister 875 * 876 * This function should be called to remove the @teedev even if 877 * tee_device_register() hasn't been called yet. Does nothing if 878 * @teedev is NULL. 879 */ 880 void tee_device_unregister(struct tee_device *teedev) 881 { 882 if (!teedev) 883 return; 884 885 if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) { 886 sysfs_remove_group(&teedev->dev.kobj, &tee_dev_group); 887 cdev_del(&teedev->cdev); 888 device_del(&teedev->dev); 889 } 890 891 tee_device_put(teedev); 892 wait_for_completion(&teedev->c_no_users); 893 894 /* 895 * No need to take a mutex any longer now since teedev->desc was 896 * set to NULL before teedev->c_no_users was completed. 897 */ 898 899 teedev->pool = NULL; 900 901 put_device(&teedev->dev); 902 } 903 EXPORT_SYMBOL_GPL(tee_device_unregister); 904 905 /** 906 * tee_get_drvdata() - Return driver_data pointer 907 * @teedev: Device containing the driver_data pointer 908 * @returns the driver_data pointer supplied to tee_register(). 909 */ 910 void *tee_get_drvdata(struct tee_device *teedev) 911 { 912 return dev_get_drvdata(&teedev->dev); 913 } 914 EXPORT_SYMBOL_GPL(tee_get_drvdata); 915 916 static int __init tee_init(void) 917 { 918 int rc; 919 920 tee_class = class_create(THIS_MODULE, "tee"); 921 if (IS_ERR(tee_class)) { 922 pr_err("couldn't create class\n"); 923 return PTR_ERR(tee_class); 924 } 925 926 rc = alloc_chrdev_region(&tee_devt, 0, TEE_NUM_DEVICES, "tee"); 927 if (rc) { 928 pr_err("failed to allocate char dev region\n"); 929 class_destroy(tee_class); 930 tee_class = NULL; 931 } 932 933 return rc; 934 } 935 936 static void __exit tee_exit(void) 937 { 938 class_destroy(tee_class); 939 tee_class = NULL; 940 unregister_chrdev_region(tee_devt, TEE_NUM_DEVICES); 941 } 942 943 subsys_initcall(tee_init); 944 module_exit(tee_exit); 945 946 MODULE_AUTHOR("Linaro"); 947 MODULE_DESCRIPTION("TEE Driver"); 948 MODULE_VERSION("1.0"); 949 MODULE_LICENSE("GPL v2"); 950