summaryrefslogtreecommitdiff
path: root/src/device-node.c
blob: 3000a76571ee247c3122c330627744c06fde6830 (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
/*
 * device-node
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <string.h>
#include <glib.h>
#include <dlfcn.h>
#include <errno.h>
#include "device-internal.h"

#define container_of(ptr, type, member) ({            \
		 const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
		 (type *)( (char *)__mptr - offsetof(type,member) );})

static GList *dev_head;
static void *dlopen_handle;
const OEM_sys_devman_plugin_interface *oem_intf;

void add_device(const enum device_type *devtype)
{
	dev_head = g_list_append(dev_head, (enum device_type *)devtype);
}

void remove_device(const enum device_type *devtype)
{
	dev_head = g_list_remove(dev_head, (enum device_type *)devtype);
}

static enum device_type *find_device(enum device_type devtype)
{
	GList *elem;
	enum device_type *type;

	for (elem = dev_head; elem; elem = elem->next) {
		type = elem->data;
		if (*type == devtype)
			return type;
	}

	return NULL;
}

API int device_get_property(enum device_type devtype, int property, int *value)
{
	struct device *dev;
	enum device_type *type;
	int r;

	type = find_device(devtype);
	if (type == NULL) {
		_E("devtype cannot find");
		errno = EPERM;
		return -1;
	}

	dev = container_of(type, struct device, type);

	if (dev->get_prop == NULL) {
		_E("devtype doesn't have getter function");
		errno = EPERM;
		return -1;
	}

	r = dev->get_prop(property, value);
	if (r == -ENODEV) {
		_E("Not support driver");
		errno = ENODEV;
		return -1;
	} else if (r == -1) {
		_E("get_prop of %s(%d) return failes", dev->name, property);
		errno = EPERM;
		return -1;
	}

	errno = 0;
	return 0;
}

API int device_set_property(enum device_type devtype, int property, int value)
{
	enum device_type *type;
	struct device *dev;
	int r;

	type = find_device(devtype);
	if (type == NULL) {
		_E("devtype cannot find");
		errno = EPERM;
		return -1;
	}

	dev = container_of(type, struct device, type);

	if (dev->set_prop == NULL) {
		_E("devtype doesn't have setter function");
		errno = EPERM;
		return -1;
	}

	r = dev->set_prop(property, value);
	if (r == -ENODEV) {
		_E("Not support driver");
		errno = ENODEV;
		return -1;
	} else if (r == -1) {
		_E("set_prop of %s(%d) return failes", dev->name, property);
		errno = EPERM;
		return -1;
	}

	errno = 0;
	return 0;
}

static void __CONSTRUCTOR__ module_init(void)
{
	const OEM_sys_devman_plugin_interface *(*OEM_sys_get_devman_plugin_interface) ();
	char *error;

	dlopen_handle = dlopen(DEVMAN_PLUGIN_PATH, RTLD_NOW);
	if (!dlopen_handle) {
		_E("dlopen() failed");
		goto ERROR;
	}

	OEM_sys_get_devman_plugin_interface = dlsym(dlopen_handle, "OEM_sys_get_devman_plugin_interface");
	if ((error = dlerror()) != NULL) {
		_E("dlsym() failed: %s", error);
		goto ERROR;
	}

	oem_intf = OEM_sys_get_devman_plugin_interface();
	if (!oem_intf) {
		_E("get_devman_plugin_interface() failed");
		goto ERROR;
	}

	return;

ERROR:
	if (dlopen_handle)
		dlclose(dlopen_handle);
}

static void __DESTRUCTOR__ module_fini(void)
{
	if (dlopen_handle)
		dlclose(dlopen_handle);
}