summaryrefslogtreecommitdiffstats
path: root/cpukit/libdrvmgr/drvmgr_drvinf.c
blob: 11ba1eabb72ef6581bc43a5e6e4584e3816a4efd (plain) (blame)
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
/* SPDX-License-Identifier: BSD-2-Clause */

/* Driver Manager Driver Interface Implementation.
 *
 * COPYRIGHT (c) 2009 Cobham Gaisler AB.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

/*
 * This is the part the device driver API, the functions rely on that the
 * parent bus driver has implemented the neccessary operations correctly.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <drvmgr/drvmgr.h>
#include "drvmgr_internal.h"

/* Get device pointer from knowing the Driver and the Driver minor
 * that was assigned to it
 */
int drvmgr_get_dev(
	struct drvmgr_drv *drv,
	int minor,
	struct drvmgr_dev **pdev)
{
	struct drvmgr_dev *dev;
	if (!drv)
		return -1;

	DRVMGR_LOCK_READ();
	dev = drv->dev;
	while (dev) {
		if (dev->minor_drv == minor)
			break;
		dev = dev->next_in_drv;
	}
	DRVMGR_UNLOCK();
	if (!dev)
		return -1;
	if (pdev)
		*pdev = dev;
	return 0;
}

/* Get Bus frequency in HZ from bus driver */
int drvmgr_freq_get(
	struct drvmgr_dev *dev,
	int options,
	unsigned int *freq_hz)
{
	if (!dev || !dev->parent || !dev->parent->ops->get_freq)
		return -1;

	return dev->parent->ops->get_freq(dev, options, freq_hz);
}

/* Get driver prefix */
int drvmgr_get_dev_prefix(struct drvmgr_dev *dev, char *dev_prefix)
{
	struct drvmgr_bus_params params;
	if (!dev || !dev->parent || !dev->parent->ops->get_params)
		return -1;

	dev->parent->ops->get_params(dev, &params);
	if (!params.dev_prefix)
		return -1;
	if (dev_prefix)
		strcpy(dev_prefix, params.dev_prefix);
	return 0;
}

/* Register an interrupt */
int drvmgr_interrupt_register(
	struct drvmgr_dev *dev,
	int index,
	const char *info,
	drvmgr_isr isr,
	void *arg)
{
	if (!dev || !dev->parent || !dev->parent->ops->int_register)
		return -1;

	if (!isr)
		return -1;

	return dev->parent->ops->int_register(dev, index, info, isr, arg);
}

/* Unregister an interrupt */
int drvmgr_interrupt_unregister(
	struct drvmgr_dev *dev,
	int index,
	drvmgr_isr isr,
	void *arg)
{
	if (!dev || !dev->parent || !dev->parent->ops->int_unregister)
		return -1;

	if (!isr)
		return -1;

	return dev->parent->ops->int_unregister(dev, index, isr, arg);
}

int drvmgr_interrupt_clear(
	struct drvmgr_dev *dev,
	int index)
{
	if (!dev || !dev->parent || !dev->parent->ops->int_clear)
		return -1;

	return dev->parent->ops->int_clear(dev, index);
}

int drvmgr_interrupt_unmask(
	struct drvmgr_dev *dev,
	int index)
{
	if (!dev || !dev->parent || !dev->parent->ops->int_unmask)
		return -1;

	return dev->parent->ops->int_unmask(dev, index);
}

int drvmgr_interrupt_mask(
	struct drvmgr_dev *dev,
	int index)
{
	if (!dev || !dev->parent || !dev->parent->ops->int_mask)
		return -1;

	return dev->parent->ops->int_mask(dev, index);
}

#ifdef RTEMS_SMP
int drvmgr_interrupt_set_affinity(
	struct drvmgr_dev *dev,
	int index,
	const Processor_mask *cpus)
{
	if (!dev || !dev->parent || !dev->parent->ops->int_set_affinity)
		return -1;

	return dev->parent->ops->int_set_affinity(dev, index, cpus);
}
#endif

int drvmgr_on_rootbus(struct drvmgr_dev *dev)
{
	if (dev->parent && dev->parent->dev && dev->parent->dev->parent)
		return 0;
	else
		return 1;
}