summaryrefslogtreecommitdiff
path: root/drivers/dsp/syslink/multicore_ipc/ipc_drv.c
blob: 4bf7481f76cf575ac957a4595aae9f1ca4105afc (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
/*
 *  ipc_drv.c
 *
 *  IPC driver module.
 *
 *  Copyright (C) 2008-2009 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 <linux/module.h>
#include <linux/device.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/moduleparam.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <ipc_ioctl.h>
#include <drv_notify.h>
#include <nameserver.h>

#define IPC_NAME		"syslink_ipc"
#define IPC_MAJOR		0
#define IPC_MINOR		0
#define IPC_DEVICES		1

#define ipc_release_resource(x, y, z) (ipc_ioc_router(x, y, z, false))

struct ipc_device {
	struct cdev cdev;
};

struct ipc_device *ipc_device;
static struct class *ipc_class;

s32 ipc_major = IPC_MAJOR;
s32 ipc_minor = IPC_MINOR;
char *ipc_name = IPC_NAME;

module_param(ipc_name, charp, 0);
MODULE_PARM_DESC(ipc_name, "Device name, default = syslink_ipc");

module_param(ipc_major, int, 0);	/* Driver's major number */
MODULE_PARM_DESC(ipc_major, "Major device number, default = 0 (auto)");

module_param(ipc_minor, int, 0);	/* Driver's minor number */
MODULE_PARM_DESC(ipc_minor, "Minor device number, default = 0 (auto)");

MODULE_AUTHOR("Texas Instruments");
MODULE_LICENSE("GPL v2");

/*
 * ======== ipc_open ========
 *  This function is invoked when an application
 *  opens handle to the ipc driver
 */
int ipc_open(struct inode *inode, struct file *filp)
{
	s32 retval = 0;
	struct ipc_device *dev;
	struct ipc_process_context *pr_ctxt = NULL;

	pr_ctxt = kzalloc(sizeof(struct ipc_process_context), GFP_KERNEL);
	if (!pr_ctxt)
		retval = -ENOMEM;
	else {
		INIT_LIST_HEAD(&pr_ctxt->resources);
		spin_lock_init(&pr_ctxt->res_lock);
		dev = container_of(inode->i_cdev, struct ipc_device,
					cdev);
		pr_ctxt->dev = dev;
		filp->private_data = pr_ctxt;
	}
	return retval;
}

/*
 * ======== ipc_release ========
 *  This function is invoked when an application
 *  closes handle to the ipc driver
 */
int ipc_release(struct inode *inode, struct file *filp)
{
	s32 retval = 0;
	struct ipc_process_context *pr_ctxt;
	struct resource_info *info = NULL, *temp = NULL;

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

	pr_ctxt = filp->private_data;

	list_for_each_entry_safe(info, temp, &pr_ctxt->resources, res) {
		retval = ipc_release_resource(info->cmd, (ulong)info->data,
					filp);
	}

	list_del(&pr_ctxt->resources);
	kfree(pr_ctxt);

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

/*
 * ======== ipc_ioctl ========
 *  This function  provides IO interface  to the
 *  ipc driver
 */
int ipc_ioctl(struct inode *ip, struct file *filp, u32 cmd, ulong arg)
{
	s32 retval = 0;
	void __user *argp = (void __user *)arg;

	/* Verify the memory and ensure that it is not is kernel
	     address space
	*/
	if (_IOC_DIR(cmd) & _IOC_READ)
		retval = !access_ok(VERIFY_WRITE, argp, _IOC_SIZE(cmd));
	else if (_IOC_DIR(cmd) & _IOC_WRITE)
		retval = !access_ok(VERIFY_READ, argp, _IOC_SIZE(cmd));

	if (retval) {
		retval = -EFAULT;
		goto exit;
	}

	retval = ipc_ioc_router(cmd, (ulong)argp, filp, true);
	return retval;

exit:
	return retval;
}

const struct file_operations ipc_fops = {
	.open = ipc_open,
	.release = ipc_release,
	.ioctl = ipc_ioctl,
	.read = notify_drv_read,
	.mmap = notify_drv_mmap,
};

/*
 * ======== ipc_modules_init ========
 *  IPC Initialization routine. will initialize various
 *  sub components (modules) of IPC.
 */
static int ipc_modules_init(void)
{
	/* Setup the notify_drv module */
	_notify_drv_setup();

	return 0;
}

/*
 * ======== ipc_modules_exit ========
 *  IPC cleanup routine. will cleanup of various
 *  sub components (modules) of IPC.
 */
static void ipc_modules_exit(void)
{
	/* Destroy the notify_drv module */
	_notify_drv_destroy();
}

/*
 * ======== ipc_init ========
 *  Initialization routine. Executed when the driver is
 *  loaded (as a kernel module), or when the system
 *  is booted (when included as part of the kernel
 *  image).
 */
static int __init ipc_init(void)
{
	dev_t dev ;
	s32 retval = 0;

	retval = alloc_chrdev_region(&dev, ipc_minor, IPC_DEVICES,
							ipc_name);
	ipc_major = MAJOR(dev);

	if (retval < 0) {
		printk(KERN_ERR "ipc_init: can't get major %x\n", ipc_major);
		goto exit;
	}

	ipc_device = kmalloc(sizeof(struct ipc_device), GFP_KERNEL);
	if (!ipc_device) {
		printk(KERN_ERR "ipc_init: memory allocation failed for "
			"ipc_device\n");
		retval = -ENOMEM;
		goto unreg_exit;
	}

	memset(ipc_device, 0, sizeof(struct ipc_device));
	retval = ipc_modules_init();
	if (retval) {
		printk(KERN_ERR "ipc_init: ipc initialization failed\n");
		goto unreg_exit;

	}
	ipc_class = class_create(THIS_MODULE, "syslink_ipc");
	if (IS_ERR(ipc_class)) {
		printk(KERN_ERR "ipc_init: error creating ipc class\n");
		retval = PTR_ERR(ipc_class);
		goto unreg_exit;
	}

	device_create(ipc_class, NULL, MKDEV(ipc_major, ipc_minor), NULL,
								ipc_name);
	cdev_init(&ipc_device->cdev, &ipc_fops);
	ipc_device->cdev.owner = THIS_MODULE;
	retval = cdev_add(&ipc_device->cdev, dev, IPC_DEVICES);
	if (retval) {
		printk(KERN_ERR "ipc_init: failed to add the ipc device\n");
		goto class_exit;
	}
	return retval;

class_exit:
	class_destroy(ipc_class);

unreg_exit:
	unregister_chrdev_region(dev, IPC_DEVICES);
	kfree(ipc_device);

exit:
	return retval;
}

/*
 * ======== ipc_exit ========
 *  This function is invoked during unlinking of ipc
 *  module from the kernel. ipc resources are
 *  freed in this function.
 */
static void __exit ipc_exit(void)
{
	dev_t devno;

	ipc_modules_exit();
	devno = MKDEV(ipc_major, ipc_minor);
	if (ipc_device) {
		cdev_del(&ipc_device->cdev);
		kfree(ipc_device);
	}
	unregister_chrdev_region(devno, IPC_DEVICES);
	if (ipc_class) {
		/* remove the device from sysfs */
		device_destroy(ipc_class, MKDEV(ipc_major, ipc_minor));
		class_destroy(ipc_class);
	}
}

/*
 *  ipc driver initialization and de-initialization functions
 */
module_init(ipc_init);
module_exit(ipc_exit);