• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015-2016, Linaro Limited
4  */
5 
6 #define pr_fmt(fmt) "%s: " fmt, __func__
7 
8 #include <linux/cdev.h>
9 #include <linux/cred.h>
10 #include <linux/fs.h>
11 #include <linux/idr.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/tee_drv.h>
15 #include <linux/uaccess.h>
16 #include <crypto/hash.h>
17 #include <crypto/sha1.h>
18 #include "tee_private.h"
19 
20 #define TEE_NUM_DEVICES	32
21 
22 #define TEE_IOCTL_PARAM_SIZE(x) (sizeof(struct tee_param) * (x))
23 
24 #define TEE_UUID_NS_NAME_SIZE	128
25 
26 /*
27  * TEE Client UUID name space identifier (UUIDv4)
28  *
29  * Value here is random UUID that is allocated as name space identifier for
30  * forming Client UUID's for TEE environment using UUIDv5 scheme.
31  */
32 static const uuid_t tee_client_uuid_ns = UUID_INIT(0x58ac9ca0, 0x2086, 0x4683,
33 						   0xa1, 0xb8, 0xec, 0x4b,
34 						   0xc0, 0x8e, 0x01, 0xb6);
35 
36 /*
37  * Unprivileged devices in the lower half range and privileged devices in
38  * the upper half range.
39  */
40 static DECLARE_BITMAP(dev_mask, TEE_NUM_DEVICES);
41 static DEFINE_SPINLOCK(driver_lock);
42 
43 static struct class *tee_class;
44 static dev_t tee_devt;
45 
teedev_open(struct tee_device * teedev)46 struct tee_context *teedev_open(struct tee_device *teedev)
47 {
48 	int rc;
49 	struct tee_context *ctx;
50 
51 	if (!tee_device_get(teedev))
52 		return ERR_PTR(-EINVAL);
53 
54 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
55 	if (!ctx) {
56 		rc = -ENOMEM;
57 		goto err;
58 	}
59 
60 	kref_init(&ctx->refcount);
61 	ctx->teedev = teedev;
62 	rc = teedev->desc->ops->open(ctx);
63 	if (rc)
64 		goto err;
65 
66 	return ctx;
67 err:
68 	kfree(ctx);
69 	tee_device_put(teedev);
70 	return ERR_PTR(rc);
71 
72 }
73 EXPORT_SYMBOL_GPL(teedev_open);
74 
teedev_ctx_get(struct tee_context * ctx)75 void teedev_ctx_get(struct tee_context *ctx)
76 {
77 	if (ctx->releasing)
78 		return;
79 
80 	kref_get(&ctx->refcount);
81 }
82 
teedev_ctx_release(struct kref * ref)83 static void teedev_ctx_release(struct kref *ref)
84 {
85 	struct tee_context *ctx = container_of(ref, struct tee_context,
86 					       refcount);
87 	ctx->releasing = true;
88 	ctx->teedev->desc->ops->release(ctx);
89 	kfree(ctx);
90 }
91 
teedev_ctx_put(struct tee_context * ctx)92 void teedev_ctx_put(struct tee_context *ctx)
93 {
94 	if (ctx->releasing)
95 		return;
96 
97 	kref_put(&ctx->refcount, teedev_ctx_release);
98 }
99 
teedev_close_context(struct tee_context * ctx)100 void teedev_close_context(struct tee_context *ctx)
101 {
102 	struct tee_device *teedev = ctx->teedev;
103 
104 	teedev_ctx_put(ctx);
105 	tee_device_put(teedev);
106 }
107 EXPORT_SYMBOL_GPL(teedev_close_context);
108 
tee_open(struct inode * inode,struct file * filp)109 static int tee_open(struct inode *inode, struct file *filp)
110 {
111 	struct tee_context *ctx;
112 
113 	ctx = teedev_open(container_of(inode->i_cdev, struct tee_device, cdev));
114 	if (IS_ERR(ctx))
115 		return PTR_ERR(ctx);
116 
117 	/*
118 	 * Default user-space behaviour is to wait for tee-supplicant
119 	 * if not present for any requests in this context.
120 	 */
121 	ctx->supp_nowait = false;
122 	filp->private_data = ctx;
123 	return 0;
124 }
125 
tee_release(struct inode * inode,struct file * filp)126 static int tee_release(struct inode *inode, struct file *filp)
127 {
128 	teedev_close_context(filp->private_data);
129 	return 0;
130 }
131 
132 /**
133  * uuid_v5() - Calculate UUIDv5
134  * @uuid: Resulting UUID
135  * @ns: Name space ID for UUIDv5 function
136  * @name: Name for UUIDv5 function
137  * @size: Size of name
138  *
139  * UUIDv5 is specific in RFC 4122.
140  *
141  * This implements section (for SHA-1):
142  * 4.3.  Algorithm for Creating a Name-Based UUID
143  */
uuid_v5(uuid_t * uuid,const uuid_t * ns,const void * name,size_t size)144 static int uuid_v5(uuid_t *uuid, const uuid_t *ns, const void *name,
145 		   size_t size)
146 {
147 	unsigned char hash[SHA1_DIGEST_SIZE];
148 	struct crypto_shash *shash = NULL;
149 	struct shash_desc *desc = NULL;
150 	int rc;
151 
152 	shash = crypto_alloc_shash("sha1", 0, 0);
153 	if (IS_ERR(shash)) {
154 		rc = PTR_ERR(shash);
155 		pr_err("shash(sha1) allocation failed\n");
156 		return rc;
157 	}
158 
159 	desc = kzalloc(sizeof(*desc) + crypto_shash_descsize(shash),
160 		       GFP_KERNEL);
161 	if (!desc) {
162 		rc = -ENOMEM;
163 		goto out_free_shash;
164 	}
165 
166 	desc->tfm = shash;
167 
168 	rc = crypto_shash_init(desc);
169 	if (rc < 0)
170 		goto out_free_desc;
171 
172 	rc = crypto_shash_update(desc, (const u8 *)ns, sizeof(*ns));
173 	if (rc < 0)
174 		goto out_free_desc;
175 
176 	rc = crypto_shash_update(desc, (const u8 *)name, size);
177 	if (rc < 0)
178 		goto out_free_desc;
179 
180 	rc = crypto_shash_final(desc, hash);
181 	if (rc < 0)
182 		goto out_free_desc;
183 
184 	memcpy(uuid->b, hash, UUID_SIZE);
185 
186 	/* Tag for version 5 */
187 	uuid->b[6] = (hash[6] & 0x0F) | 0x50;
188 	uuid->b[8] = (hash[8] & 0x3F) | 0x80;
189 
190 out_free_desc:
191 	kfree(desc);
192 
193 out_free_shash:
194 	crypto_free_shash(shash);
195 	return rc;
196 }
197 
tee_session_calc_client_uuid(uuid_t * uuid,u32 connection_method,const u8 connection_data[TEE_IOCTL_UUID_LEN])198 int tee_session_calc_client_uuid(uuid_t *uuid, u32 connection_method,
199 				 const u8 connection_data[TEE_IOCTL_UUID_LEN])
200 {
201 	gid_t ns_grp = (gid_t)-1;
202 	kgid_t grp = INVALID_GID;
203 	char *name = NULL;
204 	int name_len;
205 	int rc;
206 
207 	if (connection_method == TEE_IOCTL_LOGIN_PUBLIC ||
208 	    connection_method == TEE_IOCTL_LOGIN_REE_KERNEL) {
209 		/* Nil UUID to be passed to TEE environment */
210 		uuid_copy(uuid, &uuid_null);
211 		return 0;
212 	}
213 
214 	/*
215 	 * In Linux environment client UUID is based on UUIDv5.
216 	 *
217 	 * Determine client UUID with following semantics for 'name':
218 	 *
219 	 * For TEEC_LOGIN_USER:
220 	 * uid=<uid>
221 	 *
222 	 * For TEEC_LOGIN_GROUP:
223 	 * gid=<gid>
224 	 *
225 	 */
226 
227 	name = kzalloc(TEE_UUID_NS_NAME_SIZE, GFP_KERNEL);
228 	if (!name)
229 		return -ENOMEM;
230 
231 	switch (connection_method) {
232 	case TEE_IOCTL_LOGIN_USER:
233 		name_len = snprintf(name, TEE_UUID_NS_NAME_SIZE, "uid=%x",
234 				    current_euid().val);
235 		if (name_len >= TEE_UUID_NS_NAME_SIZE) {
236 			rc = -E2BIG;
237 			goto out_free_name;
238 		}
239 		break;
240 
241 	case TEE_IOCTL_LOGIN_GROUP:
242 		memcpy(&ns_grp, connection_data, sizeof(gid_t));
243 		grp = make_kgid(current_user_ns(), ns_grp);
244 		if (!gid_valid(grp) || !in_egroup_p(grp)) {
245 			rc = -EPERM;
246 			goto out_free_name;
247 		}
248 
249 		name_len = snprintf(name, TEE_UUID_NS_NAME_SIZE, "gid=%x",
250 				    grp.val);
251 		if (name_len >= TEE_UUID_NS_NAME_SIZE) {
252 			rc = -E2BIG;
253 			goto out_free_name;
254 		}
255 		break;
256 
257 	default:
258 		rc = -EINVAL;
259 		goto out_free_name;
260 	}
261 
262 	rc = uuid_v5(uuid, &tee_client_uuid_ns, name, name_len);
263 out_free_name:
264 	kfree(name);
265 
266 	return rc;
267 }
268 EXPORT_SYMBOL_GPL(tee_session_calc_client_uuid);
269 
tee_ioctl_version(struct tee_context * ctx,struct tee_ioctl_version_data __user * uvers)270 static int tee_ioctl_version(struct tee_context *ctx,
271 			     struct tee_ioctl_version_data __user *uvers)
272 {
273 	struct tee_ioctl_version_data vers;
274 
275 	ctx->teedev->desc->ops->get_version(ctx->teedev, &vers);
276 
277 	if (ctx->teedev->desc->flags & TEE_DESC_PRIVILEGED)
278 		vers.gen_caps |= TEE_GEN_CAP_PRIVILEGED;
279 
280 	if (copy_to_user(uvers, &vers, sizeof(vers)))
281 		return -EFAULT;
282 
283 	return 0;
284 }
285 
tee_ioctl_shm_alloc(struct tee_context * ctx,struct tee_ioctl_shm_alloc_data __user * udata)286 static int tee_ioctl_shm_alloc(struct tee_context *ctx,
287 			       struct tee_ioctl_shm_alloc_data __user *udata)
288 {
289 	long ret;
290 	struct tee_ioctl_shm_alloc_data data;
291 	struct tee_shm *shm;
292 
293 	if (copy_from_user(&data, udata, sizeof(data)))
294 		return -EFAULT;
295 
296 	/* Currently no input flags are supported */
297 	if (data.flags)
298 		return -EINVAL;
299 
300 	shm = tee_shm_alloc(ctx, data.size, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF);
301 	if (IS_ERR(shm))
302 		return PTR_ERR(shm);
303 
304 	data.id = shm->id;
305 	data.flags = shm->flags;
306 	data.size = shm->size;
307 
308 	if (copy_to_user(udata, &data, sizeof(data)))
309 		ret = -EFAULT;
310 	else
311 		ret = tee_shm_get_fd(shm);
312 
313 	/*
314 	 * When user space closes the file descriptor the shared memory
315 	 * should be freed or if tee_shm_get_fd() failed then it will
316 	 * be freed immediately.
317 	 */
318 	tee_shm_put(shm);
319 	return ret;
320 }
321 
322 static int
tee_ioctl_shm_register(struct tee_context * ctx,struct tee_ioctl_shm_register_data __user * udata)323 tee_ioctl_shm_register(struct tee_context *ctx,
324 		       struct tee_ioctl_shm_register_data __user *udata)
325 {
326 	long ret;
327 	struct tee_ioctl_shm_register_data data;
328 	struct tee_shm *shm;
329 
330 	if (copy_from_user(&data, udata, sizeof(data)))
331 		return -EFAULT;
332 
333 	/* Currently no input flags are supported */
334 	if (data.flags)
335 		return -EINVAL;
336 
337 	if (!access_ok((void __user *)(unsigned long)data.addr, data.length))
338 		return -EFAULT;
339 
340 	shm = tee_shm_register(ctx, data.addr, data.length,
341 			       TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED);
342 	if (IS_ERR(shm))
343 		return PTR_ERR(shm);
344 
345 	data.id = shm->id;
346 	data.flags = shm->flags;
347 	data.length = shm->size;
348 
349 	if (copy_to_user(udata, &data, sizeof(data)))
350 		ret = -EFAULT;
351 	else
352 		ret = tee_shm_get_fd(shm);
353 	/*
354 	 * When user space closes the file descriptor the shared memory
355 	 * should be freed or if tee_shm_get_fd() failed then it will
356 	 * be freed immediately.
357 	 */
358 	tee_shm_put(shm);
359 	return ret;
360 }
361 
params_from_user(struct tee_context * ctx,struct tee_param * params,size_t num_params,struct tee_ioctl_param __user * uparams)362 static int params_from_user(struct tee_context *ctx, struct tee_param *params,
363 			    size_t num_params,
364 			    struct tee_ioctl_param __user *uparams)
365 {
366 	size_t n;
367 
368 	for (n = 0; n < num_params; n++) {
369 		struct tee_shm *shm;
370 		struct tee_ioctl_param ip;
371 
372 		if (copy_from_user(&ip, uparams + n, sizeof(ip)))
373 			return -EFAULT;
374 
375 		/* All unused attribute bits has to be zero */
376 		if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
377 			return -EINVAL;
378 
379 		params[n].attr = ip.attr;
380 		switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
381 		case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
382 		case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
383 			break;
384 		case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
385 		case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
386 			params[n].u.value.a = ip.a;
387 			params[n].u.value.b = ip.b;
388 			params[n].u.value.c = ip.c;
389 			break;
390 		case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
391 		case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
392 		case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
393 			/*
394 			 * If a NULL pointer is passed to a TA in the TEE,
395 			 * the ip.c IOCTL parameters is set to TEE_MEMREF_NULL
396 			 * indicating a NULL memory reference.
397 			 */
398 			if (ip.c != TEE_MEMREF_NULL) {
399 				/*
400 				 * If we fail to get a pointer to a shared
401 				 * memory object (and increase the ref count)
402 				 * from an identifier we return an error. All
403 				 * pointers that has been added in params have
404 				 * an increased ref count. It's the callers
405 				 * responibility to do tee_shm_put() on all
406 				 * resolved pointers.
407 				 */
408 				shm = tee_shm_get_from_id(ctx, ip.c);
409 				if (IS_ERR(shm))
410 					return PTR_ERR(shm);
411 
412 				/*
413 				 * Ensure offset + size does not overflow
414 				 * offset and does not overflow the size of
415 				 * the referred shared memory object.
416 				 */
417 				if ((ip.a + ip.b) < ip.a ||
418 				    (ip.a + ip.b) > shm->size) {
419 					tee_shm_put(shm);
420 					return -EINVAL;
421 				}
422 			} else if (ctx->cap_memref_null) {
423 				/* Pass NULL pointer to OP-TEE */
424 				shm = NULL;
425 			} else {
426 				return -EINVAL;
427 			}
428 
429 			params[n].u.memref.shm_offs = ip.a;
430 			params[n].u.memref.size = ip.b;
431 			params[n].u.memref.shm = shm;
432 			break;
433 		default:
434 			/* Unknown attribute */
435 			return -EINVAL;
436 		}
437 	}
438 	return 0;
439 }
440 
params_to_user(struct tee_ioctl_param __user * uparams,size_t num_params,struct tee_param * params)441 static int params_to_user(struct tee_ioctl_param __user *uparams,
442 			  size_t num_params, struct tee_param *params)
443 {
444 	size_t n;
445 
446 	for (n = 0; n < num_params; n++) {
447 		struct tee_ioctl_param __user *up = uparams + n;
448 		struct tee_param *p = params + n;
449 
450 		switch (p->attr) {
451 		case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
452 		case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
453 			if (put_user(p->u.value.a, &up->a) ||
454 			    put_user(p->u.value.b, &up->b) ||
455 			    put_user(p->u.value.c, &up->c))
456 				return -EFAULT;
457 			break;
458 		case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
459 		case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
460 			if (put_user((u64)p->u.memref.size, &up->b))
461 				return -EFAULT;
462 			break;
463 		default:
464 			break;
465 		}
466 	}
467 	return 0;
468 }
469 
tee_ioctl_open_session(struct tee_context * ctx,struct tee_ioctl_buf_data __user * ubuf)470 static int tee_ioctl_open_session(struct tee_context *ctx,
471 				  struct tee_ioctl_buf_data __user *ubuf)
472 {
473 	int rc;
474 	size_t n;
475 	struct tee_ioctl_buf_data buf;
476 	struct tee_ioctl_open_session_arg __user *uarg;
477 	struct tee_ioctl_open_session_arg arg;
478 	struct tee_ioctl_param __user *uparams = NULL;
479 	struct tee_param *params = NULL;
480 	bool have_session = false;
481 
482 	if (!ctx->teedev->desc->ops->open_session)
483 		return -EINVAL;
484 
485 	if (copy_from_user(&buf, ubuf, sizeof(buf)))
486 		return -EFAULT;
487 
488 	if (buf.buf_len > TEE_MAX_ARG_SIZE ||
489 	    buf.buf_len < sizeof(struct tee_ioctl_open_session_arg))
490 		return -EINVAL;
491 
492 	uarg = u64_to_user_ptr(buf.buf_ptr);
493 	if (copy_from_user(&arg, uarg, sizeof(arg)))
494 		return -EFAULT;
495 
496 	if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
497 		return -EINVAL;
498 
499 	if (arg.num_params) {
500 		params = kcalloc(arg.num_params, sizeof(struct tee_param),
501 				 GFP_KERNEL);
502 		if (!params)
503 			return -ENOMEM;
504 		uparams = uarg->params;
505 		rc = params_from_user(ctx, params, arg.num_params, uparams);
506 		if (rc)
507 			goto out;
508 	}
509 
510 	if (arg.clnt_login >= TEE_IOCTL_LOGIN_REE_KERNEL_MIN &&
511 	    arg.clnt_login <= TEE_IOCTL_LOGIN_REE_KERNEL_MAX) {
512 		pr_debug("login method not allowed for user-space client\n");
513 		rc = -EPERM;
514 		goto out;
515 	}
516 
517 	rc = ctx->teedev->desc->ops->open_session(ctx, &arg, params);
518 	if (rc)
519 		goto out;
520 	have_session = true;
521 
522 	if (put_user(arg.session, &uarg->session) ||
523 	    put_user(arg.ret, &uarg->ret) ||
524 	    put_user(arg.ret_origin, &uarg->ret_origin)) {
525 		rc = -EFAULT;
526 		goto out;
527 	}
528 	rc = params_to_user(uparams, arg.num_params, params);
529 out:
530 	/*
531 	 * If we've succeeded to open the session but failed to communicate
532 	 * it back to user space, close the session again to avoid leakage.
533 	 */
534 	if (rc && have_session && ctx->teedev->desc->ops->close_session)
535 		ctx->teedev->desc->ops->close_session(ctx, arg.session);
536 
537 	if (params) {
538 		/* Decrease ref count for all valid shared memory pointers */
539 		for (n = 0; n < arg.num_params; n++)
540 			if (tee_param_is_memref(params + n) &&
541 			    params[n].u.memref.shm)
542 				tee_shm_put(params[n].u.memref.shm);
543 		kfree(params);
544 	}
545 
546 	return rc;
547 }
548 
tee_ioctl_invoke(struct tee_context * ctx,struct tee_ioctl_buf_data __user * ubuf)549 static int tee_ioctl_invoke(struct tee_context *ctx,
550 			    struct tee_ioctl_buf_data __user *ubuf)
551 {
552 	int rc;
553 	size_t n;
554 	struct tee_ioctl_buf_data buf;
555 	struct tee_ioctl_invoke_arg __user *uarg;
556 	struct tee_ioctl_invoke_arg arg;
557 	struct tee_ioctl_param __user *uparams = NULL;
558 	struct tee_param *params = NULL;
559 
560 	if (!ctx->teedev->desc->ops->invoke_func)
561 		return -EINVAL;
562 
563 	if (copy_from_user(&buf, ubuf, sizeof(buf)))
564 		return -EFAULT;
565 
566 	if (buf.buf_len > TEE_MAX_ARG_SIZE ||
567 	    buf.buf_len < sizeof(struct tee_ioctl_invoke_arg))
568 		return -EINVAL;
569 
570 	uarg = u64_to_user_ptr(buf.buf_ptr);
571 	if (copy_from_user(&arg, uarg, sizeof(arg)))
572 		return -EFAULT;
573 
574 	if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
575 		return -EINVAL;
576 
577 	if (arg.num_params) {
578 		params = kcalloc(arg.num_params, sizeof(struct tee_param),
579 				 GFP_KERNEL);
580 		if (!params)
581 			return -ENOMEM;
582 		uparams = uarg->params;
583 		rc = params_from_user(ctx, params, arg.num_params, uparams);
584 		if (rc)
585 			goto out;
586 	}
587 
588 	rc = ctx->teedev->desc->ops->invoke_func(ctx, &arg, params);
589 	if (rc)
590 		goto out;
591 
592 	if (put_user(arg.ret, &uarg->ret) ||
593 	    put_user(arg.ret_origin, &uarg->ret_origin)) {
594 		rc = -EFAULT;
595 		goto out;
596 	}
597 	rc = params_to_user(uparams, arg.num_params, params);
598 out:
599 	if (params) {
600 		/* Decrease ref count for all valid shared memory pointers */
601 		for (n = 0; n < arg.num_params; n++)
602 			if (tee_param_is_memref(params + n) &&
603 			    params[n].u.memref.shm)
604 				tee_shm_put(params[n].u.memref.shm);
605 		kfree(params);
606 	}
607 	return rc;
608 }
609 
tee_ioctl_cancel(struct tee_context * ctx,struct tee_ioctl_cancel_arg __user * uarg)610 static int tee_ioctl_cancel(struct tee_context *ctx,
611 			    struct tee_ioctl_cancel_arg __user *uarg)
612 {
613 	struct tee_ioctl_cancel_arg arg;
614 
615 	if (!ctx->teedev->desc->ops->cancel_req)
616 		return -EINVAL;
617 
618 	if (copy_from_user(&arg, uarg, sizeof(arg)))
619 		return -EFAULT;
620 
621 	return ctx->teedev->desc->ops->cancel_req(ctx, arg.cancel_id,
622 						  arg.session);
623 }
624 
625 static int
tee_ioctl_close_session(struct tee_context * ctx,struct tee_ioctl_close_session_arg __user * uarg)626 tee_ioctl_close_session(struct tee_context *ctx,
627 			struct tee_ioctl_close_session_arg __user *uarg)
628 {
629 	struct tee_ioctl_close_session_arg arg;
630 
631 	if (!ctx->teedev->desc->ops->close_session)
632 		return -EINVAL;
633 
634 	if (copy_from_user(&arg, uarg, sizeof(arg)))
635 		return -EFAULT;
636 
637 	return ctx->teedev->desc->ops->close_session(ctx, arg.session);
638 }
639 
params_to_supp(struct tee_context * ctx,struct tee_ioctl_param __user * uparams,size_t num_params,struct tee_param * params)640 static int params_to_supp(struct tee_context *ctx,
641 			  struct tee_ioctl_param __user *uparams,
642 			  size_t num_params, struct tee_param *params)
643 {
644 	size_t n;
645 
646 	for (n = 0; n < num_params; n++) {
647 		struct tee_ioctl_param ip;
648 		struct tee_param *p = params + n;
649 
650 		ip.attr = p->attr;
651 		switch (p->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
652 		case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
653 		case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
654 			ip.a = p->u.value.a;
655 			ip.b = p->u.value.b;
656 			ip.c = p->u.value.c;
657 			break;
658 		case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
659 		case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
660 		case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
661 			ip.b = p->u.memref.size;
662 			if (!p->u.memref.shm) {
663 				ip.a = 0;
664 				ip.c = (u64)-1; /* invalid shm id */
665 				break;
666 			}
667 			ip.a = p->u.memref.shm_offs;
668 			ip.c = p->u.memref.shm->id;
669 			break;
670 		default:
671 			ip.a = 0;
672 			ip.b = 0;
673 			ip.c = 0;
674 			break;
675 		}
676 
677 		if (copy_to_user(uparams + n, &ip, sizeof(ip)))
678 			return -EFAULT;
679 	}
680 
681 	return 0;
682 }
683 
tee_ioctl_supp_recv(struct tee_context * ctx,struct tee_ioctl_buf_data __user * ubuf)684 static int tee_ioctl_supp_recv(struct tee_context *ctx,
685 			       struct tee_ioctl_buf_data __user *ubuf)
686 {
687 	int rc;
688 	struct tee_ioctl_buf_data buf;
689 	struct tee_iocl_supp_recv_arg __user *uarg;
690 	struct tee_param *params;
691 	u32 num_params;
692 	u32 func;
693 
694 	if (!ctx->teedev->desc->ops->supp_recv)
695 		return -EINVAL;
696 
697 	if (copy_from_user(&buf, ubuf, sizeof(buf)))
698 		return -EFAULT;
699 
700 	if (buf.buf_len > TEE_MAX_ARG_SIZE ||
701 	    buf.buf_len < sizeof(struct tee_iocl_supp_recv_arg))
702 		return -EINVAL;
703 
704 	uarg = u64_to_user_ptr(buf.buf_ptr);
705 	if (get_user(num_params, &uarg->num_params))
706 		return -EFAULT;
707 
708 	if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) != buf.buf_len)
709 		return -EINVAL;
710 
711 	params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
712 	if (!params)
713 		return -ENOMEM;
714 
715 	rc = params_from_user(ctx, params, num_params, uarg->params);
716 	if (rc)
717 		goto out;
718 
719 	rc = ctx->teedev->desc->ops->supp_recv(ctx, &func, &num_params, params);
720 	if (rc)
721 		goto out;
722 
723 	if (put_user(func, &uarg->func) ||
724 	    put_user(num_params, &uarg->num_params)) {
725 		rc = -EFAULT;
726 		goto out;
727 	}
728 
729 	rc = params_to_supp(ctx, uarg->params, num_params, params);
730 out:
731 	kfree(params);
732 	return rc;
733 }
734 
params_from_supp(struct tee_param * params,size_t num_params,struct tee_ioctl_param __user * uparams)735 static int params_from_supp(struct tee_param *params, size_t num_params,
736 			    struct tee_ioctl_param __user *uparams)
737 {
738 	size_t n;
739 
740 	for (n = 0; n < num_params; n++) {
741 		struct tee_param *p = params + n;
742 		struct tee_ioctl_param ip;
743 
744 		if (copy_from_user(&ip, uparams + n, sizeof(ip)))
745 			return -EFAULT;
746 
747 		/* All unused attribute bits has to be zero */
748 		if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
749 			return -EINVAL;
750 
751 		p->attr = ip.attr;
752 		switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
753 		case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
754 		case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
755 			/* Only out and in/out values can be updated */
756 			p->u.value.a = ip.a;
757 			p->u.value.b = ip.b;
758 			p->u.value.c = ip.c;
759 			break;
760 		case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
761 		case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
762 			/*
763 			 * Only the size of the memref can be updated.
764 			 * Since we don't have access to the original
765 			 * parameters here, only store the supplied size.
766 			 * The driver will copy the updated size into the
767 			 * original parameters.
768 			 */
769 			p->u.memref.shm = NULL;
770 			p->u.memref.shm_offs = 0;
771 			p->u.memref.size = ip.b;
772 			break;
773 		default:
774 			memset(&p->u, 0, sizeof(p->u));
775 			break;
776 		}
777 	}
778 	return 0;
779 }
780 
tee_ioctl_supp_send(struct tee_context * ctx,struct tee_ioctl_buf_data __user * ubuf)781 static int tee_ioctl_supp_send(struct tee_context *ctx,
782 			       struct tee_ioctl_buf_data __user *ubuf)
783 {
784 	long rc;
785 	struct tee_ioctl_buf_data buf;
786 	struct tee_iocl_supp_send_arg __user *uarg;
787 	struct tee_param *params;
788 	u32 num_params;
789 	u32 ret;
790 
791 	/* Not valid for this driver */
792 	if (!ctx->teedev->desc->ops->supp_send)
793 		return -EINVAL;
794 
795 	if (copy_from_user(&buf, ubuf, sizeof(buf)))
796 		return -EFAULT;
797 
798 	if (buf.buf_len > TEE_MAX_ARG_SIZE ||
799 	    buf.buf_len < sizeof(struct tee_iocl_supp_send_arg))
800 		return -EINVAL;
801 
802 	uarg = u64_to_user_ptr(buf.buf_ptr);
803 	if (get_user(ret, &uarg->ret) ||
804 	    get_user(num_params, &uarg->num_params))
805 		return -EFAULT;
806 
807 	if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) > buf.buf_len)
808 		return -EINVAL;
809 
810 	params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
811 	if (!params)
812 		return -ENOMEM;
813 
814 	rc = params_from_supp(params, num_params, uarg->params);
815 	if (rc)
816 		goto out;
817 
818 	rc = ctx->teedev->desc->ops->supp_send(ctx, ret, num_params, params);
819 out:
820 	kfree(params);
821 	return rc;
822 }
823 
tee_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)824 static long tee_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
825 {
826 	struct tee_context *ctx = filp->private_data;
827 	void __user *uarg = (void __user *)arg;
828 
829 	switch (cmd) {
830 	case TEE_IOC_VERSION:
831 		return tee_ioctl_version(ctx, uarg);
832 	case TEE_IOC_SHM_ALLOC:
833 		return tee_ioctl_shm_alloc(ctx, uarg);
834 	case TEE_IOC_SHM_REGISTER:
835 		return tee_ioctl_shm_register(ctx, uarg);
836 	case TEE_IOC_OPEN_SESSION:
837 		return tee_ioctl_open_session(ctx, uarg);
838 	case TEE_IOC_INVOKE:
839 		return tee_ioctl_invoke(ctx, uarg);
840 	case TEE_IOC_CANCEL:
841 		return tee_ioctl_cancel(ctx, uarg);
842 	case TEE_IOC_CLOSE_SESSION:
843 		return tee_ioctl_close_session(ctx, uarg);
844 	case TEE_IOC_SUPPL_RECV:
845 		return tee_ioctl_supp_recv(ctx, uarg);
846 	case TEE_IOC_SUPPL_SEND:
847 		return tee_ioctl_supp_send(ctx, uarg);
848 	default:
849 		return -EINVAL;
850 	}
851 }
852 
853 static const struct file_operations tee_fops = {
854 	.owner = THIS_MODULE,
855 	.open = tee_open,
856 	.release = tee_release,
857 	.unlocked_ioctl = tee_ioctl,
858 	.compat_ioctl = compat_ptr_ioctl,
859 };
860 
tee_release_device(struct device * dev)861 static void tee_release_device(struct device *dev)
862 {
863 	struct tee_device *teedev = container_of(dev, struct tee_device, dev);
864 
865 	spin_lock(&driver_lock);
866 	clear_bit(teedev->id, dev_mask);
867 	spin_unlock(&driver_lock);
868 	mutex_destroy(&teedev->mutex);
869 	idr_destroy(&teedev->idr);
870 	kfree(teedev);
871 }
872 
873 /**
874  * tee_device_alloc() - Allocate a new struct tee_device instance
875  * @teedesc:	Descriptor for this driver
876  * @dev:	Parent device for this device
877  * @pool:	Shared memory pool, NULL if not used
878  * @driver_data: Private driver data for this device
879  *
880  * Allocates a new struct tee_device instance. The device is
881  * removed by tee_device_unregister().
882  *
883  * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
884  */
tee_device_alloc(const struct tee_desc * teedesc,struct device * dev,struct tee_shm_pool * pool,void * driver_data)885 struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
886 				    struct device *dev,
887 				    struct tee_shm_pool *pool,
888 				    void *driver_data)
889 {
890 	struct tee_device *teedev;
891 	void *ret;
892 	int rc, max_id;
893 	int offs = 0;
894 
895 	if (!teedesc || !teedesc->name || !teedesc->ops ||
896 	    !teedesc->ops->get_version || !teedesc->ops->open ||
897 	    !teedesc->ops->release || !pool)
898 		return ERR_PTR(-EINVAL);
899 
900 	teedev = kzalloc(sizeof(*teedev), GFP_KERNEL);
901 	if (!teedev) {
902 		ret = ERR_PTR(-ENOMEM);
903 		goto err;
904 	}
905 
906 	max_id = TEE_NUM_DEVICES / 2;
907 
908 	if (teedesc->flags & TEE_DESC_PRIVILEGED) {
909 		offs = TEE_NUM_DEVICES / 2;
910 		max_id = TEE_NUM_DEVICES;
911 	}
912 
913 	spin_lock(&driver_lock);
914 	teedev->id = find_next_zero_bit(dev_mask, max_id, offs);
915 	if (teedev->id < max_id)
916 		set_bit(teedev->id, dev_mask);
917 	spin_unlock(&driver_lock);
918 
919 	if (teedev->id >= max_id) {
920 		ret = ERR_PTR(-ENOMEM);
921 		goto err;
922 	}
923 
924 	snprintf(teedev->name, sizeof(teedev->name), "tee%s%d",
925 		 teedesc->flags & TEE_DESC_PRIVILEGED ? "priv" : "",
926 		 teedev->id - offs);
927 
928 	teedev->dev.class = tee_class;
929 	teedev->dev.release = tee_release_device;
930 	teedev->dev.parent = dev;
931 
932 	teedev->dev.devt = MKDEV(MAJOR(tee_devt), teedev->id);
933 
934 	rc = dev_set_name(&teedev->dev, "%s", teedev->name);
935 	if (rc) {
936 		ret = ERR_PTR(rc);
937 		goto err_devt;
938 	}
939 
940 	cdev_init(&teedev->cdev, &tee_fops);
941 	teedev->cdev.owner = teedesc->owner;
942 
943 	dev_set_drvdata(&teedev->dev, driver_data);
944 	device_initialize(&teedev->dev);
945 
946 	/* 1 as tee_device_unregister() does one final tee_device_put() */
947 	teedev->num_users = 1;
948 	init_completion(&teedev->c_no_users);
949 	mutex_init(&teedev->mutex);
950 	idr_init(&teedev->idr);
951 
952 	teedev->desc = teedesc;
953 	teedev->pool = pool;
954 
955 	return teedev;
956 err_devt:
957 	unregister_chrdev_region(teedev->dev.devt, 1);
958 err:
959 	pr_err("could not register %s driver\n",
960 	       teedesc->flags & TEE_DESC_PRIVILEGED ? "privileged" : "client");
961 	if (teedev && teedev->id < TEE_NUM_DEVICES) {
962 		spin_lock(&driver_lock);
963 		clear_bit(teedev->id, dev_mask);
964 		spin_unlock(&driver_lock);
965 	}
966 	kfree(teedev);
967 	return ret;
968 }
969 EXPORT_SYMBOL_GPL(tee_device_alloc);
970 
implementation_id_show(struct device * dev,struct device_attribute * attr,char * buf)971 static ssize_t implementation_id_show(struct device *dev,
972 				      struct device_attribute *attr, char *buf)
973 {
974 	struct tee_device *teedev = container_of(dev, struct tee_device, dev);
975 	struct tee_ioctl_version_data vers;
976 
977 	teedev->desc->ops->get_version(teedev, &vers);
978 	return scnprintf(buf, PAGE_SIZE, "%d\n", vers.impl_id);
979 }
980 static DEVICE_ATTR_RO(implementation_id);
981 
982 static struct attribute *tee_dev_attrs[] = {
983 	&dev_attr_implementation_id.attr,
984 	NULL
985 };
986 
987 ATTRIBUTE_GROUPS(tee_dev);
988 
989 /**
990  * tee_device_register() - Registers a TEE device
991  * @teedev:	Device to register
992  *
993  * tee_device_unregister() need to be called to remove the @teedev if
994  * this function fails.
995  *
996  * @returns < 0 on failure
997  */
tee_device_register(struct tee_device * teedev)998 int tee_device_register(struct tee_device *teedev)
999 {
1000 	int rc;
1001 
1002 	if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
1003 		dev_err(&teedev->dev, "attempt to register twice\n");
1004 		return -EINVAL;
1005 	}
1006 
1007 	teedev->dev.groups = tee_dev_groups;
1008 
1009 	rc = cdev_device_add(&teedev->cdev, &teedev->dev);
1010 	if (rc) {
1011 		dev_err(&teedev->dev,
1012 			"unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
1013 			teedev->name, MAJOR(teedev->dev.devt),
1014 			MINOR(teedev->dev.devt), rc);
1015 		return rc;
1016 	}
1017 
1018 	teedev->flags |= TEE_DEVICE_FLAG_REGISTERED;
1019 	return 0;
1020 }
1021 EXPORT_SYMBOL_GPL(tee_device_register);
1022 
tee_device_put(struct tee_device * teedev)1023 void tee_device_put(struct tee_device *teedev)
1024 {
1025 	mutex_lock(&teedev->mutex);
1026 	/* Shouldn't put in this state */
1027 	if (!WARN_ON(!teedev->desc)) {
1028 		teedev->num_users--;
1029 		if (!teedev->num_users) {
1030 			teedev->desc = NULL;
1031 			complete(&teedev->c_no_users);
1032 		}
1033 	}
1034 	mutex_unlock(&teedev->mutex);
1035 }
1036 
tee_device_get(struct tee_device * teedev)1037 bool tee_device_get(struct tee_device *teedev)
1038 {
1039 	mutex_lock(&teedev->mutex);
1040 	if (!teedev->desc) {
1041 		mutex_unlock(&teedev->mutex);
1042 		return false;
1043 	}
1044 	teedev->num_users++;
1045 	mutex_unlock(&teedev->mutex);
1046 	return true;
1047 }
1048 
1049 /**
1050  * tee_device_unregister() - Removes a TEE device
1051  * @teedev:	Device to unregister
1052  *
1053  * This function should be called to remove the @teedev even if
1054  * tee_device_register() hasn't been called yet. Does nothing if
1055  * @teedev is NULL.
1056  */
tee_device_unregister(struct tee_device * teedev)1057 void tee_device_unregister(struct tee_device *teedev)
1058 {
1059 	if (!teedev)
1060 		return;
1061 
1062 	if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED)
1063 		cdev_device_del(&teedev->cdev, &teedev->dev);
1064 
1065 	tee_device_put(teedev);
1066 	wait_for_completion(&teedev->c_no_users);
1067 
1068 	/*
1069 	 * No need to take a mutex any longer now since teedev->desc was
1070 	 * set to NULL before teedev->c_no_users was completed.
1071 	 */
1072 
1073 	teedev->pool = NULL;
1074 
1075 	put_device(&teedev->dev);
1076 }
1077 EXPORT_SYMBOL_GPL(tee_device_unregister);
1078 
1079 /**
1080  * tee_get_drvdata() - Return driver_data pointer
1081  * @teedev:	Device containing the driver_data pointer
1082  * @returns the driver_data pointer supplied to tee_register().
1083  */
tee_get_drvdata(struct tee_device * teedev)1084 void *tee_get_drvdata(struct tee_device *teedev)
1085 {
1086 	return dev_get_drvdata(&teedev->dev);
1087 }
1088 EXPORT_SYMBOL_GPL(tee_get_drvdata);
1089 
1090 struct match_dev_data {
1091 	struct tee_ioctl_version_data *vers;
1092 	const void *data;
1093 	int (*match)(struct tee_ioctl_version_data *, const void *);
1094 };
1095 
match_dev(struct device * dev,const void * data)1096 static int match_dev(struct device *dev, const void *data)
1097 {
1098 	const struct match_dev_data *match_data = data;
1099 	struct tee_device *teedev = container_of(dev, struct tee_device, dev);
1100 
1101 	teedev->desc->ops->get_version(teedev, match_data->vers);
1102 	return match_data->match(match_data->vers, match_data->data);
1103 }
1104 
1105 struct tee_context *
tee_client_open_context(struct tee_context * start,int (* match)(struct tee_ioctl_version_data *,const void *),const void * data,struct tee_ioctl_version_data * vers)1106 tee_client_open_context(struct tee_context *start,
1107 			int (*match)(struct tee_ioctl_version_data *,
1108 				     const void *),
1109 			const void *data, struct tee_ioctl_version_data *vers)
1110 {
1111 	struct device *dev = NULL;
1112 	struct device *put_dev = NULL;
1113 	struct tee_context *ctx = NULL;
1114 	struct tee_ioctl_version_data v;
1115 	struct match_dev_data match_data = { vers ? vers : &v, data, match };
1116 
1117 	if (start)
1118 		dev = &start->teedev->dev;
1119 
1120 	do {
1121 		dev = class_find_device(tee_class, dev, &match_data, match_dev);
1122 		if (!dev) {
1123 			ctx = ERR_PTR(-ENOENT);
1124 			break;
1125 		}
1126 
1127 		put_device(put_dev);
1128 		put_dev = dev;
1129 
1130 		ctx = teedev_open(container_of(dev, struct tee_device, dev));
1131 	} while (IS_ERR(ctx) && PTR_ERR(ctx) != -ENOMEM);
1132 
1133 	put_device(put_dev);
1134 	/*
1135 	 * Default behaviour for in kernel client is to not wait for
1136 	 * tee-supplicant if not present for any requests in this context.
1137 	 * Also this flag could be configured again before call to
1138 	 * tee_client_open_session() if any in kernel client requires
1139 	 * different behaviour.
1140 	 */
1141 	if (!IS_ERR(ctx))
1142 		ctx->supp_nowait = true;
1143 
1144 	return ctx;
1145 }
1146 EXPORT_SYMBOL_GPL(tee_client_open_context);
1147 
tee_client_close_context(struct tee_context * ctx)1148 void tee_client_close_context(struct tee_context *ctx)
1149 {
1150 	teedev_close_context(ctx);
1151 }
1152 EXPORT_SYMBOL_GPL(tee_client_close_context);
1153 
tee_client_get_version(struct tee_context * ctx,struct tee_ioctl_version_data * vers)1154 void tee_client_get_version(struct tee_context *ctx,
1155 			    struct tee_ioctl_version_data *vers)
1156 {
1157 	ctx->teedev->desc->ops->get_version(ctx->teedev, vers);
1158 }
1159 EXPORT_SYMBOL_GPL(tee_client_get_version);
1160 
tee_client_open_session(struct tee_context * ctx,struct tee_ioctl_open_session_arg * arg,struct tee_param * param)1161 int tee_client_open_session(struct tee_context *ctx,
1162 			    struct tee_ioctl_open_session_arg *arg,
1163 			    struct tee_param *param)
1164 {
1165 	if (!ctx->teedev->desc->ops->open_session)
1166 		return -EINVAL;
1167 	return ctx->teedev->desc->ops->open_session(ctx, arg, param);
1168 }
1169 EXPORT_SYMBOL_GPL(tee_client_open_session);
1170 
tee_client_close_session(struct tee_context * ctx,u32 session)1171 int tee_client_close_session(struct tee_context *ctx, u32 session)
1172 {
1173 	if (!ctx->teedev->desc->ops->close_session)
1174 		return -EINVAL;
1175 	return ctx->teedev->desc->ops->close_session(ctx, session);
1176 }
1177 EXPORT_SYMBOL_GPL(tee_client_close_session);
1178 
tee_client_invoke_func(struct tee_context * ctx,struct tee_ioctl_invoke_arg * arg,struct tee_param * param)1179 int tee_client_invoke_func(struct tee_context *ctx,
1180 			   struct tee_ioctl_invoke_arg *arg,
1181 			   struct tee_param *param)
1182 {
1183 	if (!ctx->teedev->desc->ops->invoke_func)
1184 		return -EINVAL;
1185 	return ctx->teedev->desc->ops->invoke_func(ctx, arg, param);
1186 }
1187 EXPORT_SYMBOL_GPL(tee_client_invoke_func);
1188 
tee_client_cancel_req(struct tee_context * ctx,struct tee_ioctl_cancel_arg * arg)1189 int tee_client_cancel_req(struct tee_context *ctx,
1190 			  struct tee_ioctl_cancel_arg *arg)
1191 {
1192 	if (!ctx->teedev->desc->ops->cancel_req)
1193 		return -EINVAL;
1194 	return ctx->teedev->desc->ops->cancel_req(ctx, arg->cancel_id,
1195 						  arg->session);
1196 }
1197 
tee_client_device_match(struct device * dev,struct device_driver * drv)1198 static int tee_client_device_match(struct device *dev,
1199 				   struct device_driver *drv)
1200 {
1201 	const struct tee_client_device_id *id_table;
1202 	struct tee_client_device *tee_device;
1203 
1204 	id_table = to_tee_client_driver(drv)->id_table;
1205 	tee_device = to_tee_client_device(dev);
1206 
1207 	while (!uuid_is_null(&id_table->uuid)) {
1208 		if (uuid_equal(&tee_device->id.uuid, &id_table->uuid))
1209 			return 1;
1210 		id_table++;
1211 	}
1212 
1213 	return 0;
1214 }
1215 
tee_client_device_uevent(struct device * dev,struct kobj_uevent_env * env)1216 static int tee_client_device_uevent(struct device *dev,
1217 				    struct kobj_uevent_env *env)
1218 {
1219 	uuid_t *dev_id = &to_tee_client_device(dev)->id.uuid;
1220 
1221 	return add_uevent_var(env, "MODALIAS=tee:%pUb", dev_id);
1222 }
1223 
1224 struct bus_type tee_bus_type = {
1225 	.name		= "tee",
1226 	.match		= tee_client_device_match,
1227 	.uevent		= tee_client_device_uevent,
1228 };
1229 EXPORT_SYMBOL_GPL(tee_bus_type);
1230 
tee_init(void)1231 static int __init tee_init(void)
1232 {
1233 	int rc;
1234 
1235 	tee_class = class_create(THIS_MODULE, "tee");
1236 	if (IS_ERR(tee_class)) {
1237 		pr_err("couldn't create class\n");
1238 		return PTR_ERR(tee_class);
1239 	}
1240 
1241 	rc = alloc_chrdev_region(&tee_devt, 0, TEE_NUM_DEVICES, "tee");
1242 	if (rc) {
1243 		pr_err("failed to allocate char dev region\n");
1244 		goto out_unreg_class;
1245 	}
1246 
1247 	rc = bus_register(&tee_bus_type);
1248 	if (rc) {
1249 		pr_err("failed to register tee bus\n");
1250 		goto out_unreg_chrdev;
1251 	}
1252 
1253 	return 0;
1254 
1255 out_unreg_chrdev:
1256 	unregister_chrdev_region(tee_devt, TEE_NUM_DEVICES);
1257 out_unreg_class:
1258 	class_destroy(tee_class);
1259 	tee_class = NULL;
1260 
1261 	return rc;
1262 }
1263 
tee_exit(void)1264 static void __exit tee_exit(void)
1265 {
1266 	bus_unregister(&tee_bus_type);
1267 	unregister_chrdev_region(tee_devt, TEE_NUM_DEVICES);
1268 	class_destroy(tee_class);
1269 	tee_class = NULL;
1270 }
1271 
1272 subsys_initcall(tee_init);
1273 module_exit(tee_exit);
1274 
1275 MODULE_AUTHOR("Linaro");
1276 MODULE_DESCRIPTION("TEE Driver");
1277 MODULE_VERSION("1.0");
1278 MODULE_LICENSE("GPL v2");
1279