summaryrefslogtreecommitdiff
path: root/security/tf_driver/tf_device.c
blob: 47ff39e6561d47990979e70a0e7c6d233d3eb12b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/**
 * Copyright (c) 2011 Trusted Logic S.A.
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <linux/atomic.h>
#include <linux/uaccess.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/page-flags.h>
#include <linux/pm.h>
#include <linux/sysdev.h>
#include <linux/vmalloc.h>
#include <linux/signal.h>
#ifdef CONFIG_ANDROID
#include <linux/device.h>
#endif

#include "tf_protocol.h"
#include "tf_defs.h"
#include "tf_util.h"
#include "tf_conn.h"
#include "tf_comm.h"
#ifdef CONFIG_TF_ZEBRA
#include <plat/cpu.h>
#include "tf_zebra.h"
#endif
#ifdef CONFIG_TF_DRIVER_CRYPTO_FIPS
#include "tf_crypto.h"
#endif

#include "s_version.h"

/*----------------------------------------------------------------------------
 * Forward Declarations
 *----------------------------------------------------------------------------*/

/*
 * Creates and registers the device to be managed by the specified driver.
 *
 * Returns zero upon successful completion, or an appropriate error code upon
 * failure.
 */
static int tf_device_register(void);


/*
 * Implements the device Open callback.
 */
static int tf_device_open(
		struct inode *inode,
		struct file *file);


/*
 * Implements the device Release callback.
 */
static int tf_device_release(
		struct inode *inode,
		struct file *file);


/*
 * Implements the device ioctl callback.
 */
static long tf_device_ioctl(
		struct file *file,
		unsigned int ioctl_num,
		unsigned long ioctl_param);


/*
 * Implements the device shutdown callback.
 */
static int tf_device_shutdown(
		struct sys_device *sysdev);


/*
 * Implements the device suspend callback.
 */
static int tf_device_suspend(
		struct sys_device *sysdev,
		pm_message_t state);


/*
 * Implements the device resume callback.
 */
static int tf_device_resume(
		struct sys_device *sysdev);


/*---------------------------------------------------------------------------
 * Module Parameters
 *---------------------------------------------------------------------------*/

/*
 * The device major number used to register a unique character device driver.
 * Let the default value be 122
 */
static int device_major_number = 122;

module_param(device_major_number, int, 0000);
MODULE_PARM_DESC(device_major_number,
	"The device major number used to register a unique character "
	"device driver");

#ifdef CONFIG_TF_TRUSTZONE
/**
 * The softint interrupt line used by the Secure World.
 */
static int soft_interrupt = -1;

module_param(soft_interrupt, int, 0000);
MODULE_PARM_DESC(soft_interrupt,
	"The softint interrupt line used by the Secure world");
#endif

#ifdef CONFIG_TF_DRIVER_DEBUG_SUPPORT
unsigned tf_debug_level = UINT_MAX;
module_param_named(debug, tf_debug_level, uint, 0644);
#endif

#ifdef CONFIG_TF_DRIVER_CRYPTO_FIPS
char *tf_integrity_hmac_sha256_expected_value;
module_param_named(hmac_sha256, tf_integrity_hmac_sha256_expected_value,
		   charp, 0444);

#ifdef CONFIG_TF_DRIVER_FAULT_INJECTION
unsigned tf_fault_injection_mask;
module_param_named(fault, tf_fault_injection_mask, uint, 0644);
#endif

int tf_self_test_blkcipher_align;
module_param_named(post_align, tf_self_test_blkcipher_align, int, 0644);
int tf_self_test_blkcipher_use_vmalloc;
module_param_named(post_vmalloc, tf_self_test_blkcipher_use_vmalloc, int, 0644);
#endif

#ifdef CONFIG_ANDROID
static struct class *tf_class;
#endif

/*
 * Interfaces the system device with the kernel.
 */
struct sys_device g_tf_sysdev;

/*----------------------------------------------------------------------------
 * Global Variables
 *----------------------------------------------------------------------------*/

