summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/nvec_user.c
blob: b11c00e0c0c49dbad74c1bb72d59326046910bec (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
/*
 * arch/arm/mach-tegra/nvec_user.c
 *
 * User-land access to NvEc embedded controller features
 *
 * 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/proc_fs.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include "linux/nvos_ioctl.h"
#include "nvec.h"
#include "linux/nvec_ioctls.h"
#include "nvreftrack.h"
#include "nvassert.h"

static NvRtHandle s_RtHandle = NULL;

NvError NvECPackage_Dispatch(void *InBuffer, NvU32 InSize, void *OutBuffer,
	NvU32 OutSize, NvDispatchCtx* Ctx);

static int nvec_open(struct inode *inode, struct file *file);
static int nvec_close(struct inode *inode, struct file *file);
static long nvec_unlocked_ioctl(struct file *file,
	unsigned int cmd, unsigned long arg);

int nvec_open(struct inode *inode, struct file *file)
{
	NvRtClientHandle Client;

	if (NvRtRegisterClient(s_RtHandle, &Client) != NvSuccess)
		return -ENOMEM;

	file->private_data = (void*)Client;
	return 0;
}

int nvec_close(struct inode *inode, struct file *file)
{
	NvRtClientHandle client = (NvRtClientHandle)file->private_data;

	if (NvRtUnregisterClient(s_RtHandle, client)) {
		NvDispatchCtx dctx;

		dctx.Rt = s_RtHandle;
		dctx.Client = client;
		dctx.PackageIdx = 0;

		// TODO: Enable this code for freeing up leaked handles
		#if 0
		for (;;)
		{
			void* ptr = NvRtFreeObjRef(&dctx,
				NvRtObjType_NvEc_NvEcHandle, NULL);
			if (!ptr) break;
			NVRT_LEAK("NvEc", "NvEcHandle", ptr);
			NvEcClose(ptr);
		}
		#endif

		NvRtUnregisterClient(s_RtHandle, client);
	}
	return 0;
}

long nvec_unlocked_ioctl(struct file *file,
	unsigned int cmd, unsigned long arg)
{
	NvError err;
	NvOsIoctlParams p;
	NvU32 size;
	NvU32 small_buf[8];
	void *ptr = 0;
	long e;
	NvBool bAlloc = NV_FALSE;

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

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

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

		size = p.InBufferSize + p.InOutBufferSize + p.OutBufferSize;
		if (size <= sizeof(small_buf)) {
			ptr = small_buf;
		} else {
			ptr = NvOsAlloc(size);
			if (!ptr) {
				printk("NvECKernelIoctls_Generic: alloc err\n");
				goto fail;
			}

			bAlloc = NV_TRUE;
		}

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

		err = NvECPackage_Dispatch(ptr,
			p.InBufferSize + p.InOutBufferSize,
			((NvU8 *)ptr) + p.InBufferSize, p.InOutBufferSize +
			p.OutBufferSize, &dctx);
		if (err != NvSuccess) {
			printk("NvECKernelIoctls_Generic: dispatch failure\n");
			goto fail;
		}

		if (p.InOutBufferSize || p.OutBufferSize) {
			err = NvOsCopyOut(
				((NvU8 *)((NvOsIoctlParams *)arg)->pBuffer) +
					p.InBufferSize,
				((NvU8 *)ptr) + p.InBufferSize,
				p.InOutBufferSize + p.OutBufferSize);
			if (err != NvSuccess) {
				printk("NvECKernelIoctls_Generic: copyout err\n");
				goto fail;
			}
		}

		break;
	}
	default:
		printk("unknown ioctl code\n");
		goto fail;
	}
	e = 0;
	goto clean;

fail:
	e = -EINVAL;

clean:
	if (bAlloc)
		NvOsFree(ptr);

	return e;
}

#define DEVICE_NAME "nvec"

static const struct file_operations nvec_fops =
{
	.owner		= THIS_MODULE,
	.open		= nvec_open,
	.release	= nvec_close,
	.unlocked_ioctl	= nvec_unlocked_ioctl,
};

static struct miscdevice nvec_dev =
{
	.name	= DEVICE_NAME,
	.fops	= &nvec_fops,
	.minor	= MISC_DYNAMIC_MINOR,
};

static NvEcHandle s_NvEcHandle = NULL;

static int __init nvec_probe(struct platform_device *pdev)
{
	int e = 0;
	NvError status;
	NvU32 NumTypes = 1; // TODO: must have NvRtObjType_NvEc_Num instead;

	NV_ASSERT(s_RtHandle == NULL);

	if (NvRtCreate(1, &NumTypes, &s_RtHandle) != NvSuccess) {
		printk("nvec NvRtCreate returned error\n");
		return -ENOMEM;
	}

	status = NvEcOpen(&s_NvEcHandle, 0);
	if (status != NvError_Success) {
		printk("nvec NvEcOpen returned 0x%x\n", status);
		return -EINVAL;
	}

	e = misc_register(&nvec_dev);
	if (e < 0) {
		if (s_RtHandle) {
			NvRtDestroy(s_RtHandle);
			s_RtHandle = NULL;
		}
		printk("nvec failed to open\n");
	}
	return e;
}

static int nvec_remove(struct platform_device *pdev)
{
    NvEcClose(s_NvEcHandle);
    misc_deregister( &nvec_dev );
    NvRtDestroy(s_RtHandle);
    s_RtHandle = NULL;
    return 0;
}

static int nvec_suspend(struct platform_device *pdev, pm_message_t state)
{
    NvError e = NvEcPowerSuspend(NvEcPowerState_Suspend);
    if (e != NvSuccess)
        return -1;

    return 0;
}

static int nvec_resume(struct platform_device *pdev)
{
    NvError e = NvEcPowerResume();
    if (e != NvSuccess)
        return -1;

    return 0;
}

static void nvec_shutdown(struct platform_device *pdev)
{
    NvEcPowerSuspend(NvEcPowerState_PowerDown);
}

static struct platform_driver tegra_nvec_driver = {
    .probe      = nvec_probe,
    .remove     = nvec_remove,
    .suspend    = nvec_suspend,
    .resume     = nvec_resume,
    .shutdown   = nvec_shutdown,
    .driver     = {
        .name   = "nvec",
    },
};

static int __devinit nvec_init( void )
{
    return platform_driver_register(&tegra_nvec_driver);
}

static void __exit nvec_deinit( void )
{
    return platform_driver_unregister(&tegra_nvec_driver);
}

module_init(nvec_init);
module_exit(nvec_deinit);