summaryrefslogtreecommitdiff
path: root/drivers/dsp/syslink/procmgr/proc4430/proc4430_drv.c
blob: 9a334b372a3cb7580ef4c286384532f524242da4 (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
/*
 * proc4430_drv.c
 *
 * Syslink driver support functions for TI OMAP processors.
 *
 * Copyright (C) 2009-2010 Texas Instruments, Inc.
 *
 * This package 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 PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include <generated/autoconf.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/uaccess.h>
#include <linux/platform_device.h>


/* Module headers */
#include "proc4430.h"
#include "proc4430_drvdefs.h"



/** ============================================================================
 *  Macros and types
 *  ============================================================================
 */
#define PROC4430_NAME "syslink-proc4430"

static char *driver_name = PROC4430_NAME;

static s32 driver_major;

static s32 driver_minor;

struct proc_4430_dev {
	struct cdev cdev;
};

static struct proc_4430_dev *proc_4430_device;

static struct class *proc_4430_class;



/** ============================================================================
 *  Forward declarations of internal functions
 *  ============================================================================
 */
/* Linux driver function to open the driver object. */
static int proc4430_drv_open(struct inode *inode, struct file *filp);

/* Linux driver function to close the driver object. */
static int proc4430_drv_release(struct inode *inode, struct file *filp);

/* Linux driver function to invoke the APIs through ioctl. */
static long proc4430_drv_ioctl(struct file *filp, unsigned int cmd,
			unsigned long args);

/* Linux driver function to map memory regions to user space. */
static int proc4430_drv_mmap(struct file *filp,
			struct vm_area_struct *vma);

/* Module initialization function for Linux driver. */
static int __init proc4430_drv_initializeModule(void);

/* Module finalization  function for Linux driver. */
static void  __exit proc4430_drv_finalizeModule(void);


/* Process Context */
struct proc4430_process_context {
	u32 setup_count;
};

/** ============================================================================
 *  Globals
 *  ============================================================================
 */

/*
  File operations table for PROC4430.
 */
static const struct file_operations proc_4430_fops = {
	.open = proc4430_drv_open,
	.release = proc4430_drv_release,
	.unlocked_ioctl = proc4430_drv_ioctl,
	.mmap = proc4430_drv_mmap,
};

static int proc4430_drv_open(struct inode *inode, struct file *filp)
{
	s32 retval = 0;
	struct proc4430_process_context *pr_ctxt = NULL;

	pr_ctxt = kzalloc(sizeof(struct proc4430_process_context), GFP_KERNEL);
	if (!pr_ctxt)
		retval = -ENOMEM;

	filp->private_data = pr_ctxt;
	return retval;
}

static int proc4430_drv_release(struct inode *inode, struct file *filp)
{
	s32 retval = 0;
	struct proc4430_process_context *pr_ctxt;

	if (!filp->private_data) {
		retval = -EIO;
		goto err;
	}

	pr_ctxt = filp->private_data;
	while (pr_ctxt->setup_count-- > 0) {
		/* Destroy has not been called.  Call destroy now. */
		proc4430_destroy();
	}
	kfree(pr_ctxt);

	filp->private_data = NULL;
err:
	return retval;
}


/*
  Linux driver function to invoke the APIs through ioctl.
 *
 */
static long proc4430_drv_ioctl(struct file *filp,
		unsigned int cmd, unsigned long args)
{
	int retval = 0;
	struct proc_mgr_cmd_args *cmd_args = (struct proc_mgr_cmd_args *)args;
	struct proc_mgr_cmd_args command_args;
	struct proc4430_process_context *pr_ctxt =
		(struct proc4430_process_context *)filp->private_data;

	switch (cmd) {
	case CMD_PROC4430_GETCONFIG:
	{
		struct proc4430_cmd_args_get_config *src_args =
			(struct proc4430_cmd_args_get_config *)args;
		struct proc4430_config cfg;

		/* copy_from_useris not needed for
		* proc4430_get_config, since the
		* user's config is not used.
		*/
		proc4430_get_config(&cfg);

		retval = copy_to_user((void __user *)(src_args->cfg),
				(const void *)&cfg,
				sizeof(struct proc4430_config));
		if (WARN_ON(retval < 0))
			goto func_exit;
	}
	break;

	case CMD_PROC4430_SETUP:
	{
		struct proc4430_cmd_args_setup *src_args =
			(struct proc4430_cmd_args_setup *)args;
		struct proc4430_config cfg;

		retval = copy_from_user((void *)&cfg,
			(const void __user *)(src_args->cfg),
			sizeof(struct proc4430_config));
		if (WARN_ON(retval < 0))
			goto func_exit;
		proc4430_setup(&cfg);
		pr_ctxt->setup_count++;
	}
	break;

	case CMD_PROC4430_DESTROY:
	{
		proc4430_destroy();
		pr_ctxt->setup_count--;
	}
	break;

	case CMD_PROC4430_PARAMS_INIT:
	{
		struct proc4430_cmd_args_params_init src_args;
		struct proc4430_params params;

		/* Copy the full args from user-side. */
		retval = copy_from_user((void *)&src_args,
			(const void __user *)(args),
			sizeof(struct proc4430_cmd_args_params_init));
		if (WARN_ON(retval < 0))
			goto func_exit;
		proc4430_params_init(src_args.handle, &params);
		retval = copy_to_user((void __user *)(src_args.params),
			(const void *) &params,
			sizeof(struct proc4430_params));
		if (WARN_ON(retval < 0))
			goto func_exit;
	}
	break;

	case CMD_PROC4430_CREATE:
	{
		struct proc4430_cmd_args_create   src_args;
		struct proc4430_params params;
		struct proc4430_mem_entry *entries = NULL;

		/* Copy the full args from user-side. */
		retval = copy_from_user((void *)&src_args,
				(const void __user *)(args),
				sizeof(struct proc4430_cmd_args_create));
		if (WARN_ON(retval < 0))
			goto func_exit;
		retval = copy_from_user((void *) &params,
			(const void __user *)(src_args.params),
			sizeof(struct proc4430_params));
		if (WARN_ON(retval < 0))
			goto func_exit;
		/* Copy the contents of mem_entries from user-side */
		if (params.num_mem_entries) {
			entries = vmalloc(params.num_mem_entries * \
				sizeof(struct proc4430_mem_entry));
			if (WARN_ON(!entries))
				goto func_exit;
			retval = copy_from_user((void *) (entries),
				(const void __user *)(params.mem_entries),
				params.num_mem_entries * \
				sizeof(struct proc4430_mem_entry));
			if (WARN_ON(retval < 0)) {
				vfree(entries);
				goto func_exit;
			}
			params.mem_entries = entries;
		}
		src_args.handle = proc4430_create(src_args.proc_id,
							&params);
		if (WARN_ON(src_args.handle == NULL))
			goto func_exit;
		retval = copy_to_user((void __user *)(args),
				(const void *)&src_args,
				sizeof(struct proc4430_cmd_args_create));
		/* Free the memory created */
		if (params.num_mem_entries)
			vfree(entries);
	}
	break;

	case CMD_PROC4430_DELETE:
	{
		struct proc4430_cmd_args_delete src_args;

		/* Copy the full args from user-side. */
		retval = copy_from_user((void *)&src_args,
			(const void __user *)(args),
			sizeof(struct proc4430_cmd_args_delete));
		if (WARN_ON(retval < 0))
			goto func_exit;
		retval = proc4430_delete(&(src_args.handle));
		WARN_ON(retval < 0);
	}
	break;

	case CMD_PROC4430_OPEN:
	{
		struct proc4430_cmd_args_open src_args;

		/*Copy the full args from user-side. */
		retval = copy_from_user((void *)&src_args,
				(const void __user *)(args),
				sizeof(struct proc4430_cmd_args_open));
		if (WARN_ON(retval < 0))
			goto func_exit;
		retval = proc4430_open(&(src_args.handle),
					src_args.proc_id);
		retval = copy_to_user((void __user *)(args),
				(const void *)&src_args,
			sizeof(struct proc4430_cmd_args_open));
		WARN_ON(retval < 0);
	}
	break;

	case CMD_PROC4430_CLOSE:
	{
		struct proc4430_cmd_args_close src_args;

		/*Copy the full args from user-side. */
		retval = copy_from_user((void *)&src_args,
			(const void __user *)(args),
			sizeof(struct proc4430_cmd_args_close));
		if (WARN_ON(retval < 0))
			goto func_exit;
		retval = proc4430_close(src_args.handle);
		WARN_ON(retval < 0);
	}
	break;

	default:
	{
		pr_err("unsupported ioctl\n");
	}
	break;
	}
func_exit:
	/* Set the status and copy the common args to user-side. */
	command_args.api_status = retval;
	retval = copy_to_user((void __user *) cmd_args,
		(const void *) &command_args,
		 sizeof(struct proc_mgr_cmd_args));
	WARN_ON(retval < 0);
	return retval;
}


/*
  Linux driver function to map memory regions to user space.
 *
 */
static int proc4430_drv_mmap(struct file *filp, struct vm_area_struct *vma)
{
	vma->vm_page_prot = pgprot_dmacoherent(vma->vm_page_prot);

	if (remap_pfn_range(vma,
			 vma->vm_start,
			 vma->vm_pgoff,
			 vma->vm_end - vma->vm_start,
			 vma->vm_page_prot)) {
		return -EAGAIN;
	}
	return 0;
}


/** ==================================================================
 *  Functions required for multiple .ko modules configuration
 *  ==================================================================
 */
/*
  Module initialization  function for Linux driver.
 */
static int __init proc4430_drv_initializeModule(void)
{
	dev_t dev = 0 ;
	int retval;

	/* Display the version info and created date/time */
	pr_info("proc4430_drv_initializeModule\n");

	if (driver_major) {
		dev = MKDEV(driver_major, driver_minor);
		retval = register_chrdev_region(dev, 1, driver_name);
	} else {
		retval = alloc_chrdev_region(&dev, driver_minor, 1,
				 driver_name);
		driver_major = MAJOR(dev);
	}

	proc_4430_device = kmalloc(sizeof(struct proc_4430_dev), GFP_KERNEL);
	if (!proc_4430_device) {
		retval = -ENOMEM;
		unregister_chrdev_region(dev, 1);
		goto exit;
	}
	memset(proc_4430_device, 0, sizeof(struct proc_4430_dev));
	cdev_init(&proc_4430_device->cdev, &proc_4430_fops);
	proc_4430_device->cdev.owner = THIS_MODULE;
	proc_4430_device->cdev.ops = &proc_4430_fops;

	retval = cdev_add(&proc_4430_device->cdev, dev, 1);

	if (retval) {
		pr_err("Failed to add the syslink proc_4430 device\n");
		goto exit;
	}

	/* udev support */
	proc_4430_class = class_create(THIS_MODULE, "syslink-proc4430");

	if (IS_ERR(proc_4430_class)) {
		pr_err("Error creating bridge class\n");
		goto exit;
	}
	device_create(proc_4430_class, NULL, MKDEV(driver_major, driver_minor),
			NULL, PROC4430_NAME);
exit:
	return 0;
}

/*
 function to finalize the driver module.
 */
static void __exit proc4430_drv_finalizeModule(void)
{
	dev_t devno = 0;

	/* FIX ME: THIS MIGHT NOT BE THE RIGHT PLACE TO CALL THE SETUP */
	proc4430_destroy();

	devno = MKDEV(driver_major, driver_minor);
	if (proc_4430_device) {
		cdev_del(&proc_4430_device->cdev);
		kfree(proc_4430_device);
	}
	unregister_chrdev_region(devno, 1);
	if (proc_4430_class) {
		/* remove the device from sysfs */
		device_destroy(proc_4430_class, MKDEV(driver_major,
						driver_minor));
		class_destroy(proc_4430_class);
	}
	return;
}

/*
  Macro calls that indicate initialization and finalization functions
 *		  to the kernel.
 */
MODULE_LICENSE("GPL v2");
module_init(proc4430_drv_initializeModule);
module_exit(proc4430_drv_finalizeModule);