summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/nvec_user.c
blob: 0f43f6f0548d08c574cc0b846ffa0a971cdd6fac (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
/*
 * 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 "nvos_ioctl.h"
#include "nvec.h"
#include "nvec_ioctls.h"
#include "nvreftrack.h"
#include "nvassert.h"
#include "nvec_device.h"

static NvRtHandle s_RtHandle = NULL;
int device_count = 0;

#define dev_to_nvec_driver(d)	container_of(d, struct nvec_driver, driver)
#define to_nvec_device(x) container_of((x), struct nvec_device, dev)

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 nvec_bus_probe(struct device *_dev)
{
	struct nvec_driver *drv = dev_to_nvec_driver(_dev->driver);
	struct nvec_device *dev = to_nvec_device(_dev);

	return drv->probe(dev);
}

static int nvec_bus_remove(struct device *_dev)
{
    return 0;
}

int nvec_bus_match(struct device *_dev, struct device_driver *drv)
{
	struct nvec_device *dev = to_nvec_device(_dev);

	return (strcmp(dev->name, drv->name) == 0);
}

static int nvec_bus_suspend(struct device *_dev, pm_message_t state)
{
	struct nvec_driver *drv = dev_to_nvec_driver(_dev->driver);
	struct nvec_device *dev = to_nvec_device(_dev);
	NvError e = NvError_Success;

	device_count--;
	drv->suspend(dev, state);

	if (!device_count)
	{
		e = NvEcPowerSuspend(NvEcPowerState_Suspend);
		if (e != NvSuccess) {
			return -1;
		}
	}

	return 0;
}

static int nvec_bus_resume(struct device *_dev)
{
	struct nvec_driver *drv = dev_to_nvec_driver(_dev->driver);
	struct nvec_device *dev = to_nvec_device(_dev);

	if (!device_count)
	{
		NvError e = NvEcPowerResume();
		if (e != NvSuccess) {
			return -1;
		}
	}

	device_count++;
	drv->resume(dev);
	return 0;
}

static void nvec_bus_shutdown(struct device *pdev)
{
    NvEcPowerSuspend(NvEcPowerState_PowerDown);
}

static struct bus_type nvec_bus_type = {
	.name		= DEVICE_NAME,
	.match		= nvec_bus_match,
	.probe		= nvec_bus_probe,
	.remove		= nvec_bus_remove,
	.suspend	= nvec_bus_suspend,
	.resume		= nvec_bus_resume,
	.shutdown	= nvec_bus_shutdown,
};

static struct device nvec_bus_dev = {
	.init_name	= DEVICE_NAME
};

int nvec_register_driver(struct nvec_driver *drv)
{
	drv->driver.name = drv->name;
	drv->driver.bus = &nvec_bus_type;
	return driver_register(&drv->driver);
}
EXPORT_SYMBOL_GPL(nvec_register_driver);

void nvec_unregister_driver(struct nvec_driver *drv)
{
	drv->driver.bus = &nvec_bus_type;
	driver_unregister(&drv->driver);
}
EXPORT_SYMBOL_GPL(nvec_unregister_driver);

int nvec_register_device(struct nvec_device *pdev)
{
	if (!pdev)
		return -EINVAL;

	if (!pdev->dev.parent)
		pdev->dev.parent = &nvec_bus_dev;

	pdev->dev.bus = &nvec_bus_type;

	dev_set_name(&pdev->dev, pdev->name);

	device_count++;
	return device_register(&pdev->dev);
}
EXPORT_SYMBOL_GPL(nvec_register_device);

void nvec_unregister_device(struct nvec_device *pdev)
{
	if (pdev) {
		device_count--;
		device_unregister(&pdev->dev);
	}
}
EXPORT_SYMBOL_GPL(nvec_unregister_device);

static int __init nvec_init(void)
{
	int err = 0;
	NvError status = NvSuccess;
	NvU32 NumTypes = 1; // TODO: must have NvRtObjType_NvEc_Num instead;

	err = device_register(&nvec_bus_dev);
	if (err)
		return err;

	err = bus_register(&nvec_bus_type);
	if (err){
		device_unregister(&nvec_bus_dev);
		return err;
	}


	NV_ASSERT(s_RtHandle == NULL);

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

	status = NvEcOpen(&s_NvEcHandle, 0);
	if (status != NvError_Success) {
		printk("nvec NvEcOpen returned 0x%x\n", status);
		NvRtDestroy(s_RtHandle);
		s_RtHandle = NULL;
		bus_unregister(&nvec_bus_type);
		device_unregister(&nvec_bus_dev);
		return -EINVAL;
	}

	err = misc_register(&nvec_dev);
	if (err < 0) {
		if (s_RtHandle) {
			NvEcClose(s_NvEcHandle);
			s_NvEcHandle = NULL;
			NvRtDestroy(s_RtHandle);
			s_RtHandle = NULL;
			bus_unregister(&nvec_bus_type);
			device_unregister(&nvec_bus_dev);
		}
		printk("nvec failed to open\n");
	}

	return err;
}

static void __exit nvec_exit(void)
{
	NvEcClose(s_NvEcHandle);
	s_NvEcHandle = NULL;
	misc_deregister( &nvec_dev );
	NvRtDestroy(s_RtHandle);
	s_RtHandle = NULL;

	bus_unregister(&nvec_bus_type);
	device_unregister(&nvec_bus_dev);
}

module_init(nvec_init);
module_exit(nvec_exit);

MODULE_LICENSE("GPL");