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>
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
)
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
))
51 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
57 kref_init(&ctx
->refcount
);
59 INIT_LIST_HEAD(&ctx
->list_shm
);
60 filp
->private_data
= ctx
;
61 rc
= teedev
->desc
->ops
->open(ctx
);
68 tee_device_put(teedev
);
72 void teedev_ctx_get(struct tee_context
*ctx
)
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
,
84 ctx
->releasing
= true;
85 ctx
->teedev
->desc
->ops
->release(ctx
);
89 void teedev_ctx_put(struct tee_context
*ctx
)
94 kref_put(&ctx
->refcount
, teedev_ctx_release
);
97 static void teedev_close_context(struct tee_context
*ctx
)
99 tee_device_put(ctx
->teedev
);
103 static int tee_release(struct inode
*inode
, struct file
*filp
)
105 teedev_close_context(filp
->private_data
);
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
)))
125 static int tee_ioctl_shm_alloc(struct tee_context
*ctx
,
126 struct tee_ioctl_shm_alloc_data __user
*udata
)
129 struct tee_ioctl_shm_alloc_data data
;
132 if (copy_from_user(&data
, udata
, sizeof(data
)))
135 /* Currently no input flags are supported */
139 shm
= tee_shm_alloc(ctx
, data
.size
, TEE_SHM_MAPPED
| TEE_SHM_DMA_BUF
);
144 data
.flags
= shm
->flags
;
145 data
.size
= shm
->size
;
147 if (copy_to_user(udata
, &data
, sizeof(data
)))
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.
162 tee_ioctl_shm_register(struct tee_context
*ctx
,
163 struct tee_ioctl_shm_register_data __user
*udata
)
166 struct tee_ioctl_shm_register_data data
;
169 if (copy_from_user(&data
, udata
, sizeof(data
)))
172 /* Currently no input flags are supported */
176 shm
= tee_shm_register(ctx
, data
.addr
, data
.length
,
177 TEE_SHM_DMA_BUF
| TEE_SHM_USER_MAPPED
);
182 data
.flags
= shm
->flags
;
183 data
.length
= shm
->size
;
185 if (copy_to_user(udata
, &data
, sizeof(data
)))
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.
198 static int params_from_user(struct tee_context
*ctx
, struct tee_param
*params
,
200 struct tee_ioctl_param __user
*uparams
)
204 for (n
= 0; n
< num_params
; n
++) {
206 struct tee_ioctl_param ip
;
208 if (copy_from_user(&ip
, uparams
+ n
, sizeof(ip
)))
211 /* All unused attribute bits has to be zero */
212 if (ip
.attr
& ~TEE_IOCTL_PARAM_ATTR_MASK
)
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
:
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
;
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
);
242 * Ensure offset + size does not overflow offset
243 * and does not overflow the size of the referred
244 * shared memory object.
246 if ((ip
.a
+ ip
.b
) < ip
.a
||
247 (ip
.a
+ ip
.b
) > shm
->size
) {
252 params
[n
].u
.memref
.shm_offs
= ip
.a
;
253 params
[n
].u
.memref
.size
= ip
.b
;
254 params
[n
].u
.memref
.shm
= shm
;
257 /* Unknown attribute */
264 static int params_to_user(struct tee_ioctl_param __user
*uparams
,
265 size_t num_params
, struct tee_param
*params
)
269 for (n
= 0; n
< num_params
; n
++) {
270 struct tee_ioctl_param __user
*up
= uparams
+ n
;
271 struct tee_param
*p
= params
+ n
;
274 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT
:
275 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
276 if (put_user(p
->u
.value
.a
, &up
->a
) ||
277 put_user(p
->u
.value
.b
, &up
->b
) ||
278 put_user(p
->u
.value
.c
, &up
->c
))
281 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
282 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
283 if (put_user((u64
)p
->u
.memref
.size
, &up
->b
))
292 static int tee_ioctl_open_session(struct tee_context
*ctx
,
293 struct tee_ioctl_buf_data __user
*ubuf
)
297 struct tee_ioctl_buf_data buf
;
298 struct tee_ioctl_open_session_arg __user
*uarg
;
299 struct tee_ioctl_open_session_arg arg
;
300 struct tee_ioctl_param __user
*uparams
= NULL
;
301 struct tee_param
*params
= NULL
;
302 bool have_session
= false;
304 if (!ctx
->teedev
->desc
->ops
->open_session
)
307 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
310 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
311 buf
.buf_len
< sizeof(struct tee_ioctl_open_session_arg
))
314 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
315 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
318 if (sizeof(arg
) + TEE_IOCTL_PARAM_SIZE(arg
.num_params
) != buf
.buf_len
)
321 if (arg
.num_params
) {
322 params
= kcalloc(arg
.num_params
, sizeof(struct tee_param
),
326 uparams
= uarg
->params
;
327 rc
= params_from_user(ctx
, params
, arg
.num_params
, uparams
);
332 rc
= ctx
->teedev
->desc
->ops
->open_session(ctx
, &arg
, params
);
337 if (put_user(arg
.session
, &uarg
->session
) ||
338 put_user(arg
.ret
, &uarg
->ret
) ||
339 put_user(arg
.ret_origin
, &uarg
->ret_origin
)) {
343 rc
= params_to_user(uparams
, arg
.num_params
, params
);
346 * If we've succeeded to open the session but failed to communicate
347 * it back to user space, close the session again to avoid leakage.
349 if (rc
&& have_session
&& ctx
->teedev
->desc
->ops
->close_session
)
350 ctx
->teedev
->desc
->ops
->close_session(ctx
, arg
.session
);
353 /* Decrease ref count for all valid shared memory pointers */
354 for (n
= 0; n
< arg
.num_params
; n
++)
355 if (tee_param_is_memref(params
+ n
) &&
356 params
[n
].u
.memref
.shm
)
357 tee_shm_put(params
[n
].u
.memref
.shm
);
364 static int tee_ioctl_invoke(struct tee_context
*ctx
,
365 struct tee_ioctl_buf_data __user
*ubuf
)
369 struct tee_ioctl_buf_data buf
;
370 struct tee_ioctl_invoke_arg __user
*uarg
;
371 struct tee_ioctl_invoke_arg arg
;
372 struct tee_ioctl_param __user
*uparams
= NULL
;
373 struct tee_param
*params
= NULL
;
375 if (!ctx
->teedev
->desc
->ops
->invoke_func
)
378 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
381 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
382 buf
.buf_len
< sizeof(struct tee_ioctl_invoke_arg
))
385 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
386 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
389 if (sizeof(arg
) + TEE_IOCTL_PARAM_SIZE(arg
.num_params
) != buf
.buf_len
)
392 if (arg
.num_params
) {
393 params
= kcalloc(arg
.num_params
, sizeof(struct tee_param
),
397 uparams
= uarg
->params
;
398 rc
= params_from_user(ctx
, params
, arg
.num_params
, uparams
);
403 rc
= ctx
->teedev
->desc
->ops
->invoke_func(ctx
, &arg
, params
);
407 if (put_user(arg
.ret
, &uarg
->ret
) ||
408 put_user(arg
.ret_origin
, &uarg
->ret_origin
)) {
412 rc
= params_to_user(uparams
, arg
.num_params
, params
);
415 /* Decrease ref count for all valid shared memory pointers */
416 for (n
= 0; n
< arg
.num_params
; n
++)
417 if (tee_param_is_memref(params
+ n
) &&
418 params
[n
].u
.memref
.shm
)
419 tee_shm_put(params
[n
].u
.memref
.shm
);
425 static int tee_ioctl_cancel(struct tee_context
*ctx
,
426 struct tee_ioctl_cancel_arg __user
*uarg
)
428 struct tee_ioctl_cancel_arg arg
;
430 if (!ctx
->teedev
->desc
->ops
->cancel_req
)
433 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
436 return ctx
->teedev
->desc
->ops
->cancel_req(ctx
, arg
.cancel_id
,
441 tee_ioctl_close_session(struct tee_context
*ctx
,
442 struct tee_ioctl_close_session_arg __user
*uarg
)
444 struct tee_ioctl_close_session_arg arg
;
446 if (!ctx
->teedev
->desc
->ops
->close_session
)
449 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
452 return ctx
->teedev
->desc
->ops
->close_session(ctx
, arg
.session
);
455 static int params_to_supp(struct tee_context
*ctx
,
456 struct tee_ioctl_param __user
*uparams
,
457 size_t num_params
, struct tee_param
*params
)
461 for (n
= 0; n
< num_params
; n
++) {
462 struct tee_ioctl_param ip
;
463 struct tee_param
*p
= params
+ n
;
466 switch (p
->attr
& TEE_IOCTL_PARAM_ATTR_TYPE_MASK
) {
467 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT
:
468 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
473 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT
:
474 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
475 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
476 ip
.b
= p
->u
.memref
.size
;
477 if (!p
->u
.memref
.shm
) {
479 ip
.c
= (u64
)-1; /* invalid shm id */
482 ip
.a
= p
->u
.memref
.shm_offs
;
483 ip
.c
= p
->u
.memref
.shm
->id
;
492 if (copy_to_user(uparams
+ n
, &ip
, sizeof(ip
)))
499 static int tee_ioctl_supp_recv(struct tee_context
*ctx
,
500 struct tee_ioctl_buf_data __user
*ubuf
)
503 struct tee_ioctl_buf_data buf
;
504 struct tee_iocl_supp_recv_arg __user
*uarg
;
505 struct tee_param
*params
;
509 if (!ctx
->teedev
->desc
->ops
->supp_recv
)
512 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
515 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
516 buf
.buf_len
< sizeof(struct tee_iocl_supp_recv_arg
))
519 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
520 if (get_user(num_params
, &uarg
->num_params
))
523 if (sizeof(*uarg
) + TEE_IOCTL_PARAM_SIZE(num_params
) != buf
.buf_len
)
526 params
= kcalloc(num_params
, sizeof(struct tee_param
), GFP_KERNEL
);
530 rc
= params_from_user(ctx
, params
, num_params
, uarg
->params
);
534 rc
= ctx
->teedev
->desc
->ops
->supp_recv(ctx
, &func
, &num_params
, params
);
538 if (put_user(func
, &uarg
->func
) ||
539 put_user(num_params
, &uarg
->num_params
)) {
544 rc
= params_to_supp(ctx
, uarg
->params
, num_params
, params
);
550 static int params_from_supp(struct tee_param
*params
, size_t num_params
,
551 struct tee_ioctl_param __user
*uparams
)
555 for (n
= 0; n
< num_params
; n
++) {
556 struct tee_param
*p
= params
+ n
;
557 struct tee_ioctl_param ip
;
559 if (copy_from_user(&ip
, uparams
+ n
, sizeof(ip
)))
562 /* All unused attribute bits has to be zero */
563 if (ip
.attr
& ~TEE_IOCTL_PARAM_ATTR_MASK
)
567 switch (ip
.attr
& TEE_IOCTL_PARAM_ATTR_TYPE_MASK
) {
568 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT
:
569 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
570 /* Only out and in/out values can be updated */
575 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
576 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
578 * Only the size of the memref can be updated.
579 * Since we don't have access to the original
580 * parameters here, only store the supplied size.
581 * The driver will copy the updated size into the
582 * original parameters.
584 p
->u
.memref
.shm
= NULL
;
585 p
->u
.memref
.shm_offs
= 0;
586 p
->u
.memref
.size
= ip
.b
;
589 memset(&p
->u
, 0, sizeof(p
->u
));
596 static int tee_ioctl_supp_send(struct tee_context
*ctx
,
597 struct tee_ioctl_buf_data __user
*ubuf
)
600 struct tee_ioctl_buf_data buf
;
601 struct tee_iocl_supp_send_arg __user
*uarg
;
602 struct tee_param
*params
;
606 /* Not valid for this driver */
607 if (!ctx
->teedev
->desc
->ops
->supp_send
)
610 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
613 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
614 buf
.buf_len
< sizeof(struct tee_iocl_supp_send_arg
))
617 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
618 if (get_user(ret
, &uarg
->ret
) ||
619 get_user(num_params
, &uarg
->num_params
))
622 if (sizeof(*uarg
) + TEE_IOCTL_PARAM_SIZE(num_params
) > buf
.buf_len
)
625 params
= kcalloc(num_params
, sizeof(struct tee_param
), GFP_KERNEL
);
629 rc
= params_from_supp(params
, num_params
, uarg
->params
);
633 rc
= ctx
->teedev
->desc
->ops
->supp_send(ctx
, ret
, num_params
, params
);
639 static long tee_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
641 struct tee_context
*ctx
= filp
->private_data
;
642 void __user
*uarg
= (void __user
*)arg
;
645 case TEE_IOC_VERSION
:
646 return tee_ioctl_version(ctx
, uarg
);
647 case TEE_IOC_SHM_ALLOC
:
648 return tee_ioctl_shm_alloc(ctx
, uarg
);
649 case TEE_IOC_SHM_REGISTER
:
650 return tee_ioctl_shm_register(ctx
, uarg
);
651 case TEE_IOC_OPEN_SESSION
:
652 return tee_ioctl_open_session(ctx
, uarg
);
654 return tee_ioctl_invoke(ctx
, uarg
);
656 return tee_ioctl_cancel(ctx
, uarg
);
657 case TEE_IOC_CLOSE_SESSION
:
658 return tee_ioctl_close_session(ctx
, uarg
);
659 case TEE_IOC_SUPPL_RECV
:
660 return tee_ioctl_supp_recv(ctx
, uarg
);
661 case TEE_IOC_SUPPL_SEND
:
662 return tee_ioctl_supp_send(ctx
, uarg
);
668 static const struct file_operations tee_fops
= {
669 .owner
= THIS_MODULE
,
671 .release
= tee_release
,
672 .unlocked_ioctl
= tee_ioctl
,
673 .compat_ioctl
= tee_ioctl
,
676 static void tee_release_device(struct device
*dev
)
678 struct tee_device
*teedev
= container_of(dev
, struct tee_device
, dev
);
680 spin_lock(&driver_lock
);
681 clear_bit(teedev
->id
, dev_mask
);
682 spin_unlock(&driver_lock
);
683 mutex_destroy(&teedev
->mutex
);
684 idr_destroy(&teedev
->idr
);
689 * tee_device_alloc() - Allocate a new struct tee_device instance
690 * @teedesc: Descriptor for this driver
691 * @dev: Parent device for this device
692 * @pool: Shared memory pool, NULL if not used
693 * @driver_data: Private driver data for this device
695 * Allocates a new struct tee_device instance. The device is
696 * removed by tee_device_unregister().
698 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
700 struct tee_device
*tee_device_alloc(const struct tee_desc
*teedesc
,
702 struct tee_shm_pool
*pool
,
705 struct tee_device
*teedev
;
710 if (!teedesc
|| !teedesc
->name
|| !teedesc
->ops
||
711 !teedesc
->ops
->get_version
|| !teedesc
->ops
->open
||
712 !teedesc
->ops
->release
|| !pool
)
713 return ERR_PTR(-EINVAL
);
715 teedev
= kzalloc(sizeof(*teedev
), GFP_KERNEL
);
717 ret
= ERR_PTR(-ENOMEM
);
721 max_id
= TEE_NUM_DEVICES
/ 2;
723 if (teedesc
->flags
& TEE_DESC_PRIVILEGED
) {
724 offs
= TEE_NUM_DEVICES
/ 2;
725 max_id
= TEE_NUM_DEVICES
;
728 spin_lock(&driver_lock
);
729 teedev
->id
= find_next_zero_bit(dev_mask
, max_id
, offs
);
730 if (teedev
->id
< max_id
)
731 set_bit(teedev
->id
, dev_mask
);
732 spin_unlock(&driver_lock
);
734 if (teedev
->id
>= max_id
) {
735 ret
= ERR_PTR(-ENOMEM
);
739 snprintf(teedev
->name
, sizeof(teedev
->name
), "tee%s%d",
740 teedesc
->flags
& TEE_DESC_PRIVILEGED
? "priv" : "",
743 teedev
->dev
.class = tee_class
;
744 teedev
->dev
.release
= tee_release_device
;
745 teedev
->dev
.parent
= dev
;
747 teedev
->dev
.devt
= MKDEV(MAJOR(tee_devt
), teedev
->id
);
749 rc
= dev_set_name(&teedev
->dev
, "%s", teedev
->name
);
755 cdev_init(&teedev
->cdev
, &tee_fops
);
756 teedev
->cdev
.owner
= teedesc
->owner
;
757 teedev
->cdev
.kobj
.parent
= &teedev
->dev
.kobj
;
759 dev_set_drvdata(&teedev
->dev
, driver_data
);
760 device_initialize(&teedev
->dev
);
762 /* 1 as tee_device_unregister() does one final tee_device_put() */
763 teedev
->num_users
= 1;
764 init_completion(&teedev
->c_no_users
);
765 mutex_init(&teedev
->mutex
);
766 idr_init(&teedev
->idr
);
768 teedev
->desc
= teedesc
;
773 unregister_chrdev_region(teedev
->dev
.devt
, 1);
775 pr_err("could not register %s driver\n",
776 teedesc
->flags
& TEE_DESC_PRIVILEGED
? "privileged" : "client");
777 if (teedev
&& teedev
->id
< TEE_NUM_DEVICES
) {
778 spin_lock(&driver_lock
);
779 clear_bit(teedev
->id
, dev_mask
);
780 spin_unlock(&driver_lock
);
785 EXPORT_SYMBOL_GPL(tee_device_alloc
);
787 static ssize_t
implementation_id_show(struct device
*dev
,
788 struct device_attribute
*attr
, char *buf
)
790 struct tee_device
*teedev
= container_of(dev
, struct tee_device
, dev
);
791 struct tee_ioctl_version_data vers
;
793 teedev
->desc
->ops
->get_version(teedev
, &vers
);
794 return scnprintf(buf
, PAGE_SIZE
, "%d\n", vers
.impl_id
);
796 static DEVICE_ATTR_RO(implementation_id
);
798 static struct attribute
*tee_dev_attrs
[] = {
799 &dev_attr_implementation_id
.attr
,
803 static const struct attribute_group tee_dev_group
= {
804 .attrs
= tee_dev_attrs
,
808 * tee_device_register() - Registers a TEE device
809 * @teedev: Device to register
811 * tee_device_unregister() need to be called to remove the @teedev if
812 * this function fails.
814 * @returns < 0 on failure
816 int tee_device_register(struct tee_device
*teedev
)
820 if (teedev
->flags
& TEE_DEVICE_FLAG_REGISTERED
) {
821 dev_err(&teedev
->dev
, "attempt to register twice\n");
825 rc
= cdev_add(&teedev
->cdev
, teedev
->dev
.devt
, 1);
827 dev_err(&teedev
->dev
,
828 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
829 teedev
->name
, MAJOR(teedev
->dev
.devt
),
830 MINOR(teedev
->dev
.devt
), rc
);
834 rc
= device_add(&teedev
->dev
);
836 dev_err(&teedev
->dev
,
837 "unable to device_add() %s, major %d, minor %d, err=%d\n",
838 teedev
->name
, MAJOR(teedev
->dev
.devt
),
839 MINOR(teedev
->dev
.devt
), rc
);
843 rc
= sysfs_create_group(&teedev
->dev
.kobj
, &tee_dev_group
);
845 dev_err(&teedev
->dev
,
846 "failed to create sysfs attributes, err=%d\n", rc
);
847 goto err_sysfs_create_group
;
850 teedev
->flags
|= TEE_DEVICE_FLAG_REGISTERED
;
853 err_sysfs_create_group
:
854 device_del(&teedev
->dev
);
856 cdev_del(&teedev
->cdev
);
859 EXPORT_SYMBOL_GPL(tee_device_register
);
861 void tee_device_put(struct tee_device
*teedev
)
863 mutex_lock(&teedev
->mutex
);
864 /* Shouldn't put in this state */
865 if (!WARN_ON(!teedev
->desc
)) {
867 if (!teedev
->num_users
) {
869 complete(&teedev
->c_no_users
);
872 mutex_unlock(&teedev
->mutex
);
875 bool tee_device_get(struct tee_device
*teedev
)
877 mutex_lock(&teedev
->mutex
);
879 mutex_unlock(&teedev
->mutex
);
883 mutex_unlock(&teedev
->mutex
);
888 * tee_device_unregister() - Removes a TEE device
889 * @teedev: Device to unregister
891 * This function should be called to remove the @teedev even if
892 * tee_device_register() hasn't been called yet. Does nothing if
895 void tee_device_unregister(struct tee_device
*teedev
)
900 if (teedev
->flags
& TEE_DEVICE_FLAG_REGISTERED
) {
901 sysfs_remove_group(&teedev
->dev
.kobj
, &tee_dev_group
);
902 cdev_del(&teedev
->cdev
);
903 device_del(&teedev
->dev
);
906 tee_device_put(teedev
);
907 wait_for_completion(&teedev
->c_no_users
);
910 * No need to take a mutex any longer now since teedev->desc was
911 * set to NULL before teedev->c_no_users was completed.
916 put_device(&teedev
->dev
);
918 EXPORT_SYMBOL_GPL(tee_device_unregister
);
921 * tee_get_drvdata() - Return driver_data pointer
922 * @teedev: Device containing the driver_data pointer
923 * @returns the driver_data pointer supplied to tee_register().
925 void *tee_get_drvdata(struct tee_device
*teedev
)
927 return dev_get_drvdata(&teedev
->dev
);
929 EXPORT_SYMBOL_GPL(tee_get_drvdata
);
931 static int __init
tee_init(void)
935 tee_class
= class_create(THIS_MODULE
, "tee");
936 if (IS_ERR(tee_class
)) {
937 pr_err("couldn't create class\n");
938 return PTR_ERR(tee_class
);
941 rc
= alloc_chrdev_region(&tee_devt
, 0, TEE_NUM_DEVICES
, "tee");
943 pr_err("failed to allocate char dev region\n");
944 class_destroy(tee_class
);
951 static void __exit
tee_exit(void)
953 class_destroy(tee_class
);
955 unregister_chrdev_region(tee_devt
, TEE_NUM_DEVICES
);
958 subsys_initcall(tee_init
);
959 module_exit(tee_exit
);
961 MODULE_AUTHOR("Linaro");
962 MODULE_DESCRIPTION("TEE Driver");
963 MODULE_VERSION("1.0");
964 MODULE_LICENSE("GPL v2");