Linux 4.16.11
[linux/fpc-iii.git] / drivers / tee / tee_core.c
blob6c4b200a45604827517ff1b840ba642ab2beb165
1 /*
2 * Copyright (c) 2015-2016, Linaro Limited
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.
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.
15 #define pr_fmt(fmt) "%s: " fmt, __func__
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"
27 #define TEE_NUM_DEVICES 32
29 #define TEE_IOCTL_PARAM_SIZE(x) (sizeof(struct tee_param) * (x))
32 * Unprivileged devices in the lower half range and privileged devices in
33 * the upper half range.
35 static DECLARE_BITMAP(dev_mask, TEE_NUM_DEVICES);
36 static DEFINE_SPINLOCK(driver_lock);
38 static struct class *tee_class;
39 static dev_t tee_devt;
41 static int tee_open(struct inode *inode, struct file *filp)
43 int rc;
44 struct tee_device *teedev;
45 struct tee_context *ctx;
47 teedev = container_of(inode->i_cdev, struct tee_device, cdev);
48 if (!tee_device_get(teedev))
49 return -EINVAL;
51 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
52 if (!ctx) {
53 rc = -ENOMEM;
54 goto err;
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;
65 return 0;
66 err:
67 kfree(ctx);
68 tee_device_put(teedev);
69 return rc;
72 void teedev_ctx_get(struct tee_context *ctx)
74 if (ctx->releasing)
75 return;
77 kref_get(&ctx->refcount);
80 static void teedev_ctx_release(struct kref *ref)
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);
89 void teedev_ctx_put(struct tee_context *ctx)
91 if (ctx->releasing)
92 return;
94 kref_put(&ctx->refcount, teedev_ctx_release);
97 static void teedev_close_context(struct tee_context *ctx)
99 tee_device_put(ctx->teedev);
100 teedev_ctx_put(ctx);
103 static int tee_release(struct inode *inode, struct file *filp)
105 teedev_close_context(filp->private_data);
106 return 0;
109 static int tee_ioctl_version(struct tee_context *ctx,
110 struct tee_ioctl_version_data __user *uvers)
112 struct tee_ioctl_version_data vers;
114 ctx->teedev->desc->ops->get_version(ctx->teedev, &vers);
116 if (ctx->teedev->desc->flags & TEE_DESC_PRIVILEGED)
117 vers.gen_caps |= TEE_GEN_CAP_PRIVILEGED;
119 if (copy_to_user(uvers, &vers, sizeof(vers)))
120 return -EFAULT;
122 return 0;
125 static int tee_ioctl_shm_alloc(struct tee_context *ctx,
126 struct tee_ioctl_shm_alloc_data __user *udata)
128 long ret;
129 struct tee_ioctl_shm_alloc_data data;
130 struct tee_shm *shm;
132 if (copy_from_user(&data, udata, sizeof(data)))
133 return -EFAULT;
135 /* Currently no input flags are supported */
136 if (data.flags)
137 return -EINVAL;
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);
143 data.id = shm->id;
144 data.flags = shm->flags;
145 data.size = shm->size;
147 if (copy_to_user(udata, &data, sizeof(data)))
148 ret = -EFAULT;
149 else
150 ret = tee_shm_get_fd(shm);
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.
157 tee_shm_put(shm);
158 return ret;
161 static int
162 tee_ioctl_shm_register(struct tee_context *ctx,
163 struct tee_ioctl_shm_register_data __user *udata)
165 long ret;
166 struct tee_ioctl_shm_register_data data;
167 struct tee_shm *shm;
169 if (copy_from_user(&data, udata, sizeof(data)))
170 return -EFAULT;
172 /* Currently no input flags are supported */
173 if (data.flags)
174 return -EINVAL;
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);
181 data.id = shm->id;
182 data.flags = shm->flags;
183 data.length = shm->size;
185 if (copy_to_user(udata, &data, sizeof(data)))
186 ret = -EFAULT;
187 else
188 ret = tee_shm_get_fd(shm);
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.
194 tee_shm_put(shm);
195 return ret;
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)
202 size_t n;
204 for (n = 0; n < num_params; n++) {
205 struct tee_shm *shm;
206 struct tee_ioctl_param ip;
208 if (copy_from_user(&ip, uparams + n, sizeof(ip)))
209 return -EFAULT;
211 /* All unused attribute bits has to be zero */
212 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
213 return -EINVAL;
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:
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.
237 shm = tee_shm_get_from_id(ctx, ip.c);
238 if (IS_ERR(shm))
239 return PTR_ERR(shm);
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;
250 return 0;
253 static int params_to_user(struct tee_ioctl_param __user *uparams,
254 size_t num_params, struct tee_param *params)
256 size_t n;
258 for (n = 0; n < num_params; n++) {
259 struct tee_ioctl_param __user *up = uparams + n;
260 struct tee_param *p = params + n;
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;
278 return 0;
281 static int tee_ioctl_open_session(struct tee_context *ctx,
282 struct tee_ioctl_buf_data __user *ubuf)
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;
293 if (!ctx->teedev->desc->ops->open_session)
294 return -EINVAL;
296 if (copy_from_user(&buf, ubuf, sizeof(buf)))
297 return -EFAULT;
299 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
300 buf.buf_len < sizeof(struct tee_ioctl_open_session_arg))
301 return -EINVAL;
303 uarg = u64_to_user_ptr(buf.buf_ptr);
304 if (copy_from_user(&arg, uarg, sizeof(arg)))
305 return -EFAULT;
307 if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
308 return -EINVAL;
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;
321 rc = ctx->teedev->desc->ops->open_session(ctx, &arg, params);
322 if (rc)
323 goto out;
324 have_session = true;
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;
332 rc = params_to_user(uparams, arg.num_params, params);
333 out:
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.
338 if (rc && have_session && ctx->teedev->desc->ops->close_session)
339 ctx->teedev->desc->ops->close_session(ctx, arg.session);
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);
350 return rc;
353 static int tee_ioctl_invoke(struct tee_context *ctx,
354 struct tee_ioctl_buf_data __user *ubuf)
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;
364 if (!ctx->teedev->desc->ops->invoke_func)
365 return -EINVAL;
367 if (copy_from_user(&buf, ubuf, sizeof(buf)))
368 return -EFAULT;
370 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
371 buf.buf_len < sizeof(struct tee_ioctl_invoke_arg))
372 return -EINVAL;
374 uarg = u64_to_user_ptr(buf.buf_ptr);
375 if (copy_from_user(&arg, uarg, sizeof(arg)))
376 return -EFAULT;
378 if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
379 return -EINVAL;
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;
392 rc = ctx->teedev->desc->ops->invoke_func(ctx, &arg, params);
393 if (rc)
394 goto out;
396 if (put_user(arg.ret, &uarg->ret) ||
397 put_user(arg.ret_origin, &uarg->ret_origin)) {
398 rc = -EFAULT;
399 goto out;
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);
411 return rc;
414 static int tee_ioctl_cancel(struct tee_context *ctx,
415 struct tee_ioctl_cancel_arg __user *uarg)
417 struct tee_ioctl_cancel_arg arg;
419 if (!ctx->teedev->desc->ops->cancel_req)
420 return -EINVAL;
422 if (copy_from_user(&arg, uarg, sizeof(arg)))
423 return -EFAULT;
425 return ctx->teedev->desc->ops->cancel_req(ctx, arg.cancel_id,
426 arg.session);
429 static int
430 tee_ioctl_close_session(struct tee_context *ctx,
431 struct tee_ioctl_close_session_arg __user *uarg)
433 struct tee_ioctl_close_session_arg arg;
435 if (!ctx->teedev->desc->ops->close_session)
436 return -EINVAL;
438 if (copy_from_user(&arg, uarg, sizeof(arg)))
439 return -EFAULT;
441 return ctx->teedev->desc->ops->close_session(ctx, arg.session);
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)
448 size_t n;
450 for (n = 0; n < num_params; n++) {
451 struct tee_ioctl_param ip;
452 struct tee_param *p = params + n;
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;
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;
481 if (copy_to_user(uparams + n, &ip, sizeof(ip)))
482 return -EFAULT;
485 return 0;
488 static int tee_ioctl_supp_recv(struct tee_context *ctx,
489 struct tee_ioctl_buf_data __user *ubuf)
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;
498 if (!ctx->teedev->desc->ops->supp_recv)
499 return -EINVAL;
501 if (copy_from_user(&buf, ubuf, sizeof(buf)))
502 return -EFAULT;
504 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
505 buf.buf_len < sizeof(struct tee_iocl_supp_recv_arg))
506 return -EINVAL;
508 uarg = u64_to_user_ptr(buf.buf_ptr);
509 if (get_user(num_params, &uarg->num_params))
510 return -EFAULT;
512 if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) != buf.buf_len)
513 return -EINVAL;
515 params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
516 if (!params)
517 return -ENOMEM;
519 rc = params_from_user(ctx, params, num_params, uarg->params);
520 if (rc)
521 goto out;
523 rc = ctx->teedev->desc->ops->supp_recv(ctx, &func, &num_params, params);
524 if (rc)
525 goto out;
527 if (put_user(func, &uarg->func) ||
528 put_user(num_params, &uarg->num_params)) {
529 rc = -EFAULT;
530 goto out;
533 rc = params_to_supp(ctx, uarg->params, num_params, params);
534 out:
535 kfree(params);
536 return rc;
539 static int params_from_supp(struct tee_param *params, size_t num_params,
540 struct tee_ioctl_param __user *uparams)
542 size_t n;
544 for (n = 0; n < num_params; n++) {
545 struct tee_param *p = params + n;
546 struct tee_ioctl_param ip;
548 if (copy_from_user(&ip, uparams + n, sizeof(ip)))
549 return -EFAULT;
551 /* All unused attribute bits has to be zero */
552 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
553 return -EINVAL;
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:
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.
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;
582 return 0;
585 static int tee_ioctl_supp_send(struct tee_context *ctx,
586 struct tee_ioctl_buf_data __user *ubuf)
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;
595 /* Not valid for this driver */
596 if (!ctx->teedev->desc->ops->supp_send)
597 return -EINVAL;
599 if (copy_from_user(&buf, ubuf, sizeof(buf)))
600 return -EFAULT;
602 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
603 buf.buf_len < sizeof(struct tee_iocl_supp_send_arg))
604 return -EINVAL;
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;
611 if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) > buf.buf_len)
612 return -EINVAL;
614 params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
615 if (!params)
616 return -ENOMEM;
618 rc = params_from_supp(params, num_params, uarg->params);
619 if (rc)
620 goto out;
622 rc = ctx->teedev->desc->ops->supp_send(ctx, ret, num_params, params);
623 out:
624 kfree(params);
625 return rc;
628 static long tee_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
630 struct tee_context *ctx = filp->private_data;
631 void __user *uarg = (void __user *)arg;
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;
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,
665 static void tee_release_device(struct device *dev)
667 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
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);
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
684 * Allocates a new struct tee_device instance. The device is
685 * removed by tee_device_unregister().
687 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
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)
694 struct tee_device *teedev;
695 void *ret;
696 int rc;
697 int offs = 0;
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);
704 teedev = kzalloc(sizeof(*teedev), GFP_KERNEL);
705 if (!teedev) {
706 ret = ERR_PTR(-ENOMEM);
707 goto err;
710 if (teedesc->flags & TEE_DESC_PRIVILEGED)
711 offs = TEE_NUM_DEVICES / 2;
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);
719 if (teedev->id >= TEE_NUM_DEVICES) {
720 ret = ERR_PTR(-ENOMEM);
721 goto err;
724 snprintf(teedev->name, sizeof(teedev->name), "tee%s%d",
725 teedesc->flags & TEE_DESC_PRIVILEGED ? "priv" : "",
726 teedev->id - offs);
728 teedev->dev.class = tee_class;
729 teedev->dev.release = tee_release_device;
730 teedev->dev.parent = dev;
732 teedev->dev.devt = MKDEV(MAJOR(tee_devt), teedev->id);
734 rc = dev_set_name(&teedev->dev, "%s", teedev->name);
735 if (rc) {
736 ret = ERR_PTR(rc);
737 goto err_devt;
740 cdev_init(&teedev->cdev, &tee_fops);
741 teedev->cdev.owner = teedesc->owner;
742 teedev->cdev.kobj.parent = &teedev->dev.kobj;
744 dev_set_drvdata(&teedev->dev, driver_data);
745 device_initialize(&teedev->dev);
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);
753 teedev->desc = teedesc;
754 teedev->pool = pool;
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);
767 kfree(teedev);
768 return ret;
770 EXPORT_SYMBOL_GPL(tee_device_alloc);
772 static ssize_t implementation_id_show(struct device *dev,
773 struct device_attribute *attr, char *buf)
775 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
776 struct tee_ioctl_version_data vers;
778 teedev->desc->ops->get_version(teedev, &vers);
779 return scnprintf(buf, PAGE_SIZE, "%d\n", vers.impl_id);
781 static DEVICE_ATTR_RO(implementation_id);
783 static struct attribute *tee_dev_attrs[] = {
784 &dev_attr_implementation_id.attr,
785 NULL
788 static const struct attribute_group tee_dev_group = {
789 .attrs = tee_dev_attrs,
793 * tee_device_register() - Registers a TEE device
794 * @teedev: Device to register
796 * tee_device_unregister() need to be called to remove the @teedev if
797 * this function fails.
799 * @returns < 0 on failure
801 int tee_device_register(struct tee_device *teedev)
803 int rc;
805 if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
806 dev_err(&teedev->dev, "attempt to register twice\n");
807 return -EINVAL;
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;
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;
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;
835 teedev->flags |= TEE_DEVICE_FLAG_REGISTERED;
836 return 0;
838 err_sysfs_create_group:
839 device_del(&teedev->dev);
840 err_device_add:
841 cdev_del(&teedev->cdev);
842 return rc;
844 EXPORT_SYMBOL_GPL(tee_device_register);
846 void tee_device_put(struct tee_device *teedev)
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);
857 mutex_unlock(&teedev->mutex);
860 bool tee_device_get(struct tee_device *teedev)
862 mutex_lock(&teedev->mutex);
863 if (!teedev->desc) {
864 mutex_unlock(&teedev->mutex);
865 return false;
867 teedev->num_users++;
868 mutex_unlock(&teedev->mutex);
869 return true;
873 * tee_device_unregister() - Removes a TEE device
874 * @teedev: Device to unregister
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.
880 void tee_device_unregister(struct tee_device *teedev)
882 if (!teedev)
883 return;
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);
891 tee_device_put(teedev);
892 wait_for_completion(&teedev->c_no_users);
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.
899 teedev->pool = NULL;
901 put_device(&teedev->dev);
903 EXPORT_SYMBOL_GPL(tee_device_unregister);
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().
910 void *tee_get_drvdata(struct tee_device *teedev)
912 return dev_get_drvdata(&teedev->dev);
914 EXPORT_SYMBOL_GPL(tee_get_drvdata);
916 static int __init tee_init(void)
918 int rc;
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);
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;
933 return rc;
936 static void __exit tee_exit(void)
938 class_destroy(tee_class);
939 tee_class = NULL;
940 unregister_chrdev_region(tee_devt, TEE_NUM_DEVICES);
943 subsys_initcall(tee_init);
944 module_exit(tee_exit);
946 MODULE_AUTHOR("Linaro");
947 MODULE_DESCRIPTION("TEE Driver");
948 MODULE_VERSION("1.0");
949 MODULE_LICENSE("GPL v2");