security: tf_driver: remove tegra_read_cycle()
[linux-3.10.git] / security / tf_driver / tf_device.c
1 /**
2  * Copyright (c) 2011 Trusted Logic S.A.
3  * All Rights Reserved.
4  *
5  * Copyright (C) 2011-2012 NVIDIA Corporation.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19  * MA 02111-1307 USA
20  */
21
22 #include <linux/atomic.h>
23 #include <linux/uaccess.h>
24 #include <linux/module.h>
25 #include <linux/errno.h>
26 #include <linux/mm.h>
27 #include <linux/page-flags.h>
28 #include <linux/pm.h>
29 #include <linux/syscore_ops.h>
30 #include <linux/vmalloc.h>
31 #include <linux/signal.h>
32 #ifdef CONFIG_ANDROID
33 #include <linux/device.h>
34 #endif
35
36 #include <trace/events/nvsecurity.h>
37
38 #include "tf_protocol.h"
39 #include "tf_defs.h"
40 #include "tf_util.h"
41 #include "tf_conn.h"
42 #include "tf_comm.h"
43 #ifdef CONFIG_TF_ZEBRA
44 #include <plat/cpu.h>
45 #include "tf_zebra.h"
46 #endif
47 #ifdef CONFIG_TF_DRIVER_CRYPTO_FIPS
48 #include "tf_crypto.h"
49 #endif
50
51 #include "s_version.h"
52
53 /*----------------------------------------------------------------------------
54  * Forward Declarations
55  *----------------------------------------------------------------------------*/
56
57 /*
58  * Creates and registers the device to be managed by the specified driver.
59  *
60  * Returns zero upon successful completion, or an appropriate error code upon
61  * failure.
62  */
63 static int tf_device_register(void);
64
65
66 /*
67  * Implements the device Open callback.
68  */
69 static int tf_device_open(
70                 struct inode *inode,
71                 struct file *file);
72
73
74 /*
75  * Implements the device Release callback.
76  */
77 static int tf_device_release(
78                 struct inode *inode,
79                 struct file *file);
80
81
82 /*
83  * Implements the device ioctl callback.
84  */
85 static long tf_device_ioctl(
86                 struct file *file,
87                 unsigned int ioctl_num,
88                 unsigned long ioctl_param);
89
90
91 /*
92  * Implements the device shutdown callback.
93  */
94 static void tf_device_shutdown(void);
95
96
97 /*
98  * Implements the device suspend callback.
99  */
100 static int tf_device_suspend(void);
101
102
103 /*
104  * Implements the device resume callback.
105  */
106 static void tf_device_resume(void);
107
108
109 /*---------------------------------------------------------------------------
110  * Module Parameters
111  *---------------------------------------------------------------------------*/
112
113 /*
114  * The device major number used to register a unique character device driver.
115  * Let the default value be 122
116  */
117 static int device_major_number = 122;
118
119 module_param(device_major_number, int, 0000);
120 MODULE_PARM_DESC(device_major_number,
121         "The device major number used to register a unique character "
122         "device driver");
123
124 #ifdef CONFIG_TF_TRUSTZONE
125 /**
126  * The softint interrupt line used by the Secure World.
127  */
128 static int soft_interrupt = -1;
129
130 module_param(soft_interrupt, int, 0000);
131 MODULE_PARM_DESC(soft_interrupt,
132         "The softint interrupt line used by the Secure world");
133 #endif
134
135 #ifdef CONFIG_TF_DRIVER_DEBUG_SUPPORT
136 unsigned tf_debug_level = UINT_MAX;
137 module_param_named(debug, tf_debug_level, uint, 0644);
138 #endif
139
140 #ifdef CONFIG_TF_DRIVER_CRYPTO_FIPS
141 char *tf_integrity_hmac_sha256_expected_value;
142 module_param_named(hmac_sha256, tf_integrity_hmac_sha256_expected_value,
143                    charp, 0444);
144
145 #ifdef CONFIG_TF_DRIVER_FAULT_INJECTION
146 unsigned tf_fault_injection_mask;
147 module_param_named(fault, tf_fault_injection_mask, uint, 0644);
148 #endif
149
150 int tf_self_test_blkcipher_align;
151 module_param_named(post_align, tf_self_test_blkcipher_align, int, 0644);
152 int tf_self_test_blkcipher_use_vmalloc;
153 module_param_named(post_vmalloc, tf_self_test_blkcipher_use_vmalloc, int, 0644);
154 #endif
155
156 #ifdef CONFIG_ANDROID
157 static struct class *tf_class;
158 #endif
159
160 /*----------------------------------------------------------------------------
161  * Global Variables
162  *----------------------------------------------------------------------------*/
163
164 /*
165  * tf_driver character device definitions.
166  * read and write methods are not defined
167  * and will return an error if used by user space
168  */
169 static const struct file_operations g_tf_device_file_ops = {
170         .owner = THIS_MODULE,
171         .open = tf_device_open,
172         .release = tf_device_release,
173         .unlocked_ioctl = tf_device_ioctl,
174         .llseek = no_llseek,
175 };
176
177
178 static struct syscore_ops g_tf_device_syscore_ops = {
179         .shutdown = tf_device_shutdown,
180         .suspend = tf_device_suspend,
181         .resume = tf_device_resume,
182 };
183
184 /* The single device supported by this driver */
185 static struct tf_device g_tf_dev;
186
187 /*----------------------------------------------------------------------------
188  * Implementations
189  *----------------------------------------------------------------------------*/
190
191 struct tf_device *tf_get_device(void)
192 {
193         return &g_tf_dev;
194 }
195
196 /*
197  * sysfs entries
198  */
199 struct tf_sysfs_entry {
200         struct attribute attr;
201         ssize_t (*show)(struct tf_device *, char *);
202         ssize_t (*store)(struct tf_device *, const char *, size_t);
203 };
204
205 /*
206  * sysfs entry showing allocation stats
207  */
208 static ssize_t info_show(struct tf_device *dev, char *buf)
209 {
210         struct tf_device_stats *dev_stats = &dev->stats;
211
212         return snprintf(buf, PAGE_SIZE,
213                 "stat.memories.allocated: %d\n"
214                 "stat.pages.allocated:    %d\n"
215                 "stat.pages.locked:       %d\n",
216                 atomic_read(&dev_stats->stat_memories_allocated),
217                 atomic_read(&dev_stats->stat_pages_allocated),
218                 atomic_read(&dev_stats->stat_pages_locked));
219 }
220 static struct tf_sysfs_entry tf_info_entry = __ATTR_RO(info);
221
222 #ifdef CONFIG_TF_ZEBRA
223 /*
224  * sysfs entry showing whether secure world is up and running
225  */
226 static ssize_t tf_started_show(struct tf_device *dev, char *buf)
227 {
228         int tf_started = test_bit(TF_COMM_FLAG_PA_AVAILABLE,
229                 &dev->sm.flags);
230
231         return snprintf(buf, PAGE_SIZE, "%s\n", tf_started ? "yes" : "no");
232 }
233 static struct tf_sysfs_entry tf_started_entry =
234         __ATTR_RO(tf_started);
235
236 static ssize_t workspace_addr_show(struct tf_device *dev, char *buf)
237 {
238         return snprintf(buf, PAGE_SIZE, "0x%08x\n", dev->workspace_addr);
239 }
240 static struct tf_sysfs_entry tf_workspace_addr_entry =
241         __ATTR_RO(workspace_addr);
242
243 static ssize_t workspace_size_show(struct tf_device *dev, char *buf)
244 {
245         return snprintf(buf, PAGE_SIZE, "0x%08x\n", dev->workspace_size);
246 }
247 static struct tf_sysfs_entry tf_workspace_size_entry =
248         __ATTR_RO(workspace_size);
249 #endif
250
251 static ssize_t tf_attr_show(struct kobject *kobj, struct attribute *attr,
252         char *page)
253 {
254         struct tf_sysfs_entry *entry = container_of(attr, struct tf_sysfs_entry,
255                 attr);
256         struct tf_device *dev = container_of(kobj, struct tf_device, kobj);
257
258         if (!entry->show)
259                 return -EIO;
260
261         return entry->show(dev, page);
262 }
263
264 static ssize_t tf_attr_store(struct kobject *kobj, struct attribute *attr,
265         const char *page, size_t length)
266 {
267         struct tf_sysfs_entry *entry = container_of(attr, struct tf_sysfs_entry,
268                 attr);
269         struct tf_device *dev = container_of(kobj, struct tf_device, kobj);
270
271         if (!entry->store)
272                 return -EIO;
273
274         return entry->store(dev, page, length);
275 }
276
277 static void tf_kobj_release(struct kobject *kobj) {}
278
279 static struct attribute *tf_default_attrs[] = {
280         &tf_info_entry.attr,
281 #ifdef CONFIG_TF_ZEBRA
282         &tf_started_entry.attr,
283         &tf_workspace_addr_entry.attr,
284         &tf_workspace_size_entry.attr,
285 #endif
286         NULL,
287 };
288 static const struct sysfs_ops tf_sysfs_ops = {
289         .show   = tf_attr_show,
290         .store  = tf_attr_store,
291 };
292 static struct kobj_type tf_ktype = {
293         .release        = tf_kobj_release,
294         .sysfs_ops      = &tf_sysfs_ops,
295         .default_attrs  = tf_default_attrs
296 };
297
298 /*----------------------------------------------------------------------------*/
299
300 #if defined(MODULE) && defined(CONFIG_TF_ZEBRA)
301 static char *smc_mem;
302 module_param(smc_mem, charp, S_IRUGO);
303 #endif
304
305 /*
306  * First routine called when the kernel module is loaded
307  */
308 static int __init tf_device_register(void)
309 {
310         int error;
311         struct tf_device *dev = &g_tf_dev;
312
313         dprintk(KERN_INFO "tf_device_register()\n");
314
315         /*
316          * Initialize the device
317          */
318         dev->dev_number = MKDEV(device_major_number,
319                 TF_DEVICE_MINOR_NUMBER);
320         cdev_init(&dev->cdev, &g_tf_device_file_ops);
321         dev->cdev.owner = THIS_MODULE;
322
323         INIT_LIST_HEAD(&dev->connection_list);
324         spin_lock_init(&dev->connection_list_lock);
325
326 #if defined(MODULE) && defined(CONFIG_TF_ZEBRA)
327         error = (*tf_comm_early_init)();
328         if (error)
329                 goto module_early_init_failed;
330
331         error = tf_device_mshield_init(smc_mem);
332         if (error)
333                 goto mshield_init_failed;
334
335 #ifdef CONFIG_TF_DRIVER_CRYPTO_FIPS
336         error = tf_crypto_hmac_module_init();
337         if (error)
338                 goto hmac_init_failed;
339
340         error = tf_self_test_register_device();
341         if (error)
342                 goto self_test_register_device_failed;
343 #endif
344 #endif
345
346         /* register the sysfs object driver stats */
347         error = kobject_init_and_add(&dev->kobj,  &tf_ktype, NULL, "%s",
348                  TF_DEVICE_BASE_NAME);
349         if (error) {
350                 printk(KERN_ERR "tf_device_register(): "
351                         "kobject_init_and_add failed (error %d)!\n", error);
352                 kobject_put(&dev->kobj);
353                 goto kobject_init_and_add_failed;
354         }
355
356         /*
357          * Register the system device.
358          */
359         register_syscore_ops(&g_tf_device_syscore_ops);
360
361         /*
362          * Register the char device.
363          */
364         printk(KERN_INFO "Registering char device %s (%u:%u)\n",
365                 TF_DEVICE_BASE_NAME,
366                 MAJOR(dev->dev_number),
367                 MINOR(dev->dev_number));
368         error = register_chrdev_region(dev->dev_number, 1,
369                 TF_DEVICE_BASE_NAME);
370         if (error != 0) {
371                 printk(KERN_ERR "tf_device_register():"
372                         " register_chrdev_region failed (error %d)!\n",
373                         error);
374                 goto register_chrdev_region_failed;
375         }
376
377         error = cdev_add(&dev->cdev, dev->dev_number, 1);
378         if (error != 0) {
379                 printk(KERN_ERR "tf_device_register(): "
380                         "cdev_add failed (error %d)!\n",
381                         error);
382                 goto cdev_add_failed;
383         }
384
385         /*
386          * Initialize the communication with the Secure World.
387          */
388 #ifdef CONFIG_TF_TRUSTZONE
389         dev->sm.soft_int_irq = soft_interrupt;
390 #endif
391         error = tf_init(&g_tf_dev.sm);
392         if (error != S_SUCCESS) {
393                 dprintk(KERN_ERR "tf_device_register(): "
394                         "tf_init failed (error %d)!\n",
395                         error);
396                 goto init_failed;
397         }
398
399 #ifdef CONFIG_TF_DRIVER_CRYPTO_FIPS
400         error = tf_self_test_post_init(&(g_tf_dev.kobj));
401         /* N.B. error > 0 indicates a POST failure, which will not
402            prevent the module from loading. */
403         if (error < 0) {
404                 dprintk(KERN_ERR "tf_device_register(): "
405                         "tf_self_test_post_vectors failed (error %d)!\n",
406                         error);
407                 goto post_failed;
408         }
409 #endif
410
411 #ifdef CONFIG_ANDROID
412         tf_class = class_create(THIS_MODULE, TF_DEVICE_BASE_NAME);
413         device_create(tf_class, NULL,
414                 dev->dev_number,
415                 NULL, TF_DEVICE_BASE_NAME);
416 #endif
417
418 #ifdef CONFIG_TF_ZEBRA
419         /*
420          * Initializes the /dev/tf_ctrl device node.
421          */
422         error = tf_ctrl_device_register();
423         if (error)
424                 goto ctrl_failed;
425 #endif
426
427 #ifdef CONFIG_TF_DRIVER_DEBUG_SUPPORT
428         address_cache_property((unsigned long) &tf_device_register);
429 #endif
430         /*
431          * Successful completion.
432          */
433
434         dprintk(KERN_INFO "tf_device_register(): Success\n");
435         return 0;
436
437         /*
438          * Error: undo all operations in the reverse order
439          */
440 #ifdef CONFIG_TF_ZEBRA
441 ctrl_failed:
442 #endif
443 #ifdef CONFIG_TF_DRIVER_CRYPTO_FIPS
444         tf_self_test_post_exit();
445 post_failed:
446 #endif
447 init_failed:
448         cdev_del(&dev->cdev);
449 cdev_add_failed:
450         unregister_chrdev_region(dev->dev_number, 1);
451 register_chrdev_region_failed:
452         unregister_syscore_ops(&g_tf_device_syscore_ops);
453 kobject_init_and_add_failed:
454         kobject_del(&g_tf_dev.kobj);
455
456 #if defined(MODULE) && defined(CONFIG_TF_ZEBRA)
457 #ifdef CONFIG_TF_DRIVER_CRYPTO_FIPS
458         tf_self_test_unregister_device();
459 self_test_register_device_failed:
460         tf_crypto_hmac_module_exit();
461 hmac_init_failed:
462 #endif
463         tf_device_mshield_exit();
464 mshield_init_failed:
465 module_early_init_failed:
466 #endif
467         dprintk(KERN_INFO "tf_device_register(): Failure (error %d)\n",
468                 error);
469         return error;
470 }
471
472 /*----------------------------------------------------------------------------*/
473
474 static int tf_device_open(struct inode *inode, struct file *file)
475 {
476         int error;
477         struct tf_device *dev = &g_tf_dev;
478         struct tf_connection *connection = NULL;
479
480         dprintk(KERN_INFO "tf_device_open(%u:%u, %p)\n",
481                 imajor(inode), iminor(inode), file);
482
483         /* Dummy lseek for non-seekable driver */
484         error = nonseekable_open(inode, file);
485         if (error != 0) {
486                 dprintk(KERN_ERR "tf_device_open(%p): "
487                         "nonseekable_open failed (error %d)!\n",
488                         file, error);
489                 goto error;
490         }
491
492 #ifndef CONFIG_ANDROID
493         /*
494          * Check file flags. We only autthorize the O_RDWR access
495          */
496         if (file->f_flags != O_RDWR) {
497                 dprintk(KERN_ERR "tf_device_open(%p): "
498                         "Invalid access mode %u\n",
499                         file, file->f_flags);
500                 error = -EACCES;
501                 goto error;
502         }
503 #endif
504
505         /*
506          * Open a new connection.
507          */
508
509         error = tf_open(dev, file, &connection);
510         if (error != 0) {
511                 dprintk(KERN_ERR "tf_device_open(%p): "
512                         "tf_open failed (error %d)!\n",
513                         file, error);
514                 goto error;
515         }
516
517         file->private_data = connection;
518
519         /*
520          * Send the CreateDeviceContext command to the secure
521          */
522         error = tf_create_device_context(connection);
523         if (error != 0) {
524                 dprintk(KERN_ERR "tf_device_open(%p): "
525                         "tf_create_device_context failed (error %d)!\n",
526                         file, error);
527                 goto error1;
528         }
529
530         /*
531          * Successful completion.
532          */
533
534         dprintk(KERN_INFO "tf_device_open(%p): Success (connection=%p)\n",
535                 file, connection);
536         return 0;
537
538         /*
539          * Error handling.
540          */
541
542 error1:
543         tf_close(connection);
544 error:
545         dprintk(KERN_INFO "tf_device_open(%p): Failure (error %d)\n",
546                 file, error);
547         return error;
548 }
549
550 /*----------------------------------------------------------------------------*/
551
552 static int tf_device_release(struct inode *inode, struct file *file)
553 {
554         struct tf_connection *connection;
555
556         dprintk(KERN_INFO "tf_device_release(%u:%u, %p)\n",
557                 imajor(inode), iminor(inode), file);
558
559         connection = tf_conn_from_file(file);
560         tf_close(connection);
561
562         dprintk(KERN_INFO "tf_device_release(%p): Success\n", file);
563         return 0;
564 }
565
566 /*----------------------------------------------------------------------------*/
567
568 static long tf_device_ioctl(struct file *file, unsigned int ioctl_num,
569         unsigned long ioctl_param)
570 {
571         int result = S_SUCCESS;
572         struct tf_connection *connection;
573         union tf_command command;
574         struct tf_command_header header;
575         union tf_answer answer;
576         u32 command_size;
577         u32 answer_size;
578         void *user_answer;
579
580         dprintk(KERN_INFO "tf_device_ioctl(%p, %u, %p)\n",
581                 file, ioctl_num, (void *) ioctl_param);
582
583         switch (ioctl_num) {
584         case IOCTL_TF_GET_VERSION:
585                 /* ioctl is asking for the driver interface version */
586                 result = TF_DRIVER_INTERFACE_VERSION;
587                 goto exit;
588
589 #ifdef CONFIG_TF_ION
590         case IOCTL_TF_ION_REGISTER: {
591                 int ion_register;
592                 /* ioctl is asking to register an ion handle */
593                 if (copy_from_user(&ion_register,
594                                 (int *) ioctl_param,
595                                 sizeof(int))) {
596                         dprintk(KERN_ERR "tf_device_ioctl(%p): "
597                                 "copy_from_user failed\n",
598                                 file);
599                         result = -EFAULT;
600                         goto exit;
601                 }
602
603                 connection = tf_conn_from_file(file);
604                 BUG_ON(connection == NULL);
605
606                 /* Initialize ION connection */
607                 if (connection->ion_client == NULL) {
608                         connection->ion_client = ion_client_create(
609                                                 zebra_ion_device,
610                                                 (1 << ION_HEAP_TYPE_CARVEOUT),
611                                                 "tf");
612                 }
613
614                 if (connection->ion_client == NULL) {
615                         dprintk(KERN_ERR "tf_device_ioctl(%p): "
616                                 "unable to create ion client\n",
617                                 file);
618                         result = -EFAULT;
619                         goto exit;
620                 }
621
622                 /*
623                  * TODO: We should use a reference count on this handle in order
624                  * to not unregistered it while using it.
625                  */
626                return (long)ion_import_fd(connection->ion_client, ion_register);
627         }
628
629         case IOCTL_TF_ION_UNREGISTER: {
630                 int ion_register;
631                 /* ioctl is asking to unregister an ion handle */
632
633                 if (copy_from_user(&ion_register,
634                                 (int *) ioctl_param,
635                                 sizeof(int))) {
636                         dprintk(KERN_ERR "tf_device_ioctl(%p): "
637                                 "copy_from_user failed\n",
638                                 file);
639                         result = -EFAULT;
640                         goto exit;
641                 }
642
643                 connection = tf_conn_from_file(file);
644                 BUG_ON(connection == NULL);
645
646                 if (connection->ion_client == NULL) {
647                         dprintk(KERN_ERR "tf_device_ioctl(%p): "
648                                 "ion client does not exist\n",
649                                 file);
650                         result = -EFAULT;
651                         goto exit;
652                 }
653
654                 ion_free(connection->ion_client,
655                         (struct ion_handle *) ion_register);
656
657                 return S_SUCCESS;
658         }
659 #endif
660
661         case IOCTL_TF_EXCHANGE:
662                 /*
663                  * ioctl is asking to perform a message exchange with the Secure
664                  * Module
665                  */
666
667                 /*
668                  * Make a local copy of the data from the user application
669                  * This routine checks the data is readable
670                  *
671                  * Get the header first.
672                  */
673                 if (copy_from_user(&header,
674                                 (struct tf_command_header *)ioctl_param,
675                                 sizeof(struct tf_command_header))) {
676                         dprintk(KERN_ERR "tf_device_ioctl(%p): "
677                                 "Cannot access ioctl parameter %p\n",
678                                 file, (void *) ioctl_param);
679                         result = -EFAULT;
680                         goto exit;
681                 }
682
683                 /* size in words of u32 */
684                 command_size = header.message_size +
685                         sizeof(struct tf_command_header)/sizeof(u32);
686                 if (command_size > sizeof(command)/sizeof(u32)) {
687                         dprintk(KERN_ERR "tf_device_ioctl(%p): "
688                                 "Buffer overflow: too many bytes to copy %d\n",
689                                 file, command_size);
690                         result = -EFAULT;
691                         goto exit;
692                 }
693
694                 if (copy_from_user(&command,
695                                 (union tf_command *)ioctl_param,
696                                 command_size * sizeof(u32))) {
697                         dprintk(KERN_ERR "tf_device_ioctl(%p): "
698                                 "Cannot access ioctl parameter %p\n",
699                                 file, (void *) ioctl_param);
700                         result = -EFAULT;
701                         goto exit;
702                 }
703
704                 connection = tf_conn_from_file(file);
705                 BUG_ON(connection == NULL);
706
707                 /*
708                  * The answer memory space address is in the operation_id field
709                  */
710                 user_answer = (void *) command.header.operation_id;
711
712                 atomic_inc(&(connection->pending_op_count));
713
714                 dprintk(KERN_WARNING "tf_device_ioctl(%p): "
715                         "Sending message type  0x%08x\n",
716                         file, command.header.message_type);
717
718                 switch (command.header.message_type) {
719                 case TF_MESSAGE_TYPE_OPEN_CLIENT_SESSION:
720                         result = tf_open_client_session(connection,
721                                 &command, &answer);
722                         break;
723
724                 case TF_MESSAGE_TYPE_CLOSE_CLIENT_SESSION:
725                         result = tf_close_client_session(connection,
726                                 &command, &answer);
727                         break;
728
729                 case TF_MESSAGE_TYPE_REGISTER_SHARED_MEMORY:
730                         result = tf_register_shared_memory(connection,
731                                 &command, &answer);
732                         break;
733
734                 case TF_MESSAGE_TYPE_RELEASE_SHARED_MEMORY:
735                         result = tf_release_shared_memory(connection,
736                                 &command, &answer);
737                         break;
738
739                 case TF_MESSAGE_TYPE_INVOKE_CLIENT_COMMAND:
740                         trace_invoke_client_command(NVSEC_INVOKE_CMD_START);
741                         result = tf_invoke_client_command(connection,
742                                 &command, &answer);
743                         trace_invoke_client_command(NVSEC_INVOKE_CMD_DONE);
744                         break;
745
746                 case TF_MESSAGE_TYPE_CANCEL_CLIENT_COMMAND:
747                         result = tf_cancel_client_command(connection,
748                                 &command, &answer);
749                         break;
750
751                 default:
752                         dprintk(KERN_ERR "tf_device_ioctl(%p): "
753                                 "Incorrect message type (0x%08x)!\n",
754                                 connection, command.header.message_type);
755                         result = -EOPNOTSUPP;
756                         break;
757                 }
758
759                 atomic_dec(&(connection->pending_op_count));
760
761                 if (result != 0) {
762                         dprintk(KERN_WARNING "tf_device_ioctl(%p): "
763                                 "Operation returning error code 0x%08x)!\n",
764                                 file, result);
765                         goto exit;
766                 }
767
768                 /*
769                  * Copy the answer back to the user space application.
770                  * The driver does not check this field, only copy back to user
771                  * space the data handed over by Secure World
772                  */
773                 answer_size = answer.header.message_size +
774                         sizeof(struct tf_answer_header)/sizeof(u32);
775                 if (copy_to_user(user_answer,
776                                 &answer, answer_size * sizeof(u32))) {
777                         dprintk(KERN_WARNING "tf_device_ioctl(%p): "
778                                 "Failed to copy back the full command "
779                                 "answer to %p\n", file, user_answer);
780                         result = -EFAULT;
781                         goto exit;
782                 }
783
784                 /* successful completion */
785                 dprintk(KERN_INFO "tf_device_ioctl(%p): Success\n", file);
786                 break;
787
788         case  IOCTL_TF_GET_DESCRIPTION: {
789                 /* ioctl asking for the version information buffer */
790                 struct tf_version_information_buffer *pInfoBuffer;
791
792                 dprintk(KERN_INFO "IOCTL_TF_GET_DESCRIPTION:(%p, %u, %p)\n",
793                         file, ioctl_num, (void *) ioctl_param);
794
795                 pInfoBuffer =
796                         ((struct tf_version_information_buffer *) ioctl_param);
797
798                 dprintk(KERN_INFO "IOCTL_TF_GET_DESCRIPTION1: "
799                         "driver_description=\"%64s\"\n", S_VERSION_STRING);
800
801                 if (copy_to_user(pInfoBuffer->driver_description,
802                                 S_VERSION_STRING,
803                                 strlen(S_VERSION_STRING) + 1)) {
804                         dprintk(KERN_ERR "tf_device_ioctl(%p): "
805                                 "Fail to copy back the driver description "
806                                 "to  %p\n",
807                                 file, pInfoBuffer->driver_description);
808                         result = -EFAULT;
809                         goto exit;
810                 }
811
812                 dprintk(KERN_INFO "IOCTL_TF_GET_DESCRIPTION2: "
813                         "secure_world_description=\"%64s\"\n",
814                         tf_get_description(&g_tf_dev.sm));
815
816                 if (copy_to_user(pInfoBuffer->secure_world_description,
817                                 tf_get_description(&g_tf_dev.sm),
818                                 TF_DESCRIPTION_BUFFER_LENGTH)) {
819                         dprintk(KERN_WARNING "tf_device_ioctl(%p): "
820                                 "Failed to copy back the secure world "
821                                 "description to %p\n",
822                                 file, pInfoBuffer->secure_world_description);
823                         result = -EFAULT;
824                         goto exit;
825                 }
826                 break;
827         }
828
829         default:
830                 dprintk(KERN_ERR "tf_device_ioctl(%p): "
831                         "Unknown IOCTL code 0x%08x!\n",
832                         file, ioctl_num);
833                 result = -EOPNOTSUPP;
834                 goto exit;
835         }
836
837 exit:
838         return result;
839 }
840
841 /*----------------------------------------------------------------------------*/
842
843 static void tf_device_shutdown(void)
844 {
845         if (0 > tf_power_management(&g_tf_dev.sm, TF_POWER_OPERATION_SHUTDOWN))
846                 dprintk(KERN_ERR "tf_device_shutdown failing\n");
847 }
848
849 /*----------------------------------------------------------------------------*/
850
851 static int tf_device_suspend(void)
852 {
853         dprintk(KERN_INFO "tf_device_suspend: Enter\n");
854         return tf_power_management(&g_tf_dev.sm,
855                 TF_POWER_OPERATION_HIBERNATE);
856 }
857
858
859 /*----------------------------------------------------------------------------*/
860
861 static void tf_device_resume(void)
862 {
863         if (0 > tf_power_management(&g_tf_dev.sm, TF_POWER_OPERATION_RESUME))
864                 dprintk(KERN_ERR "tf_device_resume failing\n");
865 }
866
867
868 /*----------------------------------------------------------------------------*/
869
870 module_init(tf_device_register);
871
872 MODULE_LICENSE("GPL");
873 MODULE_AUTHOR("Trusted Logic S.A.");