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 INIT_LIST_HEAD(&ctx
->list_shm
);
48 rc
= teedev
->desc
->ops
->open(ctx
);
55 tee_device_put(teedev
);
60 void teedev_ctx_get(struct tee_context
*ctx
)
65 kref_get(&ctx
->refcount
);
68 static void teedev_ctx_release(struct kref
*ref
)
70 struct tee_context
*ctx
= container_of(ref
, struct tee_context
,
72 ctx
->releasing
= true;
73 ctx
->teedev
->desc
->ops
->release(ctx
);
77 void teedev_ctx_put(struct tee_context
*ctx
)
82 kref_put(&ctx
->refcount
, teedev_ctx_release
);
85 static void teedev_close_context(struct tee_context
*ctx
)
87 tee_device_put(ctx
->teedev
);
91 static int tee_open(struct inode
*inode
, struct file
*filp
)
93 struct tee_context
*ctx
;
95 ctx
= teedev_open(container_of(inode
->i_cdev
, struct tee_device
, cdev
));
100 * Default user-space behaviour is to wait for tee-supplicant
101 * if not present for any requests in this context.
103 ctx
->supp_nowait
= false;
104 filp
->private_data
= ctx
;
108 static int tee_release(struct inode
*inode
, struct file
*filp
)
110 teedev_close_context(filp
->private_data
);
114 static int tee_ioctl_version(struct tee_context
*ctx
,
115 struct tee_ioctl_version_data __user
*uvers
)
117 struct tee_ioctl_version_data vers
;
119 ctx
->teedev
->desc
->ops
->get_version(ctx
->teedev
, &vers
);
121 if (ctx
->teedev
->desc
->flags
& TEE_DESC_PRIVILEGED
)
122 vers
.gen_caps
|= TEE_GEN_CAP_PRIVILEGED
;
124 if (copy_to_user(uvers
, &vers
, sizeof(vers
)))
130 static int tee_ioctl_shm_alloc(struct tee_context
*ctx
,
131 struct tee_ioctl_shm_alloc_data __user
*udata
)
134 struct tee_ioctl_shm_alloc_data data
;
137 if (copy_from_user(&data
, udata
, sizeof(data
)))
140 /* Currently no input flags are supported */
144 shm
= tee_shm_alloc(ctx
, data
.size
, TEE_SHM_MAPPED
| TEE_SHM_DMA_BUF
);
149 data
.flags
= shm
->flags
;
150 data
.size
= shm
->size
;
152 if (copy_to_user(udata
, &data
, sizeof(data
)))
155 ret
= tee_shm_get_fd(shm
);
158 * When user space closes the file descriptor the shared memory
159 * should be freed or if tee_shm_get_fd() failed then it will
160 * be freed immediately.
167 tee_ioctl_shm_register(struct tee_context
*ctx
,
168 struct tee_ioctl_shm_register_data __user
*udata
)
171 struct tee_ioctl_shm_register_data data
;
174 if (copy_from_user(&data
, udata
, sizeof(data
)))
177 /* Currently no input flags are supported */
181 shm
= tee_shm_register(ctx
, data
.addr
, data
.length
,
182 TEE_SHM_DMA_BUF
| TEE_SHM_USER_MAPPED
);
187 data
.flags
= shm
->flags
;
188 data
.length
= shm
->size
;
190 if (copy_to_user(udata
, &data
, sizeof(data
)))
193 ret
= tee_shm_get_fd(shm
);
195 * When user space closes the file descriptor the shared memory
196 * should be freed or if tee_shm_get_fd() failed then it will
197 * be freed immediately.
203 static int params_from_user(struct tee_context
*ctx
, struct tee_param
*params
,
205 struct tee_ioctl_param __user
*uparams
)
209 for (n
= 0; n
< num_params
; n
++) {
211 struct tee_ioctl_param ip
;
213 if (copy_from_user(&ip
, uparams
+ n
, sizeof(ip
)))
216 /* All unused attribute bits has to be zero */
217 if (ip
.attr
& ~TEE_IOCTL_PARAM_ATTR_MASK
)
220 params
[n
].attr
= ip
.attr
;
221 switch (ip
.attr
& TEE_IOCTL_PARAM_ATTR_TYPE_MASK
) {
222 case TEE_IOCTL_PARAM_ATTR_TYPE_NONE
:
223 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT
:
225 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT
:
226 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
227 params
[n
].u
.value
.a
= ip
.a
;
228 params
[n
].u
.value
.b
= ip
.b
;
229 params
[n
].u
.value
.c
= ip
.c
;
231 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT
:
232 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
233 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
235 * If we fail to get a pointer to a shared memory
236 * object (and increase the ref count) from an
237 * identifier we return an error. All pointers that
238 * has been added in params have an increased ref
239 * count. It's the callers responibility to do
240 * tee_shm_put() on all resolved pointers.
242 shm
= tee_shm_get_from_id(ctx
, ip
.c
);
247 * Ensure offset + size does not overflow offset
248 * and does not overflow the size of the referred
249 * shared memory object.
251 if ((ip
.a
+ ip
.b
) < ip
.a
||
252 (ip
.a
+ ip
.b
) > shm
->size
) {
257 params
[n
].u
.memref
.shm_offs
= ip
.a
;
258 params
[n
].u
.memref
.size
= ip
.b
;
259 params
[n
].u
.memref
.shm
= shm
;
262 /* Unknown attribute */
269 static int params_to_user(struct tee_ioctl_param __user
*uparams
,
270 size_t num_params
, struct tee_param
*params
)
274 for (n
= 0; n
< num_params
; n
++) {
275 struct tee_ioctl_param __user
*up
= uparams
+ n
;
276 struct tee_param
*p
= params
+ n
;
279 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT
:
280 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
281 if (put_user(p
->u
.value
.a
, &up
->a
) ||
282 put_user(p
->u
.value
.b
, &up
->b
) ||
283 put_user(p
->u
.value
.c
, &up
->c
))
286 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
287 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
288 if (put_user((u64
)p
->u
.memref
.size
, &up
->b
))
297 static int tee_ioctl_open_session(struct tee_context
*ctx
,
298 struct tee_ioctl_buf_data __user
*ubuf
)
302 struct tee_ioctl_buf_data buf
;
303 struct tee_ioctl_open_session_arg __user
*uarg
;
304 struct tee_ioctl_open_session_arg arg
;
305 struct tee_ioctl_param __user
*uparams
= NULL
;
306 struct tee_param
*params
= NULL
;
307 bool have_session
= false;
309 if (!ctx
->teedev
->desc
->ops
->open_session
)
312 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
315 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
316 buf
.buf_len
< sizeof(struct tee_ioctl_open_session_arg
))
319 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
320 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
323 if (sizeof(arg
) + TEE_IOCTL_PARAM_SIZE(arg
.num_params
) != buf
.buf_len
)
326 if (arg
.num_params
) {
327 params
= kcalloc(arg
.num_params
, sizeof(struct tee_param
),
331 uparams
= uarg
->params
;
332 rc
= params_from_user(ctx
, params
, arg
.num_params
, uparams
);
337 rc
= ctx
->teedev
->desc
->ops
->open_session(ctx
, &arg
, params
);
342 if (put_user(arg
.session
, &uarg
->session
) ||
343 put_user(arg
.ret
, &uarg
->ret
) ||
344 put_user(arg
.ret_origin
, &uarg
->ret_origin
)) {
348 rc
= params_to_user(uparams
, arg
.num_params
, params
);
351 * If we've succeeded to open the session but failed to communicate
352 * it back to user space, close the session again to avoid leakage.
354 if (rc
&& have_session
&& ctx
->teedev
->desc
->ops
->close_session
)
355 ctx
->teedev
->desc
->ops
->close_session(ctx
, arg
.session
);
358 /* Decrease ref count for all valid shared memory pointers */
359 for (n
= 0; n
< arg
.num_params
; n
++)
360 if (tee_param_is_memref(params
+ n
) &&
361 params
[n
].u
.memref
.shm
)
362 tee_shm_put(params
[n
].u
.memref
.shm
);
369 static int tee_ioctl_invoke(struct tee_context
*ctx
,
370 struct tee_ioctl_buf_data __user
*ubuf
)
374 struct tee_ioctl_buf_data buf
;
375 struct tee_ioctl_invoke_arg __user
*uarg
;
376 struct tee_ioctl_invoke_arg arg
;
377 struct tee_ioctl_param __user
*uparams
= NULL
;
378 struct tee_param
*params
= NULL
;
380 if (!ctx
->teedev
->desc
->ops
->invoke_func
)
383 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
386 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
387 buf
.buf_len
< sizeof(struct tee_ioctl_invoke_arg
))
390 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
391 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
394 if (sizeof(arg
) + TEE_IOCTL_PARAM_SIZE(arg
.num_params
) != buf
.buf_len
)
397 if (arg
.num_params
) {
398 params
= kcalloc(arg
.num_params
, sizeof(struct tee_param
),
402 uparams
= uarg
->params
;
403 rc
= params_from_user(ctx
, params
, arg
.num_params
, uparams
);
408 rc
= ctx
->teedev
->desc
->ops
->invoke_func(ctx
, &arg
, params
);
412 if (put_user(arg
.ret
, &uarg
->ret
) ||
413 put_user(arg
.ret_origin
, &uarg
->ret_origin
)) {
417 rc
= params_to_user(uparams
, arg
.num_params
, params
);
420 /* Decrease ref count for all valid shared memory pointers */
421 for (n
= 0; n
< arg
.num_params
; n
++)
422 if (tee_param_is_memref(params
+ n
) &&
423 params
[n
].u
.memref
.shm
)
424 tee_shm_put(params
[n
].u
.memref
.shm
);
430 static int tee_ioctl_cancel(struct tee_context
*ctx
,
431 struct tee_ioctl_cancel_arg __user
*uarg
)
433 struct tee_ioctl_cancel_arg arg
;
435 if (!ctx
->teedev
->desc
->ops
->cancel_req
)
438 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
441 return ctx
->teedev
->desc
->ops
->cancel_req(ctx
, arg
.cancel_id
,
446 tee_ioctl_close_session(struct tee_context
*ctx
,
447 struct tee_ioctl_close_session_arg __user
*uarg
)
449 struct tee_ioctl_close_session_arg arg
;
451 if (!ctx
->teedev
->desc
->ops
->close_session
)
454 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
457 return ctx
->teedev
->desc
->ops
->close_session(ctx
, arg
.session
);
460 static int params_to_supp(struct tee_context
*ctx
,
461 struct tee_ioctl_param __user
*uparams
,
462 size_t num_params
, struct tee_param
*params
)
466 for (n
= 0; n
< num_params
; n
++) {
467 struct tee_ioctl_param ip
;
468 struct tee_param
*p
= params
+ n
;
471 switch (p
->attr
& TEE_IOCTL_PARAM_ATTR_TYPE_MASK
) {
472 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT
:
473 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
478 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT
:
479 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
480 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
481 ip
.b
= p
->u
.memref
.size
;
482 if (!p
->u
.memref
.shm
) {
484 ip
.c
= (u64
)-1; /* invalid shm id */
487 ip
.a
= p
->u
.memref
.shm_offs
;
488 ip
.c
= p
->u
.memref
.shm
->id
;
497 if (copy_to_user(uparams
+ n
, &ip
, sizeof(ip
)))
504 static int tee_ioctl_supp_recv(struct tee_context
*ctx
,
505 struct tee_ioctl_buf_data __user
*ubuf
)
508 struct tee_ioctl_buf_data buf
;
509 struct tee_iocl_supp_recv_arg __user
*uarg
;
510 struct tee_param
*params
;
514 if (!ctx
->teedev
->desc
->ops
->supp_recv
)
517 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
520 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
521 buf
.buf_len
< sizeof(struct tee_iocl_supp_recv_arg
))
524 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
525 if (get_user(num_params
, &uarg
->num_params
))
528 if (sizeof(*uarg
) + TEE_IOCTL_PARAM_SIZE(num_params
) != buf
.buf_len
)
531 params
= kcalloc(num_params
, sizeof(struct tee_param
), GFP_KERNEL
);
535 rc
= params_from_user(ctx
, params
, num_params
, uarg
->params
);
539 rc
= ctx
->teedev
->desc
->ops
->supp_recv(ctx
, &func
, &num_params
, params
);
543 if (put_user(func
, &uarg
->func
) ||
544 put_user(num_params
, &uarg
->num_params
)) {
549 rc
= params_to_supp(ctx
, uarg
->params
, num_params
, params
);
555 static int params_from_supp(struct tee_param
*params
, size_t num_params
,
556 struct tee_ioctl_param __user
*uparams
)
560 for (n
= 0; n
< num_params
; n
++) {
561 struct tee_param
*p
= params
+ n
;
562 struct tee_ioctl_param ip
;
564 if (copy_from_user(&ip
, uparams
+ n
, sizeof(ip
)))
567 /* All unused attribute bits has to be zero */
568 if (ip
.attr
& ~TEE_IOCTL_PARAM_ATTR_MASK
)
572 switch (ip
.attr
& TEE_IOCTL_PARAM_ATTR_TYPE_MASK
) {
573 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT
:
574 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
575 /* Only out and in/out values can be updated */
580 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
581 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
583 * Only the size of the memref can be updated.
584 * Since we don't have access to the original
585 * parameters here, only store the supplied size.
586 * The driver will copy the updated size into the
587 * original parameters.
589 p
->u
.memref
.shm
= NULL
;
590 p
->u
.memref
.shm_offs
= 0;
591 p
->u
.memref
.size
= ip
.b
;
594 memset(&p
->u
, 0, sizeof(p
->u
));
601 static int tee_ioctl_supp_send(struct tee_context
*ctx
,
602 struct tee_ioctl_buf_data __user
*ubuf
)
605 struct tee_ioctl_buf_data buf
;
606 struct tee_iocl_supp_send_arg __user
*uarg
;
607 struct tee_param
*params
;
611 /* Not valid for this driver */
612 if (!ctx
->teedev
->desc
->ops
->supp_send
)
615 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
618 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
619 buf
.buf_len
< sizeof(struct tee_iocl_supp_send_arg
))
622 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
623 if (get_user(ret
, &uarg
->ret
) ||
624 get_user(num_params
, &uarg
->num_params
))
627 if (sizeof(*uarg
) + TEE_IOCTL_PARAM_SIZE(num_params
) > buf
.buf_len
)
630 params
= kcalloc(num_params
, sizeof(struct tee_param
), GFP_KERNEL
);
634 rc
= params_from_supp(params
, num_params
, uarg
->params
);
638 rc
= ctx
->teedev
->desc
->ops
->supp_send(ctx
, ret
, num_params
, params
);
644 static long tee_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
646 struct tee_context
*ctx
= filp
->private_data
;
647 void __user
*uarg
= (void __user
*)arg
;
650 case TEE_IOC_VERSION
:
651 return tee_ioctl_version(ctx
, uarg
);
652 case TEE_IOC_SHM_ALLOC
:
653 return tee_ioctl_shm_alloc(ctx
, uarg
);
654 case TEE_IOC_SHM_REGISTER
:
655 return tee_ioctl_shm_register(ctx
, uarg
);
656 case TEE_IOC_OPEN_SESSION
:
657 return tee_ioctl_open_session(ctx
, uarg
);
659 return tee_ioctl_invoke(ctx
, uarg
);
661 return tee_ioctl_cancel(ctx
, uarg
);
662 case TEE_IOC_CLOSE_SESSION
:
663 return tee_ioctl_close_session(ctx
, uarg
);
664 case TEE_IOC_SUPPL_RECV
:
665 return tee_ioctl_supp_recv(ctx
, uarg
);
666 case TEE_IOC_SUPPL_SEND
:
667 return tee_ioctl_supp_send(ctx
, uarg
);
673 static const struct file_operations tee_fops
= {
674 .owner
= THIS_MODULE
,
676 .release
= tee_release
,
677 .unlocked_ioctl
= tee_ioctl
,
678 .compat_ioctl
= compat_ptr_ioctl
,
681 static void tee_release_device(struct device
*dev
)
683 struct tee_device
*teedev
= container_of(dev
, struct tee_device
, dev
);
685 spin_lock(&driver_lock
);
686 clear_bit(teedev
->id
, dev_mask
);
687 spin_unlock(&driver_lock
);
688 mutex_destroy(&teedev
->mutex
);
689 idr_destroy(&teedev
->idr
);
694 * tee_device_alloc() - Allocate a new struct tee_device instance
695 * @teedesc: Descriptor for this driver
696 * @dev: Parent device for this device
697 * @pool: Shared memory pool, NULL if not used
698 * @driver_data: Private driver data for this device
700 * Allocates a new struct tee_device instance. The device is
701 * removed by tee_device_unregister().
703 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
705 struct tee_device
*tee_device_alloc(const struct tee_desc
*teedesc
,
707 struct tee_shm_pool
*pool
,
710 struct tee_device
*teedev
;
715 if (!teedesc
|| !teedesc
->name
|| !teedesc
->ops
||
716 !teedesc
->ops
->get_version
|| !teedesc
->ops
->open
||
717 !teedesc
->ops
->release
|| !pool
)
718 return ERR_PTR(-EINVAL
);
720 teedev
= kzalloc(sizeof(*teedev
), GFP_KERNEL
);
722 ret
= ERR_PTR(-ENOMEM
);
726 max_id
= TEE_NUM_DEVICES
/ 2;
728 if (teedesc
->flags
& TEE_DESC_PRIVILEGED
) {
729 offs
= TEE_NUM_DEVICES
/ 2;
730 max_id
= TEE_NUM_DEVICES
;
733 spin_lock(&driver_lock
);
734 teedev
->id
= find_next_zero_bit(dev_mask
, max_id
, offs
);
735 if (teedev
->id
< max_id
)
736 set_bit(teedev
->id
, dev_mask
);
737 spin_unlock(&driver_lock
);
739 if (teedev
->id
>= max_id
) {
740 ret
= ERR_PTR(-ENOMEM
);
744 snprintf(teedev
->name
, sizeof(teedev
->name
), "tee%s%d",
745 teedesc
->flags
& TEE_DESC_PRIVILEGED
? "priv" : "",
748 teedev
->dev
.class = tee_class
;
749 teedev
->dev
.release
= tee_release_device
;
750 teedev
->dev
.parent
= dev
;
752 teedev
->dev
.devt
= MKDEV(MAJOR(tee_devt
), teedev
->id
);
754 rc
= dev_set_name(&teedev
->dev
, "%s", teedev
->name
);
760 cdev_init(&teedev
->cdev
, &tee_fops
);
761 teedev
->cdev
.owner
= teedesc
->owner
;
762 teedev
->cdev
.kobj
.parent
= &teedev
->dev
.kobj
;
764 dev_set_drvdata(&teedev
->dev
, driver_data
);
765 device_initialize(&teedev
->dev
);
767 /* 1 as tee_device_unregister() does one final tee_device_put() */
768 teedev
->num_users
= 1;
769 init_completion(&teedev
->c_no_users
);
770 mutex_init(&teedev
->mutex
);
771 idr_init(&teedev
->idr
);
773 teedev
->desc
= teedesc
;
778 unregister_chrdev_region(teedev
->dev
.devt
, 1);
780 pr_err("could not register %s driver\n",
781 teedesc
->flags
& TEE_DESC_PRIVILEGED
? "privileged" : "client");
782 if (teedev
&& teedev
->id
< TEE_NUM_DEVICES
) {
783 spin_lock(&driver_lock
);
784 clear_bit(teedev
->id
, dev_mask
);
785 spin_unlock(&driver_lock
);
790 EXPORT_SYMBOL_GPL(tee_device_alloc
);
792 static ssize_t
implementation_id_show(struct device
*dev
,
793 struct device_attribute
*attr
, char *buf
)
795 struct tee_device
*teedev
= container_of(dev
, struct tee_device
, dev
);
796 struct tee_ioctl_version_data vers
;
798 teedev
->desc
->ops
->get_version(teedev
, &vers
);
799 return scnprintf(buf
, PAGE_SIZE
, "%d\n", vers
.impl_id
);
801 static DEVICE_ATTR_RO(implementation_id
);
803 static struct attribute
*tee_dev_attrs
[] = {
804 &dev_attr_implementation_id
.attr
,
808 static const struct attribute_group tee_dev_group
= {
809 .attrs
= tee_dev_attrs
,
813 * tee_device_register() - Registers a TEE device
814 * @teedev: Device to register
816 * tee_device_unregister() need to be called to remove the @teedev if
817 * this function fails.
819 * @returns < 0 on failure
821 int tee_device_register(struct tee_device
*teedev
)
825 if (teedev
->flags
& TEE_DEVICE_FLAG_REGISTERED
) {
826 dev_err(&teedev
->dev
, "attempt to register twice\n");
830 rc
= cdev_add(&teedev
->cdev
, teedev
->dev
.devt
, 1);
832 dev_err(&teedev
->dev
,
833 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
834 teedev
->name
, MAJOR(teedev
->dev
.devt
),
835 MINOR(teedev
->dev
.devt
), rc
);
839 rc
= device_add(&teedev
->dev
);
841 dev_err(&teedev
->dev
,
842 "unable to device_add() %s, major %d, minor %d, err=%d\n",
843 teedev
->name
, MAJOR(teedev
->dev
.devt
),
844 MINOR(teedev
->dev
.devt
), rc
);
848 rc
= sysfs_create_group(&teedev
->dev
.kobj
, &tee_dev_group
);
850 dev_err(&teedev
->dev
,
851 "failed to create sysfs attributes, err=%d\n", rc
);
852 goto err_sysfs_create_group
;
855 teedev
->flags
|= TEE_DEVICE_FLAG_REGISTERED
;
858 err_sysfs_create_group
:
859 device_del(&teedev
->dev
);
861 cdev_del(&teedev
->cdev
);
864 EXPORT_SYMBOL_GPL(tee_device_register
);
866 void tee_device_put(struct tee_device
*teedev
)
868 mutex_lock(&teedev
->mutex
);
869 /* Shouldn't put in this state */
870 if (!WARN_ON(!teedev
->desc
)) {
872 if (!teedev
->num_users
) {
874 complete(&teedev
->c_no_users
);
877 mutex_unlock(&teedev
->mutex
);
880 bool tee_device_get(struct tee_device
*teedev
)
882 mutex_lock(&teedev
->mutex
);
884 mutex_unlock(&teedev
->mutex
);
888 mutex_unlock(&teedev
->mutex
);
893 * tee_device_unregister() - Removes a TEE device
894 * @teedev: Device to unregister
896 * This function should be called to remove the @teedev even if
897 * tee_device_register() hasn't been called yet. Does nothing if
900 void tee_device_unregister(struct tee_device
*teedev
)
905 if (teedev
->flags
& TEE_DEVICE_FLAG_REGISTERED
) {
906 sysfs_remove_group(&teedev
->dev
.kobj
, &tee_dev_group
);
907 cdev_del(&teedev
->cdev
);
908 device_del(&teedev
->dev
);
911 tee_device_put(teedev
);
912 wait_for_completion(&teedev
->c_no_users
);
915 * No need to take a mutex any longer now since teedev->desc was
916 * set to NULL before teedev->c_no_users was completed.
921 put_device(&teedev
->dev
);
923 EXPORT_SYMBOL_GPL(tee_device_unregister
);
926 * tee_get_drvdata() - Return driver_data pointer
927 * @teedev: Device containing the driver_data pointer
928 * @returns the driver_data pointer supplied to tee_register().
930 void *tee_get_drvdata(struct tee_device
*teedev
)
932 return dev_get_drvdata(&teedev
->dev
);
934 EXPORT_SYMBOL_GPL(tee_get_drvdata
);
936 struct match_dev_data
{
937 struct tee_ioctl_version_data
*vers
;
939 int (*match
)(struct tee_ioctl_version_data
*, const void *);
942 static int match_dev(struct device
*dev
, const void *data
)
944 const struct match_dev_data
*match_data
= data
;
945 struct tee_device
*teedev
= container_of(dev
, struct tee_device
, dev
);
947 teedev
->desc
->ops
->get_version(teedev
, match_data
->vers
);
948 return match_data
->match(match_data
->vers
, match_data
->data
);
952 tee_client_open_context(struct tee_context
*start
,
953 int (*match
)(struct tee_ioctl_version_data
*,
955 const void *data
, struct tee_ioctl_version_data
*vers
)
957 struct device
*dev
= NULL
;
958 struct device
*put_dev
= NULL
;
959 struct tee_context
*ctx
= NULL
;
960 struct tee_ioctl_version_data v
;
961 struct match_dev_data match_data
= { vers
? vers
: &v
, data
, match
};
964 dev
= &start
->teedev
->dev
;
967 dev
= class_find_device(tee_class
, dev
, &match_data
, match_dev
);
969 ctx
= ERR_PTR(-ENOENT
);
976 ctx
= teedev_open(container_of(dev
, struct tee_device
, dev
));
977 } while (IS_ERR(ctx
) && PTR_ERR(ctx
) != -ENOMEM
);
981 * Default behaviour for in kernel client is to not wait for
982 * tee-supplicant if not present for any requests in this context.
983 * Also this flag could be configured again before call to
984 * tee_client_open_session() if any in kernel client requires
985 * different behaviour.
988 ctx
->supp_nowait
= true;
992 EXPORT_SYMBOL_GPL(tee_client_open_context
);
994 void tee_client_close_context(struct tee_context
*ctx
)
996 teedev_close_context(ctx
);
998 EXPORT_SYMBOL_GPL(tee_client_close_context
);
1000 void tee_client_get_version(struct tee_context
*ctx
,
1001 struct tee_ioctl_version_data
*vers
)
1003 ctx
->teedev
->desc
->ops
->get_version(ctx
->teedev
, vers
);
1005 EXPORT_SYMBOL_GPL(tee_client_get_version
);
1007 int tee_client_open_session(struct tee_context
*ctx
,
1008 struct tee_ioctl_open_session_arg
*arg
,
1009 struct tee_param
*param
)
1011 if (!ctx
->teedev
->desc
->ops
->open_session
)
1013 return ctx
->teedev
->desc
->ops
->open_session(ctx
, arg
, param
);
1015 EXPORT_SYMBOL_GPL(tee_client_open_session
);
1017 int tee_client_close_session(struct tee_context
*ctx
, u32 session
)
1019 if (!ctx
->teedev
->desc
->ops
->close_session
)
1021 return ctx
->teedev
->desc
->ops
->close_session(ctx
, session
);
1023 EXPORT_SYMBOL_GPL(tee_client_close_session
);
1025 int tee_client_invoke_func(struct tee_context
*ctx
,
1026 struct tee_ioctl_invoke_arg
*arg
,
1027 struct tee_param
*param
)
1029 if (!ctx
->teedev
->desc
->ops
->invoke_func
)
1031 return ctx
->teedev
->desc
->ops
->invoke_func(ctx
, arg
, param
);
1033 EXPORT_SYMBOL_GPL(tee_client_invoke_func
);
1035 int tee_client_cancel_req(struct tee_context
*ctx
,
1036 struct tee_ioctl_cancel_arg
*arg
)
1038 if (!ctx
->teedev
->desc
->ops
->cancel_req
)
1040 return ctx
->teedev
->desc
->ops
->cancel_req(ctx
, arg
->cancel_id
,
1044 static int tee_client_device_match(struct device
*dev
,
1045 struct device_driver
*drv
)
1047 const struct tee_client_device_id
*id_table
;
1048 struct tee_client_device
*tee_device
;
1050 id_table
= to_tee_client_driver(drv
)->id_table
;
1051 tee_device
= to_tee_client_device(dev
);
1053 while (!uuid_is_null(&id_table
->uuid
)) {
1054 if (uuid_equal(&tee_device
->id
.uuid
, &id_table
->uuid
))
1062 static int tee_client_device_uevent(struct device
*dev
,
1063 struct kobj_uevent_env
*env
)
1065 uuid_t
*dev_id
= &to_tee_client_device(dev
)->id
.uuid
;
1067 return add_uevent_var(env
, "MODALIAS=tee:%pUb", dev_id
);
1070 struct bus_type tee_bus_type
= {
1072 .match
= tee_client_device_match
,
1073 .uevent
= tee_client_device_uevent
,
1075 EXPORT_SYMBOL_GPL(tee_bus_type
);
1077 static int __init
tee_init(void)
1081 tee_class
= class_create(THIS_MODULE
, "tee");
1082 if (IS_ERR(tee_class
)) {
1083 pr_err("couldn't create class\n");
1084 return PTR_ERR(tee_class
);
1087 rc
= alloc_chrdev_region(&tee_devt
, 0, TEE_NUM_DEVICES
, "tee");
1089 pr_err("failed to allocate char dev region\n");
1090 goto out_unreg_class
;
1093 rc
= bus_register(&tee_bus_type
);
1095 pr_err("failed to register tee bus\n");
1096 goto out_unreg_chrdev
;
1102 unregister_chrdev_region(tee_devt
, TEE_NUM_DEVICES
);
1104 class_destroy(tee_class
);
1110 static void __exit
tee_exit(void)
1112 bus_unregister(&tee_bus_type
);
1113 unregister_chrdev_region(tee_devt
, TEE_NUM_DEVICES
);
1114 class_destroy(tee_class
);
1118 subsys_initcall(tee_init
);
1119 module_exit(tee_exit
);
1121 MODULE_AUTHOR("Linaro");
1122 MODULE_DESCRIPTION("TEE Driver");
1123 MODULE_VERSION("1.0");
1124 MODULE_LICENSE("GPL v2");