/*
 * tf_driver character device definitions.
 * read and write methods are not defined
 * and will return an error if used by user space
 */
static const struct file_operations g_tf_device_file_ops = {
	.owner = THIS_MODULE,
	.open = tf_device_open,
	.release = tf_device_release,
	.unlocked_ioctl = tf_device_ioctl,
	.llseek = no_llseek,
};


static struct sysdev_class g_tf_device_sys_class = {
	.name = TF_DEVICE_BASE_NAME,
	.shutdown = tf_device_shutdown,
	.suspend = tf_device_suspend,
	.resume = tf_device_resume,
};

/* The single device supported by this driver */
static struct tf_device g_tf_dev;

/*----------------------------------------------------------------------------
 * Implementations
 *----------------------------------------------------------------------------*/

struct tf_device *tf_get_device(void)
{
	return &g_tf_dev;
}

/*
 * sysfs entries
 */
struct tf_sysfs_entry {
	struct attribute attr;
	ssize_t (*show)(struct tf_device *, char *);
	ssize_t (*store)(struct tf_device *, const char *, size_t);
};

/*
 * sysfs entry showing allocation stats
 */
static ssize_t info_show(struct tf_device *dev, char *buf)
{
	struct tf_device_stats *dev_stats = &dev->stats;

	return snprintf(buf, PAGE_SIZE,
		"stat.memories.allocated: %d\n"
		"stat.pages.allocated:    %d\n"
		"stat.pages.locked:       %d\n",
		atomic_read(&dev_stats->stat_memories_allocated),
		atomic_read(&dev_stats->stat_pages_allocated),
		atomic_read(&dev_stats->stat_pages_locked));
}
static struct tf_sysfs_entry tf_info_entry = __ATTR_RO(info);

#ifdef CONFIG_TF_ZEBRA
/*
 * sysfs entry showing whether secure world is up and running
 */
static ssize_t tf_started_show(struct tf_device *dev, char *buf)
{
	int tf_started = test_bit(TF_COMM_FLAG_PA_AVAILABLE,
		&dev->sm.flags);

	return snprintf(buf, PAGE_SIZE, "%s\n", tf_started ? "yes" : "no");
}
static struct tf_sysfs_entry tf_started_entry =
	__ATTR_RO(tf_started);

static ssize_t workspace_addr_show(struct tf_device *dev, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "0x%08x\n", dev->workspace_addr);
}
static struct tf_sysfs_entry tf_workspace_addr_entry =
	__ATTR_RO(workspace_addr);

static ssize_t workspace_size_show(struct tf_device *dev, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "0x%08x\n", dev->workspace_size);
}
static struct tf_sysfs_entry tf_workspace_size_entry =
	__ATTR_RO(workspace_size);
#endif

static ssize_t tf_attr_show(struct kobject *kobj, struct attribute *attr,
	char *page)
{
	struct tf_sysfs_entry *entry = container_of(attr, struct tf_sysfs_entry,
		attr);
	struct tf_device *dev = container_of(kobj, struct tf_device, kobj);

	if (!entry->show)
		return -EIO;

	return entry->show(dev, page);
}

static ssize_t tf_attr_store(struct kobject *kobj, struct attribute *attr,
	const char *page, size_t length)
{
	struct tf_sysfs_entry *entry = container_of(attr, struct tf_sysfs_entry,
		attr);
	struct tf_device *dev = container_of(kobj, struct tf_device, kobj);

	if (!entry->store)
		return -EIO;

	return entry->store(dev, page, length);
}

static void tf_kobj_release(struct kobject *kobj) {}

static struct attribute *tf_default_attrs[] = {
	&tf_info_entry.attr,
#ifdef CONFIG_TF_ZEBRA
	&tf_started_entry.attr,
	&tf_workspace_addr_entry.attr,
	&tf_workspace_size_entry.attr,
#endif
	NULL,
};
static const struct sysfs_ops tf_sysfs_ops = {
	.show	= tf_attr_show,
	.store	= tf_attr_store,
};
static struct kobj_type tf_ktype = {
	.release	= tf_kobj_release,
	.sysfs_ops	= &tf_sysfs_ops,
	.default_attrs	= tf_default_attrs
};

