summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/nvaes_user.c
blob: 15f5f1af9bb911d2506a63f112e0fb2b7f4534ac (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
/*
 * arch/arm/mach-tegra/nvaes_user.c
 *
 * User-land access to AES Cryptographic engine.
 *
 * Copyright (c) 2008-2009, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/proc_fs.h>
#include <linux/platform_device.h>

#include <mach/nvrm_linux.h>

#include <nvddk_aes_ioctls.h>
#include <nvos_ioctl.h>
#include <nvos.h>
#include <nvassert.h>
#include <nvreftrack.h>
#include <nvddk_aes.h>

static NvRtHandle s_hRt = NULL;
static struct platform_device *tegra_aes_device = NULL;
NvError NvDdkAes_Dispatch( void *InBuffer, NvU32 InSize, void *OutBuffer, NvU32 OutSize, NvDispatchCtx* Ctx );

static int nvaes_user_open(struct inode *inode, struct file *file)
{
    NvRtClientHandle hClient = (NvRtClientHandle)NULL;
    NvError e = NvRtRegisterClient(s_hRt, &hClient);
    if (NvSuccess != e)
    {
        printk(KERN_INFO "nvaes_user_open: NvRtRegisterClient returned error 0x%x.\n", e);
        return -1;
    }

    file->private_data = (void*)hClient;

    printk(KERN_INFO "nvaes_user_open: hClient = 0x%x.\n", hClient);

    return 0;
}

static int nvaes_user_release(struct inode *inode, struct file *file)
{
    NvRtClientHandle hClient = (NvRtClientHandle)file->private_data;

    if (NvRtUnregisterClient(s_hRt, hClient))
    {
        NvDispatchCtx dctx;

        NvOsMemset(&dctx, 0, sizeof(NvDispatchCtx));
        dctx.Rt = s_hRt;
        dctx.Client = hClient;

        for (;;)
        {
            void* ptr = NvRtFreeObjRef(&dctx, NvRtObjType_NvDdkAes_NvDdkAesHandle, NULL);
            if (!ptr)
                break;
            NvDdkAesClose((NvDdkAesHandle)ptr);
        }

        if (NvRtUnregisterClient(s_hRt, hClient))
            panic("nvaes_user_release: Unable to free resourceTracker client!\n");
    }
    return 0;
}

static long nvaes_user_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    NvError e = NvSuccess;
    NvOsIoctlParams IoctlParams;
    NvU32 size = 0;
    NvU32 SmallBuf[8];
    void *ptr = NULL;
    NvBool IsAlloc = NV_FALSE;
    long status = 0;

    switch (cmd)
    {
        case NvDdkAesKernelIoctls_Generic:
        {
            NvDispatchCtx dctx;

            dctx.Rt = s_hRt;
            dctx.Client = (NvRtClientHandle)file->private_data;
            dctx.PackageIdx = 0;

            e = NvOsCopyIn(&IoctlParams, (void *)arg, sizeof(IoctlParams));
            if (e != NvSuccess)
            {
                printk("NvDdkAesKernelIoctls_Generic: copy in failed\n");
                goto fail;
            }

            size = IoctlParams.InBufferSize + IoctlParams.InOutBufferSize + IoctlParams.OutBufferSize;
            if (size <= sizeof(SmallBuf))
            {
                ptr = SmallBuf;
            }
            else
            {
                ptr = NvOsAlloc(size);
                if (!ptr)
                {
                    printk("NvDdkAesKernelIoctls_Generic: alloc err\n");
                    goto fail;
                }
                IsAlloc = NV_TRUE;
            }

            e = NvOsCopyIn(ptr, IoctlParams.pBuffer, IoctlParams.InBufferSize + IoctlParams.InOutBufferSize);
            if (e != NvSuccess)
            {
                printk("NvDdkAesKernelIoctls_Generic: copy in failure\n");
                goto fail;
            }

            e = NvDdkAes_Dispatch(ptr,
                IoctlParams.InBufferSize + IoctlParams.InOutBufferSize,
                ((NvU8 *)ptr) + IoctlParams.InBufferSize, IoctlParams.InOutBufferSize + IoctlParams.OutBufferSize,
                &dctx);
            if (e != NvSuccess)
            {
                printk("NvDdkAesKernelIoctls_Generic: dispatch failure, err = 0x%x\n", e);
                goto fail;
            }

            if (IoctlParams.InOutBufferSize || IoctlParams.OutBufferSize)
            {
                e = NvOsCopyOut(
                    ((NvU8 *)((NvOsIoctlParams *)arg)->pBuffer) +
                    IoctlParams.InBufferSize,
                    ((NvU8 *)ptr) + IoctlParams.InBufferSize,
                    IoctlParams.InOutBufferSize + IoctlParams.OutBufferSize);
                if (e != NvSuccess)
                {
                    printk("NvDdkAesKernelIoctls_Generic: copyout err\n");
                    goto fail;
                }
            }
            break;
        }
        default:
        printk("unknown ioctl code\n");
        goto fail;
    }
    goto clean;

fail:
    status = -EINVAL;

clean:
    if (IsAlloc)
        NvOsFree(ptr);
    return status;
}

static const struct file_operations nvaes_user_fops =
{
    .owner        = THIS_MODULE,
    .open        = nvaes_user_open,
    .release    = nvaes_user_release,
    .unlocked_ioctl    = nvaes_user_unlocked_ioctl,
};

static struct miscdevice nvaes_user_dev =
{
    .name    = "nvaes",
    .fops    = &nvaes_user_fops,
    .minor    = MISC_DYNAMIC_MINOR,
};

static int __devinit nvaes_user_probe(struct platform_device *pdev)
{
    NvU32 NumTypes = NvRtObjType_NvDdkAes_Num;

    NV_ASSERT(s_hRt == NULL);

    if (NvSuccess != NvRtCreate(1, &NumTypes, &s_hRt))
    {
        printk(KERN_INFO "nvaes_user_init: NvRtCreate returned error.\n");
        goto fail2;
    }

    if (misc_register(&nvaes_user_dev))
    {
        printk(KERN_INFO "nvaes_user_init: misc_register returned error.\n");
        goto fail;
    }

    return 0;

fail:
    NvRtDestroy(s_hRt);
    s_hRt = NULL;

fail2:
    return -1;

}

static int __devexit nvaes_user_remove(struct platform_device *pdev)
{
    misc_deregister(&nvaes_user_dev);
    if (s_hRt)
    {
        NvRtDestroy(s_hRt);
        s_hRt = NULL;
    }
    return 0;
}

#if defined(CONFIG_PM)
static int tegra_aes_suspend(struct platform_device *pdev, pm_message_t state)
{
	NvDdkAesSuspend();
	return 0;
}

static int tegra_aes_resume(struct platform_device *pdev)
{
	NvDdkAesResume();
	return 0;
}
#endif

struct platform_driver tegra_aes_driver = {
	.probe		= nvaes_user_probe,
	.remove		= __devexit_p(nvaes_user_remove),
#if defined(CONFIG_PM)
	.suspend = tegra_aes_suspend,
	.resume = tegra_aes_resume,
#else
	.suspend = NULL,
	.resume = NULL,
#endif
	.driver    = {
		.name	= "nvaes",
		.owner	= THIS_MODULE,
	},
};

static int __init nvaes_user_init(void)
{
	int err;
	err = platform_driver_register(&tegra_aes_driver);
	if (err) {
		pr_err("nvaes: Platform driver registration failed \n");
		goto error;
	}

	tegra_aes_device = platform_device_alloc("nvaes", -1);

	if (!tegra_aes_device) {
		err = -ENOMEM;
		pr_err("nvaes: Platform device allocation failed \n");
		goto error_unregister_driver;
	}
	err = platform_device_add(tegra_aes_device);
	if (err) {
		pr_err("nvaes: Platform device addiotion failed\n");
		goto error_free_device;
	}
	return 0;

error_free_device:
	platform_device_put(tegra_aes_device);

error_unregister_driver:
	platform_driver_unregister(&tegra_aes_driver);

error:
	return err;
}

static void __exit nvaes_user_exit(void)
{
	platform_driver_unregister(&tegra_aes_driver);
}

module_init(nvaes_user_init);
module_exit(nvaes_user_exit);
MODULE_LICENSE("GPL");