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
);
241 params
[n
].u
.memref
.shm_offs
= ip
.a
;
242 params
[n
].u
.memref
.size
= ip
.b
;
243 params
[n
].u
.memref
.shm
= shm
;
246 /* Unknown attribute */
253 static int params_to_user(struct tee_ioctl_param __user
*uparams
,
254 size_t num_params
, struct tee_param
*params
)
258 for (n
= 0; n
< num_params
; n
++) {
259 struct tee_ioctl_param __user
*up
= uparams
+ n
;
260 struct tee_param
*p
= params
+ n
;
263 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT
:
264 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
265 if (put_user(p
->u
.value
.a
, &up
->a
) ||
266 put_user(p
->u
.value
.b
, &up
->b
) ||
267 put_user(p
->u
.value
.c
, &up
->c
))
270 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
271 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
272 if (put_user((u64
)p
->u
.memref
.size
, &up
->b
))
281 static int tee_ioctl_open_session(struct tee_context
*ctx
,
282 struct tee_ioctl_buf_data __user
*ubuf
)
286 struct tee_ioctl_buf_data buf
;
287 struct tee_ioctl_open_session_arg __user
*uarg
;
288 struct tee_ioctl_open_session_arg arg
;
289 struct tee_ioctl_param __user
*uparams
= NULL
;
290 struct tee_param
*params
= NULL
;
291 bool have_session
= false;
293 if (!ctx
->teedev
->desc
->ops
->open_session
)
296 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
299 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
300 buf
.buf_len
< sizeof(struct tee_ioctl_open_session_arg
))
303 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
304 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
307 if (sizeof(arg
) + TEE_IOCTL_PARAM_SIZE(arg
.num_params
) != buf
.buf_len
)
310 if (arg
.num_params
) {
311 params
= kcalloc(arg
.num_params
, sizeof(struct tee_param
),
315 uparams
= uarg
->params
;
316 rc
= params_from_user(ctx
, params
, arg
.num_params
, uparams
);
321 rc
= ctx
->teedev
->desc
->ops
->open_session(ctx
, &arg
, params
);
326 if (put_user(arg
.session
, &uarg
->session
) ||
327 put_user(arg
.ret
, &uarg
->ret
) ||
328 put_user(arg
.ret_origin
, &uarg
->ret_origin
)) {
332 rc
= params_to_user(uparams
, arg
.num_params
, params
);
335 * If we've succeeded to open the session but failed to communicate
336 * it back to user space, close the session again to avoid leakage.
338 if (rc
&& have_session
&& ctx
->teedev
->desc
->ops
->close_session
)
339 ctx
->teedev
->desc
->ops
->close_session(ctx
, arg
.session
);
342 /* Decrease ref count for all valid shared memory pointers */
343 for (n
= 0; n
< arg
.num_params
; n
++)
344 if (tee_param_is_memref(params
+ n
) &&
345 params
[n
].u
.memref
.shm
)
346 tee_shm_put(params
[n
].u
.memref
.shm
);
353 static int tee_ioctl_invoke(struct tee_context
*ctx
,
354 struct tee_ioctl_buf_data __user
*ubuf
)
358 struct tee_ioctl_buf_data buf
;
359 struct tee_ioctl_invoke_arg __user
*uarg
;
360 struct tee_ioctl_invoke_arg arg
;
361 struct tee_ioctl_param __user
*uparams
= NULL
;
362 struct tee_param
*params
= NULL
;
364 if (!ctx
->teedev
->desc
->ops
->invoke_func
)
367 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
370 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
371 buf
.buf_len
< sizeof(struct tee_ioctl_invoke_arg
))
374 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
375 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
378 if (sizeof(arg
) + TEE_IOCTL_PARAM_SIZE(arg
.num_params
) != buf
.buf_len
)
381 if (arg
.num_params
) {
382 params
= kcalloc(arg
.num_params
, sizeof(struct tee_param
),
386 uparams
= uarg
->params
;
387 rc
= params_from_user(ctx
, params
, arg
.num_params
, uparams
);
392 rc
= ctx
->teedev
->desc
->ops
->invoke_func(ctx
, &arg
, params
);
396 if (put_user(arg
.ret
, &uarg
->ret
) ||
397 put_user(arg
.ret_origin
, &uarg
->ret_origin
)) {
401 rc
= params_to_user(uparams
, arg
.num_params
, params
);
404 /* Decrease ref count for all valid shared memory pointers */
405 for (n
= 0; n
< arg
.num_params
; n
++)
406 if (tee_param_is_memref(params
+ n
) &&
407 params
[n
].u
.memref
.shm
)
408 tee_shm_put(params
[n
].u
.memref
.shm
);
414 static int tee_ioctl_cancel(struct tee_context
*ctx
,
415 struct tee_ioctl_cancel_arg __user
*uarg
)
417 struct tee_ioctl_cancel_arg arg
;
419 if (!ctx
->teedev
->desc
->ops
->cancel_req
)
422 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
425 return ctx
->teedev
->desc
->ops
->cancel_req(ctx
, arg
.cancel_id
,
430 tee_ioctl_close_session(struct tee_context
*ctx
,
431 struct tee_ioctl_close_session_arg __user
*uarg
)
433 struct tee_ioctl_close_session_arg arg
;
435 if (!ctx
->teedev
->desc
->ops
->close_session
)
438 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
441 return ctx
->teedev
->desc
->ops
->close_session(ctx
, arg
.session
);
444 static int params_to_supp(struct tee_context
*ctx
,
445 struct tee_ioctl_param __user
*uparams
,
446 size_t num_params
, struct tee_param
*params
)
450 for (n
= 0; n
< num_params
; n
++) {
451 struct tee_ioctl_param ip
;
452 struct tee_param
*p
= params
+ n
;
455 switch (p
->attr
& TEE_IOCTL_PARAM_ATTR_TYPE_MASK
) {
456 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT
:
457 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
462 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT
:
463 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
464 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
465 ip
.b
= p
->u
.memref
.size
;
466 if (!p
->u
.memref
.shm
) {
468 ip
.c
= (u64
)-1; /* invalid shm id */
471 ip
.a
= p
->u
.memref
.shm_offs
;
472 ip
.c
= p
->u
.memref
.shm
->id
;
481 if (copy_to_user(uparams
+ n
, &ip
, sizeof(ip
)))
488 static int tee_ioctl_supp_recv(struct tee_context
*ctx
,
489 struct tee_ioctl_buf_data __user
*ubuf
)
492 struct tee_ioctl_buf_data buf
;
493 struct tee_iocl_supp_recv_arg __user
*uarg
;
494 struct tee_param
*params
;
498 if (!ctx
->teedev
->desc
->ops
->supp_recv
)
501 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
504 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
505 buf
.buf_len
< sizeof(struct tee_iocl_supp_recv_arg
))
508 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
509 if (get_user(num_params
, &uarg
->num_params
))
512 if (sizeof(*uarg
) + TEE_IOCTL_PARAM_SIZE(num_params
) != buf
.buf_len
)
515 params
= kcalloc(num_params
, sizeof(struct tee_param
), GFP_KERNEL
);
519 rc
= params_from_user(ctx
, params
, num_params
, uarg
->params
);
523 rc
= ctx
->teedev
->desc
->ops
->supp_recv(ctx
, &func
, &num_params
, params
);
527 if (put_user(func
, &uarg
->func
) ||
528 put_user(num_params
, &uarg
->num_params
)) {
533 rc
= params_to_supp(ctx
, uarg
->params
, num_params
, params
);
539 static int params_from_supp(struct tee_param
*params
, size_t num_params
,
540 struct tee_ioctl_param __user
*uparams
)
544 for (n
= 0; n
< num_params
; n
++) {
545 struct tee_param
*p
= params
+ n
;
546 struct tee_ioctl_param ip
;
548 if (copy_from_user(&ip
, uparams
+ n
, sizeof(ip
)))
551 /* All unused attribute bits has to be zero */
552 if (ip
.attr
& ~TEE_IOCTL_PARAM_ATTR_MASK
)
556 switch (ip
.attr
& TEE_IOCTL_PARAM_ATTR_TYPE_MASK
) {
557 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT
:
558 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT
:
559 /* Only out and in/out values can be updated */
564 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
:
565 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT
:
567 * Only the size of the memref can be updated.
568 * Since we don't have access to the original
569 * parameters here, only store the supplied size.
570 * The driver will copy the updated size into the
571 * original parameters.
573 p
->u
.memref
.shm
= NULL
;
574 p
->u
.memref
.shm_offs
= 0;
575 p
->u
.memref
.size
= ip
.b
;
578 memset(&p
->u
, 0, sizeof(p
->u
));
585 static int tee_ioctl_supp_send(struct tee_context
*ctx
,
586 struct tee_ioctl_buf_data __user
*ubuf
)
589 struct tee_ioctl_buf_data buf
;
590 struct tee_iocl_supp_send_arg __user
*uarg
;
591 struct tee_param
*params
;
595 /* Not valid for this driver */
596 if (!ctx
->teedev
->desc
->ops
->supp_send
)
599 if (copy_from_user(&buf
, ubuf
, sizeof(buf
)))
602 if (buf
.buf_len
> TEE_MAX_ARG_SIZE
||
603 buf
.buf_len
< sizeof(struct tee_iocl_supp_send_arg
))
606 uarg
= u64_to_user_ptr(buf
.buf_ptr
);
607 if (get_user(ret
, &uarg
->ret
) ||
608 get_user(num_params
, &uarg
->num_params
))
611 if (sizeof(*uarg
) + TEE_IOCTL_PARAM_SIZE(num_params
) > buf
.buf_len
)
614 params
= kcalloc(num_params
, sizeof(struct tee_param
), GFP_KERNEL
);
618 rc
= params_from_supp(params
, num_params
, uarg
->params
);
622 rc
= ctx
->teedev
->desc
->ops
->supp_send(ctx
, ret
, num_params
, params
);
628 static long tee_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
630 struct tee_context
*ctx
= filp
->private_data
;
631 void __user
*uarg
= (void __user
*)arg
;
634 case TEE_IOC_VERSION
:
635 return tee_ioctl_version(ctx
, uarg
);
636 case TEE_IOC_SHM_ALLOC
:
637 return tee_ioctl_shm_alloc(ctx
, uarg
);
638 case TEE_IOC_SHM_REGISTER
:
639 return tee_ioctl_shm_register(ctx
, uarg
);
640 case TEE_IOC_OPEN_SESSION
:
641 return tee_ioctl_open_session(ctx
, uarg
);
643 return tee_ioctl_invoke(ctx
, uarg
);
645 return tee_ioctl_cancel(ctx
, uarg
);
646 case TEE_IOC_CLOSE_SESSION
:
647 return tee_ioctl_close_session(ctx
, uarg
);
648 case TEE_IOC_SUPPL_RECV
:
649 return tee_ioctl_supp_recv(ctx
, uarg
);
650 case TEE_IOC_SUPPL_SEND
:
651 return tee_ioctl_supp_send(ctx
, uarg
);
657 static const struct file_operations tee_fops
= {
658 .owner
= THIS_MODULE
,
660 .release
= tee_release
,
661 .unlocked_ioctl
= tee_ioctl
,
662 .compat_ioctl
= tee_ioctl
,
665 static void tee_release_device(struct device
*dev
)
667 struct tee_device
*teedev
= container_of(dev
, struct tee_device
, dev
);
669 spin_lock(&driver_lock
);
670 clear_bit(teedev
->id
, dev_mask
);
671 spin_unlock(&driver_lock
);
672 mutex_destroy(&teedev
->mutex
);
673 idr_destroy(&teedev
->idr
);
678 * tee_device_alloc() - Allocate a new struct tee_device instance
679 * @teedesc: Descriptor for this driver
680 * @dev: Parent device for this device
681 * @pool: Shared memory pool, NULL if not used
682 * @driver_data: Private driver data for this device
684 * Allocates a new struct tee_device instance. The device is
685 * removed by tee_device_unregister().
687 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
689 struct tee_device
*tee_device_alloc(const struct tee_desc
*teedesc
,
691 struct tee_shm_pool
*pool
,
694 struct tee_device
*teedev
;
699 if (!teedesc
|| !teedesc
->name
|| !teedesc
->ops
||
700 !teedesc
->ops
->get_version
|| !teedesc
->ops
->open
||
701 !teedesc
->ops
->release
|| !pool
)
702 return ERR_PTR(-EINVAL
);
704 teedev
= kzalloc(sizeof(*teedev
), GFP_KERNEL
);
706 ret
= ERR_PTR(-ENOMEM
);
710 if (teedesc
->flags
& TEE_DESC_PRIVILEGED
)
711 offs
= TEE_NUM_DEVICES
/ 2;
713 spin_lock(&driver_lock
);
714 teedev
->id
= find_next_zero_bit(dev_mask
, TEE_NUM_DEVICES
, offs
);
715 if (teedev
->id
< TEE_NUM_DEVICES
)
716 set_bit(teedev
->id
, dev_mask
);
717 spin_unlock(&driver_lock
);
719 if (teedev
->id
>= TEE_NUM_DEVICES
) {
720 ret
= ERR_PTR(-ENOMEM
);
724 snprintf(teedev
->name
, sizeof(teedev
->name
), "tee%s%d",
725 teedesc
->flags
& TEE_DESC_PRIVILEGED
? "priv" : "",
728 teedev
->dev
.class = tee_class
;
729 teedev
->dev
.release
= tee_release_device
;
730 teedev
->dev
.parent
= dev
;
732 teedev
->dev
.devt
= MKDEV(MAJOR(tee_devt
), teedev
->id
);
734 rc
= dev_set_name(&teedev
->dev
, "%s", teedev
->name
);
740 cdev_init(&teedev
->cdev
, &tee_fops
);
741 teedev
->cdev
.owner
= teedesc
->owner
;
742 teedev
->cdev
.kobj
.parent
= &teedev
->dev
.kobj
;
744 dev_set_drvdata(&teedev
->dev
, driver_data
);
745 device_initialize(&teedev
->dev
);
747 /* 1 as tee_device_unregister() does one final tee_device_put() */
748 teedev
->num_users
= 1;
749 init_completion(&teedev
->c_no_users
);
750 mutex_init(&teedev
->mutex
);
751 idr_init(&teedev
->idr
);
753 teedev
->desc
= teedesc
;
758 unregister_chrdev_region(teedev
->dev
.devt
, 1);
760 pr_err("could not register %s driver\n",
761 teedesc
->flags
& TEE_DESC_PRIVILEGED
? "privileged" : "client");
762 if (teedev
&& teedev
->id
< TEE_NUM_DEVICES
) {
763 spin_lock(&driver_lock
);
764 clear_bit(teedev
->id
, dev_mask
);
765 spin_unlock(&driver_lock
);
770 EXPORT_SYMBOL_GPL(tee_device_alloc
);
772 static ssize_t
implementation_id_show(struct device
*dev
,
773 struct device_attribute
*attr
, char *buf
)
775 struct tee_device
*teedev
= container_of(dev
, struct tee_device
, dev
);
776 struct tee_ioctl_version_data vers
;
778 teedev
->desc
->ops
->get_version(teedev
, &vers
);
779 return scnprintf(buf
, PAGE_SIZE
, "%d\n", vers
.impl_id
);
781 static DEVICE_ATTR_RO(implementation_id
);
783 static struct attribute
*tee_dev_attrs
[] = {
784 &dev_attr_implementation_id
.attr
,
788 static const struct attribute_group tee_dev_group
= {
789 .attrs
= tee_dev_attrs
,
793 * tee_device_register() - Registers a TEE device
794 * @teedev: Device to register
796 * tee_device_unregister() need to be called to remove the @teedev if
797 * this function fails.
799 * @returns < 0 on failure
801 int tee_device_register(struct tee_device
*teedev
)
805 if (teedev
->flags
& TEE_DEVICE_FLAG_REGISTERED
) {
806 dev_err(&teedev
->dev
, "attempt to register twice\n");
810 rc
= cdev_add(&teedev
->cdev
, teedev
->dev
.devt
, 1);
812 dev_err(&teedev
->dev
,
813 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
814 teedev
->name
, MAJOR(teedev
->dev
.devt
),
815 MINOR(teedev
->dev
.devt
), rc
);
819 rc
= device_add(&teedev
->dev
);
821 dev_err(&teedev
->dev
,
822 "unable to device_add() %s, major %d, minor %d, err=%d\n",
823 teedev
->name
, MAJOR(teedev
->dev
.devt
),
824 MINOR(teedev
->dev
.devt
), rc
);
828 rc
= sysfs_create_group(&teedev
->dev
.kobj
, &tee_dev_group
);
830 dev_err(&teedev
->dev
,
831 "failed to create sysfs attributes, err=%d\n", rc
);
832 goto err_sysfs_create_group
;
835 teedev
->flags
|= TEE_DEVICE_FLAG_REGISTERED
;
838 err_sysfs_create_group
:
839 device_del(&teedev
->dev
);
841 cdev_del(&teedev
->cdev
);
844 EXPORT_SYMBOL_GPL(tee_device_register
);
846 void tee_device_put(struct tee_device
*teedev
)
848 mutex_lock(&teedev
->mutex
);
849 /* Shouldn't put in this state */
850 if (!WARN_ON(!teedev
->desc
)) {
852 if (!teedev
->num_users
) {
854 complete(&teedev
->c_no_users
);
857 mutex_unlock(&teedev
->mutex
);
860 bool tee_device_get(struct tee_device
*teedev
)
862 mutex_lock(&teedev
->mutex
);
864 mutex_unlock(&teedev
->mutex
);
868 mutex_unlock(&teedev
->mutex
);
873 * tee_device_unregister() - Removes a TEE device
874 * @teedev: Device to unregister
876 * This function should be called to remove the @teedev even if
877 * tee_device_register() hasn't been called yet. Does nothing if
880 void tee_device_unregister(struct tee_device
*teedev
)
885 if (teedev
->flags
& TEE_DEVICE_FLAG_REGISTERED
) {
886 sysfs_remove_group(&teedev
->dev
.kobj
, &tee_dev_group
);
887 cdev_del(&teedev
->cdev
);
888 device_del(&teedev
->dev
);
891 tee_device_put(teedev
);
892 wait_for_completion(&teedev
->c_no_users
);
895 * No need to take a mutex any longer now since teedev->desc was
896 * set to NULL before teedev->c_no_users was completed.
901 put_device(&teedev
->dev
);
903 EXPORT_SYMBOL_GPL(tee_device_unregister
);
906 * tee_get_drvdata() - Return driver_data pointer
907 * @teedev: Device containing the driver_data pointer
908 * @returns the driver_data pointer supplied to tee_register().
910 void *tee_get_drvdata(struct tee_device
*teedev
)
912 return dev_get_drvdata(&teedev
->dev
);
914 EXPORT_SYMBOL_GPL(tee_get_drvdata
);
916 static int __init
tee_init(void)
920 tee_class
= class_create(THIS_MODULE
, "tee");
921 if (IS_ERR(tee_class
)) {
922 pr_err("couldn't create class\n");
923 return PTR_ERR(tee_class
);
926 rc
= alloc_chrdev_region(&tee_devt
, 0, TEE_NUM_DEVICES
, "tee");
928 pr_err("failed to allocate char dev region\n");
929 class_destroy(tee_class
);
936 static void __exit
tee_exit(void)
938 class_destroy(tee_class
);
940 unregister_chrdev_region(tee_devt
, TEE_NUM_DEVICES
);
943 subsys_initcall(tee_init
);
944 module_exit(tee_exit
);
946 MODULE_AUTHOR("Linaro");
947 MODULE_DESCRIPTION("TEE Driver");
948 MODULE_VERSION("1.0");
949 MODULE_LICENSE("GPL v2");