1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015-2016, Linaro Limited
6 #define pr_fmt(fmt) "%s: " fmt, __func__
8 #include <linux/cdev.h>
10 #include <linux/idr.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/tee_drv.h>
14 #include <linux/uaccess.h>
15 #include "tee_private.h"
17 #define TEE_NUM_DEVICES 32
19 #define TEE_IOCTL_PARAM_SIZE(x) (sizeof(struct tee_param) * (x))
22 * Unprivileged devices in the lower half range and privileged devices in
23 * the upper half range.
25 static DECLARE_BITMAP(dev_mask
, TEE_NUM_DEVICES
);
26 static DEFINE_SPINLOCK(driver_lock
);
28 static struct class *tee_class
;
29 static dev_t tee_devt
;
31 static struct tee_context
*teedev_open(struct tee_device
*teedev
)
34 struct tee_context
*ctx
;
36 if (!tee_device_get(teedev
))
37 return ERR_PTR(-EINVAL
);
39 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
45 kref_init(&ctx
->refcount
);
47 rc
= teedev
->desc
->ops
->open(ctx
);
54 tee_device_put(teedev
);
59 void teedev_ctx_get(struct tee_context
*ctx
)
64 kref_get(&ctx
->refcount
);
67 static void teedev_ctx_release(struct kref
*ref
)
69 struct tee_context
*ctx
= container_of(ref
, struct tee_context
,
71 ctx
->releasing
= true;
72 ctx
->teedev
->desc
->ops
->release(ctx
);
76 void teedev_ctx_put(struct tee_context
*ctx
)
81 kref_put(&ctx
->refcount
, teedev_ctx_release
);
84 static void teedev_close_context(struct tee_context
*ctx
)
86 tee_device_put(ctx
->teedev
);
90 static int tee_open(struct inode
*inode
, struct file
*filp
)
92 struct tee_context
*ctx
;
94 ctx
= teedev_open(container_of(inode
->i_cdev
, struct tee_device
, cdev
));
99 * Default user-space behaviour is to wait for tee-supplicant
100 * if not present for any requests in this context.
102 ctx
->supp_nowait
= false;
103 filp
->private_data
= ctx
;
107 static int tee_release(struct inode
*inode
, struct file
*filp
)
109 teedev_close_context(filp
->private_data
);
113 static int tee_ioctl_version(struct tee_context
*ctx
,
114 struct tee_ioctl_version_data __user
*uvers
)
116 struct tee_ioctl_version_data vers
;
118 ctx
->teedev
->desc
->ops
->get_version(ctx
->teedev
, &vers
);
120 if (ctx
->teedev
->desc
->flags
& TEE_DESC_PRIVILEGED
)
121 vers
.gen_caps
|= TEE_GEN_CAP_PRIVILEGED
;
123 if (copy_to_user(uvers
, &vers
, sizeof(vers
)))
129 static int tee_ioctl_shm_alloc(struct tee_context
*ctx
,
130 struct tee_ioctl_shm_alloc_data __user
*udata
)
133 struct tee_ioctl_shm_alloc_data data
;
136 if (copy_from_user(&data
, udata
, sizeof(data
)))
139 /* Currently no input flags are supported */
143 shm
= tee_shm_alloc(ctx
, data
.size
, TEE_SHM_MAPPED
| TEE_SHM_DMA_BUF
);
148 data
.flags
= shm
->flags
;
149 data
.size
= shm
->size
;
151 if (copy_to_user(udata
, &data
, sizeof(data
)))
154 ret
= tee_shm_get_fd(shm
);
157 * When user space closes the file descriptor the shared memory
158 * should be freed or if tee_shm_get_fd() failed then it will
159 * be freed immediately.
166 tee_ioctl_shm_register(struct tee_context
*ctx
,
167 struct tee_ioctl_shm_register_data __user
*udata
)
170 struct tee_ioctl_shm_register_data data
;
173 if (copy_from_user(&data
, udata
, sizeof(data
)))
176 /* Currently no input flags are supported */
180 shm
= tee_shm_register(ctx
, data
.addr
, data
.length
,
181 TEE_SHM_DMA_BUF
| TEE_SHM_USER_MAPPED
);
186 data
.flags
= shm
->flags
;
187 data
.length
= shm
->size
;
189 if (copy_to_user(udata
, &data
, sizeof(data
)))
192 ret
= tee_shm_get_fd(shm
);
194 * When user space closes the file descriptor the shared memory
195 * should be freed or if tee_shm_get_fd() failed then it will
196 * be freed immediately.
202 static int params_from_user(struct tee_context
*ctx
, struct tee_param
*params
,
204 struct tee_ioctl_param __user
*uparams
)
208 for (n
= 0; n
< num_params
; n
++) {
210 struct tee_ioctl_param ip
;
212 if (copy_from_user(&ip
, uparams
+ n
, sizeof(ip
)))
215 /* All unused attribute bits has to be zero */
216 if (ip
.attr
& ~TEE_IOCTL_PARAM_ATTR_MASK
)
219 params
[n
].attr
= ip
.attr
;
220 switch (ip
.attr
& TEE_IOCTL_PARAM_ATTR_TYPE_MASK
) {
221 case TEE_IOCTL_PARAM_ATTR_TYPE_NONE
:
222 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT
:
224 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT
:
225 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
226 params
[n
].u
.value
.a
= ip
.a
;
227 params
[n
].u
.value
.b
= ip
.b
;
228 params
[n
].u
.value
.c
= ip
.c
;
230 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT
:
231 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
232 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
234 * If we fail to get a pointer to a shared memory
235 * object (and increase the ref count) from an
236 * identifier we return an error. All pointers that
237 * has been added in params have an increased ref
238 * count. It's the callers responibility to do
239 * tee_shm_put() on all resolved pointers.
241 shm
= tee_shm_get_from_id(ctx
, ip
.c
);
246 * Ensure offset + size does not overflow offset
247 * and does not overflow the size of the referred
248 * shared memory object.
250 if ((ip
.a
+ ip
.b
) < ip
.a
||
251 (ip
.a
+ ip
.b
) > shm
->size
) {
256 params
[n
].u
.memref
.shm_offs
= ip
.a
;
257 params
[n
].u
.memref
.size
= ip
.b
;
258 params
[n
].u
.memref
.shm
= shm
;
261 /* Unknown attribute */
268 static int params_to_user(struct tee_ioctl_param __user
*uparams
,
269 size_t num_params
, struct tee_param
*params
)
273 for (n
= 0; n
< num_params
; n
++) {
274 struct tee_ioctl_param __user
*up
= uparams
+ n
;
275 struct tee_param
*p
= params
+ n
;
278 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT
:
279 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
280 if (put_user(p
->u
.value
.a
, &up
->a
) ||
281 put_user(p
->u
.value
.b
, &up
->b
) ||
282 put_user(p
->u
.value
.c
, &up
->c
))
285 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
286 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
287 if (put_user((u64
)p
->u
.memref
.size
, &up
->b
))
296 static int tee_ioctl_open_session(struct tee_context
*ctx
,
297 struct tee_ioctl_buf_data __user
*ubuf
)
301 struct tee_ioctl_buf_data buf
;
302 struct tee_ioctl_open_session_arg __user
*uarg
;
303 struct tee_ioctl_open_session_arg arg
;
304 struct tee_ioctl_param __user
*uparams
= NULL
;
305 struct tee_param
*params
= NULL
;
306 bool have_session
= false;
308 if (!ctx
->teedev
->desc
->ops
->open_session
)
311 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
314 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
315 buf
.buf_len
< sizeof(struct tee_ioctl_open_session_arg
))
318 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
319 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
322 if (sizeof(arg
) + TEE_IOCTL_PARAM_SIZE(arg
.num_params
) != buf
.buf_len
)
325 if (arg
.num_params
) {
326 params
= kcalloc(arg
.num_params
, sizeof(struct tee_param
),
330 uparams
= uarg
->params
;
331 rc
= params_from_user(ctx
, params
, arg
.num_params
, uparams
);
336 rc
= ctx
->teedev
->desc
->ops
->open_session(ctx
, &arg
, params
);
341 if (put_user(arg
.session
, &uarg
->session
) ||
342 put_user(arg
.ret
, &uarg
->ret
) ||
343 put_user(arg
.ret_origin
, &uarg
->ret_origin
)) {
347 rc
= params_to_user(uparams
, arg
.num_params
, params
);
350 * If we've succeeded to open the session but failed to communicate
351 * it back to user space, close the session again to avoid leakage.
353 if (rc
&& have_session
&& ctx
->teedev
->desc
->ops
->close_session
)
354 ctx
->teedev
->desc
->ops
->close_session(ctx
, arg
.session
);
357 /* Decrease ref count for all valid shared memory pointers */
358 for (n
= 0; n
< arg
.num_params
; n
++)
359 if (tee_param_is_memref(params
+ n
) &&
360 params
[n
].u
.memref
.shm
)
361 tee_shm_put(params
[n
].u
.memref
.shm
);
368 static int tee_ioctl_invoke(struct tee_context
*ctx
,
369 struct tee_ioctl_buf_data __user
*ubuf
)
373 struct tee_ioctl_buf_data buf
;
374 struct tee_ioctl_invoke_arg __user
*uarg
;
375 struct tee_ioctl_invoke_arg arg
;
376 struct tee_ioctl_param __user
*uparams
= NULL
;
377 struct tee_param
*params
= NULL
;
379 if (!ctx
->teedev
->desc
->ops
->invoke_func
)
382 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
385 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
386 buf
.buf_len
< sizeof(struct tee_ioctl_invoke_arg
))
389 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
390 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
393 if (sizeof(arg
) + TEE_IOCTL_PARAM_SIZE(arg
.num_params
) != buf
.buf_len
)
396 if (arg
.num_params
) {
397 params
= kcalloc(arg
.num_params
, sizeof(struct tee_param
),
401 uparams
= uarg
->params
;
402 rc
= params_from_user(ctx
, params
, arg
.num_params
, uparams
);
407 rc
= ctx
->teedev
->desc
->ops
->invoke_func(ctx
, &arg
, params
);
411 if (put_user(arg
.ret
, &uarg
->ret
) ||
412 put_user(arg
.ret_origin
, &uarg
->ret_origin
)) {
416 rc
= params_to_user(uparams
, arg
.num_params
, params
);
419 /* Decrease ref count for all valid shared memory pointers */
420 for (n
= 0; n
< arg
.num_params
; n
++)
421 if (tee_param_is_memref(params
+ n
) &&
422 params
[n
].u
.memref
.shm
)
423 tee_shm_put(params
[n
].u
.memref
.shm
);
429 static int tee_ioctl_cancel(struct tee_context
*ctx
,
430 struct tee_ioctl_cancel_arg __user
*uarg
)
432 struct tee_ioctl_cancel_arg arg
;
434 if (!ctx
->teedev
->desc
->ops
->cancel_req
)
437 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
440 return ctx
->teedev
->desc
->ops
->cancel_req(ctx
, arg
.cancel_id
,
445 tee_ioctl_close_session(struct tee_context
*ctx
,
446 struct tee_ioctl_close_session_arg __user
*uarg
)
448 struct tee_ioctl_close_session_arg arg
;
450 if (!ctx
->teedev
->desc
->ops
->close_session
)
453 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
456 return ctx
->teedev
->desc
->ops
->close_session(ctx
, arg
.session
);
459 static int params_to_supp(struct tee_context
*ctx
,
460 struct tee_ioctl_param __user
*uparams
,
461 size_t num_params
, struct tee_param
*params
)
465 for (n
= 0; n
< num_params
; n
++) {
466 struct tee_ioctl_param ip
;
467 struct tee_param
*p
= params
+ n
;
470 switch (p
->attr
& TEE_IOCTL_PARAM_ATTR_TYPE_MASK
) {
471 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT
:
472 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
477 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT
:
478 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
479 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
480 ip
.b
= p
->u
.memref
.size
;
481 if (!p
->u
.memref
.shm
) {
483 ip
.c
= (u64
)-1; /* invalid shm id */
486 ip
.a
= p
->u
.memref
.shm_offs
;
487 ip
.c
= p
->u
.memref
.shm
->id
;
496 if (copy_to_user(uparams
+ n
, &ip
, sizeof(ip
)))
503 static int tee_ioctl_supp_recv(struct tee_context
*ctx
,
504 struct tee_ioctl_buf_data __user
*ubuf
)
507 struct tee_ioctl_buf_data buf
;
508 struct tee_iocl_supp_recv_arg __user
*uarg
;
509 struct tee_param
*params
;
513 if (!ctx
->teedev
->desc
->ops
->supp_recv
)
516 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
519 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
520 buf
.buf_len
< sizeof(struct tee_iocl_supp_recv_arg
))
523 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
524 if (get_user(num_params
, &uarg
->num_params
))
527 if (sizeof(*uarg
) + TEE_IOCTL_PARAM_SIZE(num_params
) != buf
.buf_len
)
530 params
= kcalloc(num_params
, sizeof(struct tee_param
), GFP_KERNEL
);
534 rc
= params_from_user(ctx
, params
, num_params
, uarg
->params
);
538 rc
= ctx
->teedev
->desc
->ops
->supp_recv(ctx
, &func
, &num_params
, params
);
542 if (put_user(func
, &uarg
->func
) ||
543 put_user(num_params
, &uarg
->num_params
)) {
548 rc
= params_to_supp(ctx
, uarg
->params
, num_params
, params
);
554 static int params_from_supp(struct tee_param
*params
, size_t num_params
,
555 struct tee_ioctl_param __user
*uparams
)
559 for (n
= 0; n
< num_params
; n
++) {
560 struct tee_param
*p
= params
+ n
;
561 struct tee_ioctl_param ip
;
563 if (copy_from_user(&ip
, uparams
+ n
, sizeof(ip
)))
566 /* All unused attribute bits has to be zero */
567 if (ip
.attr
& ~TEE_IOCTL_PARAM_ATTR_MASK
)
571 switch (ip
.attr
& TEE_IOCTL_PARAM_ATTR_TYPE_MASK
) {
572 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT
:
573 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
574 /* Only out and in/out values can be updated */
579 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
580 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
582 * Only the size of the memref can be updated.
583 * Since we don't have access to the original
584 * parameters here, only store the supplied size.
585 * The driver will copy the updated size into the
586 * original parameters.
588 p
->u
.memref
.shm
= NULL
;
589 p
->u
.memref
.shm_offs
= 0;
590 p
->u
.memref
.size
= ip
.b
;
593 memset(&p
->u
, 0, sizeof(p
->u
));
600 static int tee_ioctl_supp_send(struct tee_context
*ctx
,
601 struct tee_ioctl_buf_data __user
*ubuf
)
604 struct tee_ioctl_buf_data buf
;
605 struct tee_iocl_supp_send_arg __user
*uarg
;
606 struct tee_param
*params
;
610 /* Not valid for this driver */
611 if (!ctx
->teedev
->desc
->ops
->supp_send
)
614 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
617 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
618 buf
.buf_len
< sizeof(struct tee_iocl_supp_send_arg
))
621 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
622 if (get_user(ret
, &uarg
->ret
) ||
623 get_user(num_params
, &uarg
->num_params
))
626 if (sizeof(*uarg
) + TEE_IOCTL_PARAM_SIZE(num_params
) > buf
.buf_len
)
629 params
= kcalloc(num_params
, sizeof(struct tee_param
), GFP_KERNEL
);
633 rc
= params_from_supp(params
, num_params
, uarg
->params
);
637 rc
= ctx
->teedev
->desc
->ops
->supp_send(ctx
, ret
, num_params
, params
);
643 static long tee_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
645 struct tee_context
*ctx
= filp
->private_data
;
646 void __user
*uarg
= (void __user
*)arg
;
649 case TEE_IOC_VERSION
:
650 return tee_ioctl_version(ctx
, uarg
);
651 case TEE_IOC_SHM_ALLOC
:
652 return tee_ioctl_shm_alloc(ctx
, uarg
);
653 case TEE_IOC_SHM_REGISTER
:
654 return tee_ioctl_shm_register(ctx
, uarg
);
655 case TEE_IOC_OPEN_SESSION
:
656 return tee_ioctl_open_session(ctx
, uarg
);
658 return tee_ioctl_invoke(ctx
, uarg
);
660 return tee_ioctl_cancel(ctx
, uarg
);
661 case TEE_IOC_CLOSE_SESSION
:
662 return tee_ioctl_close_session(ctx
, uarg
);
663 case TEE_IOC_SUPPL_RECV
:
664 return tee_ioctl_supp_recv(ctx
, uarg
);
665 case TEE_IOC_SUPPL_SEND
:
666 return tee_ioctl_supp_send(ctx
, uarg
);
672 static const struct file_operations tee_fops
= {
673 .owner
= THIS_MODULE
,
675 .release
= tee_release
,
676 .unlocked_ioctl
= tee_ioctl
,
677 .compat_ioctl
= compat_ptr_ioctl
,
680 static void tee_release_device(struct device
*dev
)
682 struct tee_device
*teedev
= container_of(dev
, struct tee_device
, dev
);
684 spin_lock(&driver_lock
);
685 clear_bit(teedev
->id
, dev_mask
);
686 spin_unlock(&driver_lock
);
687 mutex_destroy(&teedev
->mutex
);
688 idr_destroy(&teedev
->idr
);
693 * tee_device_alloc() - Allocate a new struct tee_device instance
694 * @teedesc: Descriptor for this driver
695 * @dev: Parent device for this device
696 * @pool: Shared memory pool, NULL if not used
697 * @driver_data: Private driver data for this device
699 * Allocates a new struct tee_device instance. The device is
700 * removed by tee_device_unregister().
702 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
704 struct tee_device
*tee_device_alloc(const struct tee_desc
*teedesc
,
706 struct tee_shm_pool
*pool
,
709 struct tee_device
*teedev
;
714 if (!teedesc
|| !teedesc
->name
|| !teedesc
->ops
||
715 !teedesc
->ops
->get_version
|| !teedesc
->ops
->open
||
716 !teedesc
->ops
->release
|| !pool
)
717 return ERR_PTR(-EINVAL
);
719 teedev
= kzalloc(sizeof(*teedev
), GFP_KERNEL
);
721 ret
= ERR_PTR(-ENOMEM
);
725 max_id
= TEE_NUM_DEVICES
/ 2;
727 if (teedesc
->flags
& TEE_DESC_PRIVILEGED
) {
728 offs
= TEE_NUM_DEVICES
/ 2;
729 max_id
= TEE_NUM_DEVICES
;
732 spin_lock(&driver_lock
);
733 teedev
->id
= find_next_zero_bit(dev_mask
, max_id
, offs
);
734 if (teedev
->id
< max_id
)
735 set_bit(teedev
->id
, dev_mask
);
736 spin_unlock(&driver_lock
);
738 if (teedev
->id
>= max_id
) {
739 ret
= ERR_PTR(-ENOMEM
);
743 snprintf(teedev
->name
, sizeof(teedev
->name
), "tee%s%d",
744 teedesc
->flags
& TEE_DESC_PRIVILEGED
? "priv" : "",
747 teedev
->dev
.class = tee_class
;
748 teedev
->dev
.release
= tee_release_device
;
749 teedev
->dev
.parent
= dev
;
751 teedev
->dev
.devt
= MKDEV(MAJOR(tee_devt
), teedev
->id
);
753 rc
= dev_set_name(&teedev
->dev
, "%s", teedev
->name
);
759 cdev_init(&teedev
->cdev
, &tee_fops
);
760 teedev
->cdev
.owner
= teedesc
->owner
;
761 teedev
->cdev
.kobj
.parent
= &teedev
->dev
.kobj
;
763 dev_set_drvdata(&teedev
->dev
, driver_data
);
764 device_initialize(&teedev
->dev
);
766 /* 1 as tee_device_unregister() does one final tee_device_put() */
767 teedev
->num_users
= 1;
768 init_completion(&teedev
->c_no_users
);
769 mutex_init(&teedev
->mutex
);
770 idr_init(&teedev
->idr
);
772 teedev
->desc
= teedesc
;
777 unregister_chrdev_region(teedev
->dev
.devt
, 1);
779 pr_err("could not register %s driver\n",
780 teedesc
->flags
& TEE_DESC_PRIVILEGED
? "privileged" : "client");
781 if (teedev
&& teedev
->id
< TEE_NUM_DEVICES
) {
782 spin_lock(&driver_lock
);
783 clear_bit(teedev
->id
, dev_mask
);
784 spin_unlock(&driver_lock
);
789 EXPORT_SYMBOL_GPL(tee_device_alloc
);
791 static ssize_t
implementation_id_show(struct device
*dev
,
792 struct device_attribute
*attr
, char *buf
)
794 struct tee_device
*teedev
= container_of(dev
, struct tee_device
, dev
);
795 struct tee_ioctl_version_data vers
;
797 teedev
->desc
->ops
->get_version(teedev
, &vers
);
798 return scnprintf(buf
, PAGE_SIZE
, "%d\n", vers
.impl_id
);
800 static DEVICE_ATTR_RO(implementation_id
);
802 static struct attribute
*tee_dev_attrs
[] = {
803 &dev_attr_implementation_id
.attr
,
807 static const struct attribute_group tee_dev_group
= {
808 .attrs
= tee_dev_attrs
,
812 * tee_device_register() - Registers a TEE device
813 * @teedev: Device to register
815 * tee_device_unregister() need to be called to remove the @teedev if
816 * this function fails.
818 * @returns < 0 on failure
820 int tee_device_register(struct tee_device
*teedev
)
824 if (teedev
->flags
& TEE_DEVICE_FLAG_REGISTERED
) {
825 dev_err(&teedev
->dev
, "attempt to register twice\n");
829 rc
= cdev_add(&teedev
->cdev
, teedev
->dev
.devt
, 1);
831 dev_err(&teedev
->dev
,
832 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
833 teedev
->name
, MAJOR(teedev
->dev
.devt
),
834 MINOR(teedev
->dev
.devt
), rc
);
838 rc
= device_add(&teedev
->dev
);
840 dev_err(&teedev
->dev
,
841 "unable to device_add() %s, major %d, minor %d, err=%d\n",
842 teedev
->name
, MAJOR(teedev
->dev
.devt
),
843 MINOR(teedev
->dev
.devt
), rc
);
847 rc
= sysfs_create_group(&teedev
->dev
.kobj
, &tee_dev_group
);
849 dev_err(&teedev
->dev
,
850 "failed to create sysfs attributes, err=%d\n", rc
);
851 goto err_sysfs_create_group
;
854 teedev
->flags
|= TEE_DEVICE_FLAG_REGISTERED
;
857 err_sysfs_create_group
:
858 device_del(&teedev
->dev
);
860 cdev_del(&teedev
->cdev
);
863 EXPORT_SYMBOL_GPL(tee_device_register
);
865 void tee_device_put(struct tee_device
*teedev
)
867 mutex_lock(&teedev
->mutex
);
868 /* Shouldn't put in this state */
869 if (!WARN_ON(!teedev
->desc
)) {
871 if (!teedev
->num_users
) {
873 complete(&teedev
->c_no_users
);
876 mutex_unlock(&teedev
->mutex
);
879 bool tee_device_get(struct tee_device
*teedev
)
881 mutex_lock(&teedev
->mutex
);
883 mutex_unlock(&teedev
->mutex
);
887 mutex_unlock(&teedev
->mutex
);
892 * tee_device_unregister() - Removes a TEE device
893 * @teedev: Device to unregister
895 * This function should be called to remove the @teedev even if
896 * tee_device_register() hasn't been called yet. Does nothing if
899 void tee_device_unregister(struct tee_device
*teedev
)
904 if (teedev
->flags
& TEE_DEVICE_FLAG_REGISTERED
) {
905 sysfs_remove_group(&teedev
->dev
.kobj
, &tee_dev_group
);
906 cdev_del(&teedev
->cdev
);
907 device_del(&teedev
->dev
);
910 tee_device_put(teedev
);
911 wait_for_completion(&teedev
->c_no_users
);
914 * No need to take a mutex any longer now since teedev->desc was
915 * set to NULL before teedev->c_no_users was completed.
920 put_device(&teedev
->dev
);
922 EXPORT_SYMBOL_GPL(tee_device_unregister
);
925 * tee_get_drvdata() - Return driver_data pointer
926 * @teedev: Device containing the driver_data pointer
927 * @returns the driver_data pointer supplied to tee_register().
929 void *tee_get_drvdata(struct tee_device
*teedev
)
931 return dev_get_drvdata(&teedev
->dev
);
933 EXPORT_SYMBOL_GPL(tee_get_drvdata
);
935 struct match_dev_data
{
936 struct tee_ioctl_version_data
*vers
;
938 int (*match
)(struct tee_ioctl_version_data
*, const void *);
941 static int match_dev(struct device
*dev
, const void *data
)
943 const struct match_dev_data
*match_data
= data
;
944 struct tee_device
*teedev
= container_of(dev
, struct tee_device
, dev
);
946 teedev
->desc
->ops
->get_version(teedev
, match_data
->vers
);
947 return match_data
->match(match_data
->vers
, match_data
->data
);
951 tee_client_open_context(struct tee_context
*start
,
952 int (*match
)(struct tee_ioctl_version_data
*,
954 const void *data
, struct tee_ioctl_version_data
*vers
)
956 struct device
*dev
= NULL
;
957 struct device
*put_dev
= NULL
;
958 struct tee_context
*ctx
= NULL
;
959 struct tee_ioctl_version_data v
;
960 struct match_dev_data match_data
= { vers
? vers
: &v
, data
, match
};
963 dev
= &start
->teedev
->dev
;
966 dev
= class_find_device(tee_class
, dev
, &match_data
, match_dev
);
968 ctx
= ERR_PTR(-ENOENT
);
975 ctx
= teedev_open(container_of(dev
, struct tee_device
, dev
));
976 } while (IS_ERR(ctx
) && PTR_ERR(ctx
) != -ENOMEM
);
980 * Default behaviour for in kernel client is to not wait for
981 * tee-supplicant if not present for any requests in this context.
982 * Also this flag could be configured again before call to
983 * tee_client_open_session() if any in kernel client requires
984 * different behaviour.
987 ctx
->supp_nowait
= true;
991 EXPORT_SYMBOL_GPL(tee_client_open_context
);
993 void tee_client_close_context(struct tee_context
*ctx
)
995 teedev_close_context(ctx
);
997 EXPORT_SYMBOL_GPL(tee_client_close_context
);
999 void tee_client_get_version(struct tee_context
*ctx
,
1000 struct tee_ioctl_version_data
*vers
)
1002 ctx
->teedev
->desc
->ops
->get_version(ctx
->teedev
, vers
);
1004 EXPORT_SYMBOL_GPL(tee_client_get_version
);
1006 int tee_client_open_session(struct tee_context
*ctx
,
1007 struct tee_ioctl_open_session_arg
*arg
,
1008 struct tee_param
*param
)
1010 if (!ctx
->teedev
->desc
->ops
->open_session
)
1012 return ctx
->teedev
->desc
->ops
->open_session(ctx
, arg
, param
);
1014 EXPORT_SYMBOL_GPL(tee_client_open_session
);
1016 int tee_client_close_session(struct tee_context
*ctx
, u32 session
)
1018 if (!ctx
->teedev
->desc
->ops
->close_session
)
1020 return ctx
->teedev
->desc
->ops
->close_session(ctx
, session
);
1022 EXPORT_SYMBOL_GPL(tee_client_close_session
);
1024 int tee_client_invoke_func(struct tee_context
*ctx
,
1025 struct tee_ioctl_invoke_arg
*arg
,
1026 struct tee_param
*param
)
1028 if (!ctx
->teedev
->desc
->ops
->invoke_func
)
1030 return ctx
->teedev
->desc
->ops
->invoke_func(ctx
, arg
, param
);
1032 EXPORT_SYMBOL_GPL(tee_client_invoke_func
);
1034 int tee_client_cancel_req(struct tee_context
*ctx
,
1035 struct tee_ioctl_cancel_arg
*arg
)
1037 if (!ctx
->teedev
->desc
->ops
->cancel_req
)
1039 return ctx
->teedev
->desc
->ops
->cancel_req(ctx
, arg
->cancel_id
,
1043 static int tee_client_device_match(struct device
*dev
,
1044 struct device_driver
*drv
)
1046 const struct tee_client_device_id
*id_table
;
1047 struct tee_client_device
*tee_device
;
1049 id_table
= to_tee_client_driver(drv
)->id_table
;
1050 tee_device
= to_tee_client_device(dev
);
1052 while (!uuid_is_null(&id_table
->uuid
)) {
1053 if (uuid_equal(&tee_device
->id
.uuid
, &id_table
->uuid
))
1061 static int tee_client_device_uevent(struct device
*dev
,
1062 struct kobj_uevent_env
*env
)
1064 uuid_t
*dev_id
= &to_tee_client_device(dev
)->id
.uuid
;
1066 return add_uevent_var(env
, "MODALIAS=tee:%pUb", dev_id
);
1069 struct bus_type tee_bus_type
= {
1071 .match
= tee_client_device_match
,
1072 .uevent
= tee_client_device_uevent
,
1074 EXPORT_SYMBOL_GPL(tee_bus_type
);
1076 static int __init
tee_init(void)
1080 tee_class
= class_create(THIS_MODULE
, "tee");
1081 if (IS_ERR(tee_class
)) {
1082 pr_err("couldn't create class\n");
1083 return PTR_ERR(tee_class
);
1086 rc
= alloc_chrdev_region(&tee_devt
, 0, TEE_NUM_DEVICES
, "tee");
1088 pr_err("failed to allocate char dev region\n");
1089 goto out_unreg_class
;
1092 rc
= bus_register(&tee_bus_type
);
1094 pr_err("failed to register tee bus\n");
1095 goto out_unreg_chrdev
;
1101 unregister_chrdev_region(tee_devt
, TEE_NUM_DEVICES
);
1103 class_destroy(tee_class
);
1109 static void __exit
tee_exit(void)
1111 bus_unregister(&tee_bus_type
);
1112 unregister_chrdev_region(tee_devt
, TEE_NUM_DEVICES
);
1113 class_destroy(tee_class
);
1117 subsys_initcall(tee_init
);
1118 module_exit(tee_exit
);
1120 MODULE_AUTHOR("Linaro");
1121 MODULE_DESCRIPTION("TEE Driver");
1122 MODULE_VERSION("1.0");
1123 MODULE_LICENSE("GPL v2");