summaryrefslogtreecommitdiff
path: root/drivers/dsp/syslink/omap_notify/notify_driver.c
blob: c90c57335918286f0c7459faf510e412a97ac980 (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
/*
 * notify_driver.c
 *
 * Syslink driver support for OMAP Processors.
 *
 * 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/spinlock.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/uaccess.h>

#include <linux/io.h>
#include <asm/pgtable.h>
#include <syslink/gt.h>

#include <syslink/notify.h>
#include <syslink/notify_driver.h>
#include <syslink/atomic_linux.h>

/*
 *func   notify_register_driver
 *
 *desc   This function registers a Notify driver with the Notify module.
 */

int notify_register_driver(char *driver_name,
			struct notify_interface *fn_table,
			struct notify_driver_attrs *drv_attrs,
			struct notify_driver_object **driver_handle)
{
	int status = NOTIFY_SUCCESS;
	struct notify_driver_object *drv_handle = NULL;
	int i;

	BUG_ON(driver_name == NULL);
	BUG_ON(fn_table == NULL);
	BUG_ON(drv_attrs == NULL);
	BUG_ON(driver_handle == NULL);

	if (atomic_cmpmask_and_lt(&(notify_state.ref_count),
			NOTIFY_MAKE_MAGICSTAMP(0),
			NOTIFY_MAKE_MAGICSTAMP(1)) == true) {
		status = NOTIFY_E_INVALIDSTATE;
		goto func_end;
	}

	/*Initialize to status that indicates that an empty slot was not
	  *found for the driver.
	 */
	if (mutex_lock_interruptible(notify_state.gate_handle) != 0)
		WARN_ON(1);
	status = NOTIFY_E_MAXDRIVERS;
	for (i = 0; i < notify_state.cfg.maxDrivers; i++) {
		drv_handle = &(notify_state.drivers[i]);

		if (drv_handle->is_init == NOTIFY_DRIVERINITSTATUS_DONE) {
			if (strncmp(driver_name, drv_handle->name,
				NOTIFY_MAX_NAMELEN) == 0) {
				status = NOTIFY_E_ALREADYEXISTS;
				goto return_existing_handle;
			}
		}
		 if (drv_handle->is_init == NOTIFY_DRIVERINITSTATUS_NOTDONE) {
			/* Found an empty slot, so block it. */
			drv_handle->is_init =
					NOTIFY_DRIVERINITSTATUS_INPROGRESS;
			status = NOTIFY_SUCCESS;
			break;
		}
	}
	mutex_unlock(notify_state.gate_handle);
	WARN_ON(status < 0);
	/*Complete registration of the driver. */
	strncpy(drv_handle->name,
			driver_name, NOTIFY_MAX_NAMELEN);
	memcpy(&(drv_handle->attrs), drv_attrs,
				sizeof(struct notify_driver_attrs));
	memcpy(&(drv_handle->fn_table), fn_table,
				sizeof(struct notify_interface));
	drv_handle->driver_object = NULL;

return_existing_handle:
	/*is_setup is set when driverInit is called. */
	*driver_handle = drv_handle;

func_end:
	return status;
}
EXPORT_SYMBOL(notify_register_driver);

/*========================================
 *func   notify_unregister_driver
 *
 *desc   This function un-registers a Notify driver with the Notify module.
 */
int notify_unregister_driver(struct notify_driver_object  *drv_handle)
{
	int status = NOTIFY_SUCCESS;

	BUG_ON(drv_handle == NULL);


	if (atomic_cmpmask_and_lt(&(notify_state.ref_count),
			NOTIFY_MAKE_MAGICSTAMP(0),
			NOTIFY_MAKE_MAGICSTAMP(1)) == true) {
		status = NOTIFY_E_INVALIDSTATE;
	} else {
		/* Unregister the driver. */
		drv_handle->is_init = NOTIFY_DRIVERINITSTATUS_NOTDONE;

	}
	return status;
}
EXPORT_SYMBOL(notify_unregister_driver);


/*==================================
 * Function to find and return the driver handle maintained within
 * the Notify module.
 *
 * driver_name       Name of the driver to be searched.
 * handle       Return parameter: Handle to the driver.
 *
 */
int notify_get_driver_handle(char *driver_name,
				struct notify_driver_object **handle)
{
	int status = NOTIFY_E_NOTFOUND;
	struct notify_driver_object *drv_handle;
	int i;

	BUG_ON(handle == NULL);

	if (atomic_cmpmask_and_lt(&(notify_state.ref_count),
			NOTIFY_MAKE_MAGICSTAMP(0),
			NOTIFY_MAKE_MAGICSTAMP(1)) == true) {
		status = NOTIFY_E_INVALIDSTATE;
	} else if (WARN_ON(driver_name == NULL))
		status = NOTIFY_E_INVALIDARG;
	else {
		if (mutex_lock_interruptible(notify_state.gate_handle) != 0)
			WARN_ON(1);

		for (i = 0; i < notify_state.cfg.maxDrivers; i++) {
			drv_handle = &(notify_state.drivers[i]);
			/* Check whether the driver handle slot is occupied. */
			if (drv_handle->is_init == true) {
				if (strncmp(driver_name, drv_handle->name,
					NOTIFY_MAX_NAMELEN) == 0) {
					status = NOTIFY_SUCCESS;
					*handle = drv_handle;
					break;
				}
			}
		}
		mutex_unlock(notify_state.gate_handle);
	}
	return status;
}
EXPORT_SYMBOL(notify_get_driver_handle);