/*----------------------------------------------------------------------------*/

#if defined(MODULE) && defined(CONFIG_TF_ZEBRA)
static char *smc_mem;
module_param(smc_mem, charp, S_IRUGO);
#endif

/*
 * First routine called when the kernel module is loaded
 */
static int __init tf_device_register(void)
{
	int error;
	struct tf_device *dev = &g_tf_dev;

	dprintk(KERN_INFO "tf_device_register()\n");

	/*
	 * Initialize the device
	 */
	dev->dev_number = MKDEV(device_major_number,
		TF_DEVICE_MINOR_NUMBER);
	cdev_init(&dev->cdev, &g_tf_device_file_ops);
	dev->cdev.owner = THIS_MODULE;

	g_tf_sysdev.id = 0;
	g_tf_sysdev.cls = &g_tf_device_sys_class;

	INIT_LIST_HEAD(&dev->connection_list);
	spin_lock_init(&dev->connection_list_lock);

#if defined(MODULE) && defined(CONFIG_TF_ZEBRA)
	error = (*tf_comm_early_init)();
	if (error)
		goto module_early_init_failed;

	error = tf_device_mshield_init(smc_mem);
	if (error)
		goto mshield_init_failed;

#ifdef CONFIG_TF_DRIVER_CRYPTO_FIPS
	error = tf_crypto_hmac_module_init();
	if (error)
		goto hmac_init_failed;

	error = tf_self_test_register_device();
	if (error)
		goto self_test_register_device_failed;
#endif
#endif

	/* register the sysfs object driver stats */
	error = kobject_init_and_add(&dev->kobj,  &tf_ktype, NULL, "%s",
		 TF_DEVICE_BASE_NAME);
	if (error) {
		printk(KERN_ERR "tf_device_register(): "
			"kobject_init_and_add failed (error %d)!\n", error);
		kobject_put(&dev->kobj);
		goto kobject_init_and_add_failed;
	}

	/*
	 * Register the system device.
	 */

	error = sysdev_class_register(&g_tf_device_sys_class);
	if (error != 0) {
		printk(KERN_ERR "tf_device_register():"
			" sysdev_class_register failed (error %d)!\n",
			error);
		goto sysdev_class_register_failed;
	}

	error = sysdev_register(&g_tf_sysdev);
	if (error != 0) {
		dprintk(KERN_ERR "tf_device_register(): "
			"sysdev_register failed (error %d)!\n",
			error);
		goto sysdev_register_failed;
	}

	/*
	 * Register the char device.
	 */
	printk(KERN_INFO "Registering char device %s (%u:%u)\n",
		TF_DEVICE_BASE_NAME,
		MAJOR(dev->dev_number),
		MINOR(dev->dev_number));
	error = register_chrdev_region(dev->dev_number, 1,
		TF_DEVICE_BASE_NAME);
	if (error != 0) {
		printk(KERN_ERR "tf_device_register():"
			" register_chrdev_region failed (error %d)!\n",
			error);
		goto register_chrdev_region_failed;
	}

	error = cdev_add(&dev->cdev, dev->dev_number, 1);
	if (error != 0) {
		printk(KERN_ERR "tf_device_register(): "
			"cdev_add failed (error %d)!\n",
			error);
		goto cdev_add_failed;
	}

	/*
	 * Initialize the communication with the Secure World.
	 */
#ifdef CONFIG_TF_TRUSTZONE
	dev->sm.soft_int_irq = soft_interrupt;
#endif
	error = tf_init(&g_tf_dev.sm);
	if (error != S_SUCCESS) {
		dprintk(KERN_ERR "tf_device_register(): "
			"tf_init failed (error %d)!\n",
			error);
		goto init_failed;
	}

#ifdef CONFIG_TF_DRIVER_CRYPTO_FIPS
	error = tf_self_test_post_init(&(dev_stats->kobj));
	/* N.B. error > 0 indicates a POST failure, which will not
	   prevent the module from loading. */
	if (error < 0) {
		dprintk(KERN_ERR "tf_device_register(): "
			"tf_self_test_post_vectors failed (error %d)!\n",
			error);
		goto post_failed;
	}
#endif

#ifdef CONFIG_ANDROID
	tf_class = class_create(THIS_MODULE, TF_DEVICE_BASE_NAME);
	device_create(tf_class, NULL,
		dev->dev_number,
		NULL, TF_DEVICE_BASE_NAME);
#endif

#ifdef CONFIG_TF_ZEBRA
	/*
	 * Initializes the /dev/tf_ctrl device node.
	 */
	error = tf_ctrl_device_register();
	if (error)
		goto ctrl_failed;
#endif

#ifdef CONFIG_TF_DRIVER_DEBUG_SUPPORT
	address_cache_property((unsigned long) &tf_device_register);
#endif
	/*
	 * Successful completion.
	 */

	dprintk(KERN_INFO "tf_device_register(): Success\n");
	return 0;

	/*
	 * Error: undo all operations in the reverse order
	 */
#ifdef CONFIG_TF_ZEBRA
ctrl_failed:
#endif
#ifdef CONFIG_TF_DRIVER_CRYPTO_FIPS
	tf_self_test_post_exit();
post_failed:
#endif
init_failed:
	cdev_del(&dev->cdev);
cdev_add_failed:
	unregister_chrdev_region(dev->dev_number, 1);
register_chrdev_region_failed:
	sysdev_unregister(&g_tf_sysdev);
sysdev_register_failed:
	sysdev_class_unregister(&g_tf_device_sys_class);
sysdev_class_register_failed:
kobject_init_and_add_failed:
	kobject_del(&g_tf_dev.kobj);

#if defined(MODULE) && defined(CONFIG_TF_ZEBRA)
#ifdef CONFIG_TF_DRIVER_CRYPTO_FIPS
	tf_self_test_unregister_device();
self_test_register_device_failed:
	tf_crypto_hmac_module_exit();
hmac_init_failed:
#endif
	tf_device_mshield_exit();
mshield_init_failed:
module_early_init_failed:
#endif
	dprintk(KERN_INFO "tf_device_register(): Failure (error %d)\n",
		error);
	return error;
}

