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>
19 #include <linux/idr.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/tee_drv.h>
23 #include <linux/uaccess.h>
24 #include "tee_private.h"
26 #define TEE_NUM_DEVICES 32
28 #define TEE_IOCTL_PARAM_SIZE(x) (sizeof(struct tee_param) * (x))
31 * Unprivileged devices in the lower half range and privileged devices in
32 * the upper half range.
34 static DECLARE_BITMAP(dev_mask
, TEE_NUM_DEVICES
);
35 static DEFINE_SPINLOCK(driver_lock
);
37 static struct class *tee_class
;
38 static dev_t tee_devt
;
40 static struct tee_context
*teedev_open(struct tee_device
*teedev
)
43 struct tee_context
*ctx
;
45 if (!tee_device_get(teedev
))
46 return ERR_PTR(-EINVAL
);
48 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
54 kref_init(&ctx
->refcount
);
56 INIT_LIST_HEAD(&ctx
->list_shm
);
57 rc
= teedev
->desc
->ops
->open(ctx
);
64 tee_device_put(teedev
);
69 void teedev_ctx_get(struct tee_context
*ctx
)
74 kref_get(&ctx
->refcount
);
77 static void teedev_ctx_release(struct kref
*ref
)
79 struct tee_context
*ctx
= container_of(ref
, struct tee_context
,
81 ctx
->releasing
= true;
82 ctx
->teedev
->desc
->ops
->release(ctx
);
86 void teedev_ctx_put(struct tee_context
*ctx
)
91 kref_put(&ctx
->refcount
, teedev_ctx_release
);
94 static void teedev_close_context(struct tee_context
*ctx
)
96 tee_device_put(ctx
->teedev
);
100 static int tee_open(struct inode
*inode
, struct file
*filp
)
102 struct tee_context
*ctx
;
104 ctx
= teedev_open(container_of(inode
->i_cdev
, struct tee_device
, cdev
));
109 * Default user-space behaviour is to wait for tee-supplicant
110 * if not present for any requests in this context.
112 ctx
->supp_nowait
= false;
113 filp
->private_data
= ctx
;
117 static int tee_release(struct inode
*inode
, struct file
*filp
)
119 teedev_close_context(filp
->private_data
);
123 static int tee_ioctl_version(struct tee_context
*ctx
,
124 struct tee_ioctl_version_data __user
*uvers
)
126 struct tee_ioctl_version_data vers
;
128 ctx
->teedev
->desc
->ops
->get_version(ctx
->teedev
, &vers
);
130 if (ctx
->teedev
->desc
->flags
& TEE_DESC_PRIVILEGED
)
131 vers
.gen_caps
|= TEE_GEN_CAP_PRIVILEGED
;
133 if (copy_to_user(uvers
, &vers
, sizeof(vers
)))
139 static int tee_ioctl_shm_alloc(struct tee_context
*ctx
,
140 struct tee_ioctl_shm_alloc_data __user
*udata
)
143 struct tee_ioctl_shm_alloc_data data
;
146 if (copy_from_user(&data
, udata
, sizeof(data
)))
149 /* Currently no input flags are supported */
153 shm
= tee_shm_alloc(ctx
, data
.size
, TEE_SHM_MAPPED
| TEE_SHM_DMA_BUF
);
158 data
.flags
= shm
->flags
;
159 data
.size
= shm
->size
;
161 if (copy_to_user(udata
, &data
, sizeof(data
)))
164 ret
= tee_shm_get_fd(shm
);
167 * When user space closes the file descriptor the shared memory
168 * should be freed or if tee_shm_get_fd() failed then it will
169 * be freed immediately.
176 tee_ioctl_shm_register(struct tee_context
*ctx
,
177 struct tee_ioctl_shm_register_data __user
*udata
)
180 struct tee_ioctl_shm_register_data data
;
183 if (copy_from_user(&data
, udata
, sizeof(data
)))
186 /* Currently no input flags are supported */
190 shm
= tee_shm_register(ctx
, data
.addr
, data
.length
,
191 TEE_SHM_DMA_BUF
| TEE_SHM_USER_MAPPED
);
196 data
.flags
= shm
->flags
;
197 data
.length
= shm
->size
;
199 if (copy_to_user(udata
, &data
, sizeof(data
)))
202 ret
= tee_shm_get_fd(shm
);
204 * When user space closes the file descriptor the shared memory
205 * should be freed or if tee_shm_get_fd() failed then it will
206 * be freed immediately.
212 static int params_from_user(struct tee_context
*ctx
, struct tee_param
*params
,
214 struct tee_ioctl_param __user
*uparams
)
218 for (n
= 0; n
< num_params
; n
++) {
220 struct tee_ioctl_param ip
;
222 if (copy_from_user(&ip
, uparams
+ n
, sizeof(ip
)))
225 /* All unused attribute bits has to be zero */
226 if (ip
.attr
& ~TEE_IOCTL_PARAM_ATTR_MASK
)
229 params
[n
].attr
= ip
.attr
;
230 switch (ip
.attr
& TEE_IOCTL_PARAM_ATTR_TYPE_MASK
) {
231 case TEE_IOCTL_PARAM_ATTR_TYPE_NONE
:
232 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT
:
234 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT
:
235 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
236 params
[n
].u
.value
.a
= ip
.a
;
237 params
[n
].u
.value
.b
= ip
.b
;
238 params
[n
].u
.value
.c
= ip
.c
;
240 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT
:
241 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
242 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
244 * If we fail to get a pointer to a shared memory
245 * object (and increase the ref count) from an
246 * identifier we return an error. All pointers that
247 * has been added in params have an increased ref
248 * count. It's the callers responibility to do
249 * tee_shm_put() on all resolved pointers.
251 shm
= tee_shm_get_from_id(ctx
, ip
.c
);
256 * Ensure offset + size does not overflow offset
257 * and does not overflow the size of the referred
258 * shared memory object.
260 if ((ip
.a
+ ip
.b
) < ip
.a
||
261 (ip
.a
+ ip
.b
) > shm
->size
) {
266 params
[n
].u
.memref
.shm_offs
= ip
.a
;
267 params
[n
].u
.memref
.size
= ip
.b
;
268 params
[n
].u
.memref
.shm
= shm
;
271 /* Unknown attribute */
278 static int params_to_user(struct tee_ioctl_param __user
*uparams
,
279 size_t num_params
, struct tee_param
*params
)
283 for (n
= 0; n
< num_params
; n
++) {
284 struct tee_ioctl_param __user
*up
= uparams
+ n
;
285 struct tee_param
*p
= params
+ n
;
288 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT
:
289 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
290 if (put_user(p
->u
.value
.a
, &up
->a
) ||
291 put_user(p
->u
.value
.b
, &up
->b
) ||
292 put_user(p
->u
.value
.c
, &up
->c
))
295 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
296 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
297 if (put_user((u64
)p
->u
.memref
.size
, &up
->b
))
306 static int tee_ioctl_open_session(struct tee_context
*ctx
,
307 struct tee_ioctl_buf_data __user
*ubuf
)
311 struct tee_ioctl_buf_data buf
;
312 struct tee_ioctl_open_session_arg __user
*uarg
;
313 struct tee_ioctl_open_session_arg arg
;
314 struct tee_ioctl_param __user
*uparams
= NULL
;
315 struct tee_param
*params
= NULL
;
316 bool have_session
= false;
318 if (!ctx
->teedev
->desc
->ops
->open_session
)
321 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
324 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
325 buf
.buf_len
< sizeof(struct tee_ioctl_open_session_arg
))
328 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
329 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
332 if (sizeof(arg
) + TEE_IOCTL_PARAM_SIZE(arg
.num_params
) != buf
.buf_len
)
335 if (arg
.num_params
) {
336 params
= kcalloc(arg
.num_params
, sizeof(struct tee_param
),
340 uparams
= uarg
->params
;
341 rc
= params_from_user(ctx
, params
, arg
.num_params
, uparams
);
346 rc
= ctx
->teedev
->desc
->ops
->open_session(ctx
, &arg
, params
);
351 if (put_user(arg
.session
, &uarg
->session
) ||
352 put_user(arg
.ret
, &uarg
->ret
) ||
353 put_user(arg
.ret_origin
, &uarg
->ret_origin
)) {
357 rc
= params_to_user(uparams
, arg
.num_params
, params
);
360 * If we've succeeded to open the session but failed to communicate
361 * it back to user space, close the session again to avoid leakage.
363 if (rc
&& have_session
&& ctx
->teedev
->desc
->ops
->close_session
)
364 ctx
->teedev
->desc
->ops
->close_session(ctx
, arg
.session
);
367 /* Decrease ref count for all valid shared memory pointers */
368 for (n
= 0; n
< arg
.num_params
; n
++)
369 if (tee_param_is_memref(params
+ n
) &&
370 params
[n
].u
.memref
.shm
)
371 tee_shm_put(params
[n
].u
.memref
.shm
);
378 static int tee_ioctl_invoke(struct tee_context
*ctx
,
379 struct tee_ioctl_buf_data __user
*ubuf
)
383 struct tee_ioctl_buf_data buf
;
384 struct tee_ioctl_invoke_arg __user
*uarg
;
385 struct tee_ioctl_invoke_arg arg
;
386 struct tee_ioctl_param __user
*uparams
= NULL
;
387 struct tee_param
*params
= NULL
;
389 if (!ctx
->teedev
->desc
->ops
->invoke_func
)
392 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
395 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
396 buf
.buf_len
< sizeof(struct tee_ioctl_invoke_arg
))
399 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
400 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
403 if (sizeof(arg
) + TEE_IOCTL_PARAM_SIZE(arg
.num_params
) != buf
.buf_len
)
406 if (arg
.num_params
) {
407 params
= kcalloc(arg
.num_params
, sizeof(struct tee_param
),
411 uparams
= uarg
->params
;
412 rc
= params_from_user(ctx
, params
, arg
.num_params
, uparams
);
417 rc
= ctx
->teedev
->desc
->ops
->invoke_func(ctx
, &arg
, params
);
421 if (put_user(arg
.ret
, &uarg
->ret
) ||
422 put_user(arg
.ret_origin
, &uarg
->ret_origin
)) {
426 rc
= params_to_user(uparams
, arg
.num_params
, params
);
429 /* Decrease ref count for all valid shared memory pointers */
430 for (n
= 0; n
< arg
.num_params
; n
++)
431 if (tee_param_is_memref(params
+ n
) &&
432 params
[n
].u
.memref
.shm
)
433 tee_shm_put(params
[n
].u
.memref
.shm
);
439 static int tee_ioctl_cancel(struct tee_context
*ctx
,
440 struct tee_ioctl_cancel_arg __user
*uarg
)
442 struct tee_ioctl_cancel_arg arg
;
444 if (!ctx
->teedev
->desc
->ops
->cancel_req
)
447 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
450 return ctx
->teedev
->desc
->ops
->cancel_req(ctx
, arg
.cancel_id
,
455 tee_ioctl_close_session(struct tee_context
*ctx
,
456 struct tee_ioctl_close_session_arg __user
*uarg
)
458 struct tee_ioctl_close_session_arg arg
;
460 if (!ctx
->teedev
->desc
->ops
->close_session
)
463 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
466 return ctx
->teedev
->desc
->ops
->close_session(ctx
, arg
.session
);
469 static int params_to_supp(struct tee_context
*ctx
,
470 struct tee_ioctl_param __user
*uparams
,
471 size_t num_params
, struct tee_param
*params
)
475 for (n
= 0; n
< num_params
; n
++) {
476 struct tee_ioctl_param ip
;
477 struct tee_param
*p
= params
+ n
;
480 switch (p
->attr
& TEE_IOCTL_PARAM_ATTR_TYPE_MASK
) {
481 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT
:
482 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
487 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT
:
488 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
489 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
490 ip
.b
= p
->u
.memref
.size
;
491 if (!p
->u
.memref
.shm
) {
493 ip
.c
= (u64
)-1; /* invalid shm id */
496 ip
.a
= p
->u
.memref
.shm_offs
;
497 ip
.c
= p
->u
.memref
.shm
->id
;
506 if (copy_to_user(uparams
+ n
, &ip
, sizeof(ip
)))
513 static int tee_ioctl_supp_recv(struct tee_context
*ctx
,
514 struct tee_ioctl_buf_data __user
*ubuf
)
517 struct tee_ioctl_buf_data buf
;
518 struct tee_iocl_supp_recv_arg __user
*uarg
;
519 struct tee_param
*params
;
523 if (!ctx
->teedev
->desc
->ops
->supp_recv
)
526 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
529 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
530 buf
.buf_len
< sizeof(struct tee_iocl_supp_recv_arg
))
533 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
534 if (get_user(num_params
, &uarg
->num_params
))
537 if (sizeof(*uarg
) + TEE_IOCTL_PARAM_SIZE(num_params
) != buf
.buf_len
)
540 params
= kcalloc(num_params
, sizeof(struct tee_param
), GFP_KERNEL
);
544 rc
= params_from_user(ctx
, params
, num_params
, uarg
->params
);
548 rc
= ctx
->teedev
->desc
->ops
->supp_recv(ctx
, &func
, &num_params
, params
);
552 if (put_user(func
, &uarg
->func
) ||
553 put_user(num_params
, &uarg
->num_params
)) {
558 rc
= params_to_supp(ctx
, uarg
->params
, num_params
, params
);
564 static int params_from_supp(struct tee_param
*params
, size_t num_params
,
565 struct tee_ioctl_param __user
*uparams
)
569 for (n
= 0; n
< num_params
; n
++) {
570 struct tee_param
*p
= params
+ n
;
571 struct tee_ioctl_param ip
;
573 if (copy_from_user(&ip
, uparams
+ n
, sizeof(ip
)))
576 /* All unused attribute bits has to be zero */
577 if (ip
.attr
& ~TEE_IOCTL_PARAM_ATTR_MASK
)
581 switch (ip
.attr
& TEE_IOCTL_PARAM_ATTR_TYPE_MASK
) {
582 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT
:
583 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
584 /* Only out and in/out values can be updated */
589 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
590 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
592 * Only the size of the memref can be updated.
593 * Since we don't have access to the original
594 * parameters here, only store the supplied size.
595 * The driver will copy the updated size into the
596 * original parameters.
598 p
->u
.memref
.shm
= NULL
;
599 p
->u
.memref
.shm_offs
= 0;
600 p
->u
.memref
.size
= ip
.b
;
603 memset(&p
->u
, 0, sizeof(p
->u
));
610 static int tee_ioctl_supp_send(struct tee_context
*ctx
,
611 struct tee_ioctl_buf_data __user
*ubuf
)
614 struct tee_ioctl_buf_data buf
;
615 struct tee_iocl_supp_send_arg __user
*uarg
;
616 struct tee_param
*params
;
620 /* Not valid for this driver */
621 if (!ctx
->teedev
->desc
->ops
->supp_send
)
624 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
627 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
628 buf
.buf_len
< sizeof(struct tee_iocl_supp_send_arg
))
631 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
632 if (get_user(ret
, &uarg
->ret
) ||
633 get_user(num_params
, &uarg
->num_params
))
636 if (sizeof(*uarg
) + TEE_IOCTL_PARAM_SIZE(num_params
) > buf
.buf_len
)
639 params
= kcalloc(num_params
, sizeof(struct tee_param
), GFP_KERNEL
);
643 rc
= params_from_supp(params
, num_params
, uarg
->params
);
647 rc
= ctx
->teedev
->desc
->ops
->supp_send(ctx
, ret
, num_params
, params
);
653 static long tee_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
655 struct tee_context
*ctx
= filp
->private_data
;
656 void __user
*uarg
= (void __user
*)arg
;
659 case TEE_IOC_VERSION
:
660 return tee_ioctl_version(ctx
, uarg
);
661 case TEE_IOC_SHM_ALLOC
:
662 return tee_ioctl_shm_alloc(ctx
, uarg
);
663 case TEE_IOC_SHM_REGISTER
:
664 return tee_ioctl_shm_register(ctx
, uarg
);
665 case TEE_IOC_OPEN_SESSION
:
666 return tee_ioctl_open_session(ctx
, uarg
);
668 return tee_ioctl_invoke(ctx
, uarg
);
670 return tee_ioctl_cancel(ctx
, uarg
);
671 case TEE_IOC_CLOSE_SESSION
:
672 return tee_ioctl_close_session(ctx
, uarg
);
673 case TEE_IOC_SUPPL_RECV
:
674 return tee_ioctl_supp_recv(ctx
, uarg
);
675 case TEE_IOC_SUPPL_SEND
:
676 return tee_ioctl_supp_send(ctx
, uarg
);
682 static const struct file_operations tee_fops
= {
683 .owner
= THIS_MODULE
,
685 .release
= tee_release
,
686 .unlocked_ioctl
= tee_ioctl
,
687 .compat_ioctl
= tee_ioctl
,
690 static void tee_release_device(struct device
*dev
)
692 struct tee_device
*teedev
= container_of(dev
, struct tee_device
, dev
);
694 spin_lock(&driver_lock
);
695 clear_bit(teedev
->id
, dev_mask
);
696 spin_unlock(&driver_lock
);
697 mutex_destroy(&teedev
->mutex
);
698 idr_destroy(&teedev
->idr
);
703 * tee_device_alloc() - Allocate a new struct tee_device instance
704 * @teedesc: Descriptor for this driver
705 * @dev: Parent device for this device
706 * @pool: Shared memory pool, NULL if not used
707 * @driver_data: Private driver data for this device
709 * Allocates a new struct tee_device instance. The device is
710 * removed by tee_device_unregister().
712 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
714 struct tee_device
*tee_device_alloc(const struct tee_desc
*teedesc
,
716 struct tee_shm_pool
*pool
,
719 struct tee_device
*teedev
;
724 if (!teedesc
|| !teedesc
->name
|| !teedesc
->ops
||
725 !teedesc
->ops
->get_version
|| !teedesc
->ops
->open
||
726 !teedesc
->ops
->release
|| !pool
)
727 return ERR_PTR(-EINVAL
);
729 teedev
= kzalloc(sizeof(*teedev
), GFP_KERNEL
);
731 ret
= ERR_PTR(-ENOMEM
);
735 max_id
= TEE_NUM_DEVICES
/ 2;
737 if (teedesc
->flags
& TEE_DESC_PRIVILEGED
) {
738 offs
= TEE_NUM_DEVICES
/ 2;
739 max_id
= TEE_NUM_DEVICES
;
742 spin_lock(&driver_lock
);
743 teedev
->id
= find_next_zero_bit(dev_mask
, max_id
, offs
);
744 if (teedev
->id
< max_id
)
745 set_bit(teedev
->id
, dev_mask
);
746 spin_unlock(&driver_lock
);
748 if (teedev
->id
>= max_id
) {
749 ret
= ERR_PTR(-ENOMEM
);
753 snprintf(teedev
->name
, sizeof(teedev
->name
), "tee%s%d",
754 teedesc
->flags
& TEE_DESC_PRIVILEGED
? "priv" : "",
757 teedev
->dev
.class = tee_class
;
758 teedev
->dev
.release
= tee_release_device
;
759 teedev
->dev
.parent
= dev
;
761 teedev
->dev
.devt
= MKDEV(MAJOR(tee_devt
), teedev
->id
);
763 rc
= dev_set_name(&teedev
->dev
, "%s", teedev
->name
);
769 cdev_init(&teedev
->cdev
, &tee_fops
);
770 teedev
->cdev
.owner
= teedesc
->owner
;
771 teedev
->cdev
.kobj
.parent
= &teedev
->dev
.kobj
;
773 dev_set_drvdata(&teedev
->dev
, driver_data
);
774 device_initialize(&teedev
->dev
);
776 /* 1 as tee_device_unregister() does one final tee_device_put() */
777 teedev
->num_users
= 1;
778 init_completion(&teedev
->c_no_users
);
779 mutex_init(&teedev
->mutex
);
780 idr_init(&teedev
->idr
);
782 teedev
->desc
= teedesc
;
787 unregister_chrdev_region(teedev
->dev
.devt
, 1);
789 pr_err("could not register %s driver\n",
790 teedesc
->flags
& TEE_DESC_PRIVILEGED
? "privileged" : "client");
791 if (teedev
&& teedev
->id
< TEE_NUM_DEVICES
) {
792 spin_lock(&driver_lock
);
793 clear_bit(teedev
->id
, dev_mask
);
794 spin_unlock(&driver_lock
);
799 EXPORT_SYMBOL_GPL(tee_device_alloc
);
801 static ssize_t
implementation_id_show(struct device
*dev
,
802 struct device_attribute
*attr
, char *buf
)
804 struct tee_device
*teedev
= container_of(dev
, struct tee_device
, dev
);
805 struct tee_ioctl_version_data vers
;
807 teedev
->desc
->ops
->get_version(teedev
, &vers
);
808 return scnprintf(buf
, PAGE_SIZE
, "%d\n", vers
.impl_id
);
810 static DEVICE_ATTR_RO(implementation_id
);
812 static struct attribute
*tee_dev_attrs
[] = {
813 &dev_attr_implementation_id
.attr
,
817 static const struct attribute_group tee_dev_group
= {
818 .attrs
= tee_dev_attrs
,
822 * tee_device_register() - Registers a TEE device
823 * @teedev: Device to register
825 * tee_device_unregister() need to be called to remove the @teedev if
826 * this function fails.
828 * @returns < 0 on failure
830 int tee_device_register(struct tee_device
*teedev
)
834 if (teedev
->flags
& TEE_DEVICE_FLAG_REGISTERED
) {
835 dev_err(&teedev
->dev
, "attempt to register twice\n");
839 rc
= cdev_add(&teedev
->cdev
, teedev
->dev
.devt
, 1);
841 dev_err(&teedev
->dev
,
842 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
843 teedev
->name
, MAJOR(teedev
->dev
.devt
),
844 MINOR(teedev
->dev
.devt
), rc
);
848 rc
= device_add(&teedev
->dev
);
850 dev_err(&teedev
->dev
,
851 "unable to device_add() %s, major %d, minor %d, err=%d\n",
852 teedev
->name
, MAJOR(teedev
->dev
.devt
),
853 MINOR(teedev
->dev
.devt
), rc
);
857 rc
= sysfs_create_group(&teedev
->dev
.kobj
, &tee_dev_group
);
859 dev_err(&teedev
->dev
,
860 "failed to create sysfs attributes, err=%d\n", rc
);
861 goto err_sysfs_create_group
;
864 teedev
->flags
|= TEE_DEVICE_FLAG_REGISTERED
;
867 err_sysfs_create_group
:
868 device_del(&teedev
->dev
);
870 cdev_del(&teedev
->cdev
);
873 EXPORT_SYMBOL_GPL(tee_device_register
);
875 void tee_device_put(struct tee_device
*teedev
)
877 mutex_lock(&teedev
->mutex
);
878 /* Shouldn't put in this state */
879 if (!WARN_ON(!teedev
->desc
)) {
881 if (!teedev
->num_users
) {
883 complete(&teedev
->c_no_users
);
886 mutex_unlock(&teedev
->mutex
);
889 bool tee_device_get(struct tee_device
*teedev
)
891 mutex_lock(&teedev
->mutex
);
893 mutex_unlock(&teedev
->mutex
);
897 mutex_unlock(&teedev
->mutex
);
902 * tee_device_unregister() - Removes a TEE device
903 * @teedev: Device to unregister
905 * This function should be called to remove the @teedev even if
906 * tee_device_register() hasn't been called yet. Does nothing if
909 void tee_device_unregister(struct tee_device
*teedev
)
914 if (teedev
->flags
& TEE_DEVICE_FLAG_REGISTERED
) {
915 sysfs_remove_group(&teedev
->dev
.kobj
, &tee_dev_group
);
916 cdev_del(&teedev
->cdev
);
917 device_del(&teedev
->dev
);
920 tee_device_put(teedev
);
921 wait_for_completion(&teedev
->c_no_users
);
924 * No need to take a mutex any longer now since teedev->desc was
925 * set to NULL before teedev->c_no_users was completed.
930 put_device(&teedev
->dev
);
932 EXPORT_SYMBOL_GPL(tee_device_unregister
);
935 * tee_get_drvdata() - Return driver_data pointer
936 * @teedev: Device containing the driver_data pointer
937 * @returns the driver_data pointer supplied to tee_register().
939 void *tee_get_drvdata(struct tee_device
*teedev
)
941 return dev_get_drvdata(&teedev
->dev
);
943 EXPORT_SYMBOL_GPL(tee_get_drvdata
);
945 struct match_dev_data
{
946 struct tee_ioctl_version_data
*vers
;
948 int (*match
)(struct tee_ioctl_version_data
*, const void *);
951 static int match_dev(struct device
*dev
, const void *data
)
953 const struct match_dev_data
*match_data
= data
;
954 struct tee_device
*teedev
= container_of(dev
, struct tee_device
, dev
);
956 teedev
->desc
->ops
->get_version(teedev
, match_data
->vers
);
957 return match_data
->match(match_data
->vers
, match_data
->data
);
961 tee_client_open_context(struct tee_context
*start
,
962 int (*match
)(struct tee_ioctl_version_data
*,
964 const void *data
, struct tee_ioctl_version_data
*vers
)
966 struct device
*dev
= NULL
;
967 struct device
*put_dev
= NULL
;
968 struct tee_context
*ctx
= NULL
;
969 struct tee_ioctl_version_data v
;
970 struct match_dev_data match_data
= { vers
? vers
: &v
, data
, match
};
973 dev
= &start
->teedev
->dev
;
976 dev
= class_find_device(tee_class
, dev
, &match_data
, match_dev
);
978 ctx
= ERR_PTR(-ENOENT
);
985 ctx
= teedev_open(container_of(dev
, struct tee_device
, dev
));
986 } while (IS_ERR(ctx
) && PTR_ERR(ctx
) != -ENOMEM
);
990 * Default behaviour for in kernel client is to not wait for
991 * tee-supplicant if not present for any requests in this context.
992 * Also this flag could be configured again before call to
993 * tee_client_open_session() if any in kernel client requires
994 * different behaviour.
997 ctx
->supp_nowait
= true;
1001 EXPORT_SYMBOL_GPL(tee_client_open_context
);
1003 void tee_client_close_context(struct tee_context
*ctx
)
1005 teedev_close_context(ctx
);
1007 EXPORT_SYMBOL_GPL(tee_client_close_context
);
1009 void tee_client_get_version(struct tee_context
*ctx
,
1010 struct tee_ioctl_version_data
*vers
)
1012 ctx
->teedev
->desc
->ops
->get_version(ctx
->teedev
, vers
);
1014 EXPORT_SYMBOL_GPL(tee_client_get_version
);
1016 int tee_client_open_session(struct tee_context
*ctx
,
1017 struct tee_ioctl_open_session_arg
*arg
,
1018 struct tee_param
*param
)
1020 if (!ctx
->teedev
->desc
->ops
->open_session
)
1022 return ctx
->teedev
->desc
->ops
->open_session(ctx
, arg
, param
);
1024 EXPORT_SYMBOL_GPL(tee_client_open_session
);
1026 int tee_client_close_session(struct tee_context
*ctx
, u32 session
)
1028 if (!ctx
->teedev
->desc
->ops
->close_session
)
1030 return ctx
->teedev
->desc
->ops
->close_session(ctx
, session
);
1032 EXPORT_SYMBOL_GPL(tee_client_close_session
);
1034 int tee_client_invoke_func(struct tee_context
*ctx
,
1035 struct tee_ioctl_invoke_arg
*arg
,
1036 struct tee_param
*param
)
1038 if (!ctx
->teedev
->desc
->ops
->invoke_func
)
1040 return ctx
->teedev
->desc
->ops
->invoke_func(ctx
, arg
, param
);
1042 EXPORT_SYMBOL_GPL(tee_client_invoke_func
);
1044 int tee_client_cancel_req(struct tee_context
*ctx
,
1045 struct tee_ioctl_cancel_arg
*arg
)
1047 if (!ctx
->teedev
->desc
->ops
->cancel_req
)
1049 return ctx
->teedev
->desc
->ops
->cancel_req(ctx
, arg
->cancel_id
,
1053 static int tee_client_device_match(struct device
*dev
,
1054 struct device_driver
*drv
)
1056 const struct tee_client_device_id
*id_table
;
1057 struct tee_client_device
*tee_device
;
1059 id_table
= to_tee_client_driver(drv
)->id_table
;
1060 tee_device
= to_tee_client_device(dev
);
1062 while (!uuid_is_null(&id_table
->uuid
)) {
1063 if (uuid_equal(&tee_device
->id
.uuid
, &id_table
->uuid
))
1071 static int tee_client_device_uevent(struct device
*dev
,
1072 struct kobj_uevent_env
*env
)
1074 uuid_t
*dev_id
= &to_tee_client_device(dev
)->id
.uuid
;
1076 return add_uevent_var(env
, "MODALIAS=tee:%pUb", dev_id
);
1079 struct bus_type tee_bus_type
= {
1081 .match
= tee_client_device_match
,
1082 .uevent
= tee_client_device_uevent
,
1084 EXPORT_SYMBOL_GPL(tee_bus_type
);
1086 static int __init
tee_init(void)
1090 tee_class
= class_create(THIS_MODULE
, "tee");
1091 if (IS_ERR(tee_class
)) {
1092 pr_err("couldn't create class\n");
1093 return PTR_ERR(tee_class
);
1096 rc
= alloc_chrdev_region(&tee_devt
, 0, TEE_NUM_DEVICES
, "tee");
1098 pr_err("failed to allocate char dev region\n");
1099 goto out_unreg_class
;
1102 rc
= bus_register(&tee_bus_type
);
1104 pr_err("failed to register tee bus\n");
1105 goto out_unreg_chrdev
;
1111 unregister_chrdev_region(tee_devt
, TEE_NUM_DEVICES
);
1113 class_destroy(tee_class
);
1119 static void __exit
tee_exit(void)
1121 bus_unregister(&tee_bus_type
);
1122 unregister_chrdev_region(tee_devt
, TEE_NUM_DEVICES
);
1123 class_destroy(tee_class
);
1127 subsys_initcall(tee_init
);
1128 module_exit(tee_exit
);
1130 MODULE_AUTHOR("Linaro");
1131 MODULE_DESCRIPTION("TEE Driver");
1132 MODULE_VERSION("1.0");
1133 MODULE_LICENSE("GPL v2");