/*----------------------------------------------------------------------------*/

static int tf_device_open(struct inode *inode, struct file *file)
{
	int error;
	struct tf_device *dev = &g_tf_dev;
	struct tf_connection *connection = NULL;

	dprintk(KERN_INFO "tf_device_open(%u:%u, %p)\n",
		imajor(inode), iminor(inode), file);

	/* Dummy lseek for non-seekable driver */
	error = nonseekable_open(inode, file);
	if (error != 0) {
		dprintk(KERN_ERR "tf_device_open(%p): "
			"nonseekable_open failed (error %d)!\n",
			file, error);
		goto error;
	}

#ifndef CONFIG_ANDROID
	/*
	 * Check file flags. We only autthorize the O_RDWR access
	 */
	if (file->f_flags != O_RDWR) {
		dprintk(KERN_ERR "tf_device_open(%p): "
			"Invalid access mode %u\n",
			file, file->f_flags);
		error = -EACCES;
		goto error;
	}
#endif

	/*
	 * Open a new connection.
	 */

	error = tf_open(dev, file, &connection);
	if (error != 0) {
		dprintk(KERN_ERR "tf_device_open(%p): "
			"tf_open failed (error %d)!\n",
			file, error);
		goto error;
	}

	file->private_data = connection;

	/*
	 * Send the CreateDeviceContext command to the secure
	 */
	error = tf_create_device_context(connection);
	if (error != 0) {
		dprintk(KERN_ERR "tf_device_open(%p): "
			"tf_create_device_context failed (error %d)!\n",
			file, error);
		goto error1;
	}

	/*
	 * Successful completion.
	 */

	dprintk(KERN_INFO "tf_device_open(%p): Success (connection=%p)\n",
		file, connection);
	return 0;

	/*
	 * Error handling.
	 */

error1:
	tf_close(connection);
error:
	dprintk(KERN_INFO "tf_device_open(%p): Failure (error %d)\n",
		file, error);
	return error;
}

/*----------------------------------------------------------------------------*/

static int tf_device_release(struct inode *inode, struct file *file)
{
	struct tf_connection *connection;

	dprintk(KERN_INFO "tf_device_release(%u:%u, %p)\n",
		imajor(inode), iminor(inode), file);

	connection = tf_conn_from_file(file);
	tf_close(connection);

	dprintk(KERN_INFO "tf_device_release(%p): Success\n", file);
	return 0;
}

/*----------------------------------------------------------------------------*/

static long tf_device_ioctl(struct file *file, unsigned int ioctl_num,
	unsigned long ioctl_param)
{
	int result = S_SUCCESS;
	struct tf_connection *connection;
	union tf_command command;
	struct tf_command_header header;
	union tf_answer answer;
	u32 command_size;
	u32 answer_size;
	void *user_answer;

	dprintk(KERN_INFO "tf_device_ioctl(%p, %u, %p)\n",
		file, ioctl_num, (void *) ioctl_param);

	switch (ioctl_num) {
	case IOCTL_TF_GET_VERSION:
		/* ioctl is asking for the driver interface version */
		result = TF_DRIVER_INTERFACE_VERSION;
		goto exit;

	case IOCTL_TF_EXCHANGE:
		/*
		 * ioctl is asking to perform a message exchange with the Secure
		 * Module
		 */

		/*
		 * Make a local copy of the data from the user application
		 * This routine checks the data is readable
		 *
		 * Get the header first.
		 */
		if (copy_from_user(&header,
				(struct tf_command_header *)ioctl_param,
				sizeof(struct tf_command_header))) {
			dprintk(KERN_ERR "tf_device_ioctl(%p): "
				"Cannot access ioctl parameter %p\n",
				file, (void *) ioctl_param);
			result = -EFAULT;
			goto exit;
		}

		/* size in words of u32 */
		command_size = header.message_size +
			sizeof(struct tf_command_header)/sizeof(u32);
		if (command_size > sizeof(command)/sizeof(u32)) {
			dprintk(KERN_ERR "tf_device_ioctl(%p): "
				"Buffer overflow: too many bytes to copy %d\n",
				file, command_size);
			result = -EFAULT;
			goto exit;
		}

		if (copy_from_user(&command,
				(union tf_command *)ioctl_param,
				command_size * sizeof(u32))) {
			dprintk(KERN_ERR "tf_device_ioctl(%p): "
				"Cannot access ioctl parameter %p\n",
				file, (void *) ioctl_param);
			result = -EFAULT;
			goto exit;
		}

		connection = tf_conn_from_file(file);
		BUG_ON(connection == NULL);

		/*
		 * The answer memory space address is in the operation_id field
		 */
		user_answer = (void *) command.header.operation_id;

		atomic_inc(&(connection->pending_op_count));

		dprintk(KERN_WARNING "tf_device_ioctl(%p): "
			"Sending message type  0x%08x\n",
			file, command.header.message_type);

		switch (command.header.message_type) {
		case TF_MESSAGE_TYPE_OPEN_CLIENT_SESSION:
			result = tf_open_client_session(connection,
				&command, &answer);
			break;

		case TF_MESSAGE_TYPE_CLOSE_CLIENT_SESSION:
			result = tf_close_client_session(connection,
				&command, &answer);
			break;

		case TF_MESSAGE_TYPE_REGISTER_SHARED_MEMORY:
			result = tf_register_shared_memory(connection,
				&command, &answer);
			break;

		case TF_MESSAGE_TYPE_RELEASE_SHARED_MEMORY:
			result = tf_release_shared_memory(connection,
				&command, &answer);
			break;

		case TF_MESSAGE_TYPE_INVOKE_CLIENT_COMMAND:
			result = tf_invoke_client_command(connection,
				&command, &answer);
			break;

		case TF_MESSAGE_TYPE_CANCEL_CLIENT_COMMAND:
			result = tf_cancel_client_command(connection,
				&command, &answer);
			break;

		default:
			dprintk(KERN_ERR "tf_device_ioctl(%p): "
				"Incorrect message type (0x%08x)!\n",
				connection, command.header.message_type);
			result = -EOPNOTSUPP;
			break;
		}

		atomic_dec(&(connection->pending_op_count));

		if (result != 0) {
			dprintk(KERN_WARNING "tf_device_ioctl(%p): "
				"Operation returning error code 0x%08x)!\n",
				file, result);
			goto exit;
		}

		/*
		 * Copy the answer back to the user space application.
		 * The driver does not check this field, only copy back to user
		 * space the data handed over by Secure World
		 */
		answer_size = answer.header.message_size +
			sizeof(struct tf_answer_header)/sizeof(u32);
		if (copy_to_user(user_answer,
				&answer, answer_size * sizeof(u32))) {
			dprintk(KERN_WARNING "tf_device_ioctl(%p): "
				"Failed to copy back the full command "
				"answer to %p\n", file, user_answer);
			result = -EFAULT;
			goto exit;
		}

		/* successful completion */
		dprintk(KERN_INFO "tf_device_ioctl(%p): Success\n", file);
		break;

	case  IOCTL_TF_GET_DESCRIPTION: {
		/* ioctl asking for the version information buffer */
		struct tf_version_information_buffer *pInfoBuffer;

		dprintk(KERN_INFO "IOCTL_TF_GET_DESCRIPTION:(%p, %u, %p)\n",
			file, ioctl_num, (void *) ioctl_param);

		pInfoBuffer =
			((struct tf_version_information_buffer *) ioctl_param);

		dprintk(KERN_INFO "IOCTL_TF_GET_DESCRIPTION1: "
			"driver_description=\"%64s\"\n", S_VERSION_STRING);

		if (copy_to_user(pInfoBuffer->driver_description,
				S_VERSION_STRING,
				strlen(S_VERSION_STRING) + 1)) {
			dprintk(KERN_ERR "tf_device_ioctl(%p): "
				"Fail to copy back the driver description "
				"to  %p\n",
				file, pInfoBuffer->driver_description);
			result = -EFAULT;
			goto exit;
		}

		dprintk(KERN_INFO "IOCTL_TF_GET_DESCRIPTION2: "
			"secure_world_description=\"%64s\"\n",
			tf_get_description(&g_tf_dev.sm));

		if (copy_to_user(pInfoBuffer->secure_world_description,
				tf_get_description(&g_tf_dev.sm),
				TF_DESCRIPTION_BUFFER_LENGTH)) {
			dprintk(KERN_WARNING "tf_device_ioctl(%p): "
				"Failed to copy back the secure world "
				"description to %p\n",
				file, pInfoBuffer->secure_world_description);
			result = -EFAULT;
			goto exit;
		}
		break;
	}

	default:
		dprintk(KERN_ERR "tf_device_ioctl(%p): "
			"Unknown IOCTL code 0x%08x!\n",
			file, ioctl_num);
		result = -EOPNOTSUPP;
		goto exit;
	}

exit:
	return result;
}

/*----------------------------------------------------------------------------*/

static int tf_device_shutdown(struct sys_device *sysdev)
{

	return tf_power_management(&g_tf_dev.sm,
		TF_POWER_OPERATION_SHUTDOWN);
}

/*----------------------------------------------------------------------------*/

static int tf_device_suspend(struct sys_device *sysdev, pm_message_t state)
{
	dprintk(KERN_INFO "tf_device_suspend: Enter\n");
	return tf_power_management(&g_tf_dev.sm,
		TF_POWER_OPERATION_HIBERNATE);
}


/*----------------------------------------------------------------------------*/

static int tf_device_resume(struct sys_device *sysdev)
{
	return tf_power_management(&g_tf_dev.sm,
		TF_POWER_OPERATION_RESUME);
}


/*----------------------------------------------------------------------------*/

module_init(tf_device_register);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Trusted Logic S.A.");