summaryrefslogtreecommitdiff
path: root/hw/udev.c
blob: 91d1aa2e2a107c23a7346b07a2d1ccb98dc0646b (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
/*
 * device-manager
 *
 * Copyright (c) 2015 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 <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <libudev.h>
#include <glib.h>
#include <string.h>
#include "common.h"
#include "udev.h"

#define EVENT_KERNEL       "kernel"
#define EVENT_UDEV         "udev"

#define UDEV_MONITOR_SIZE   (128*1024)

struct uevent_info {
	struct udev_monitor *mon;
	GIOChannel *ch;
	guint eventid;
	GList *event_list;
};


/* Uevent */
static struct udev *udev;
static struct uevent_info kevent; /* kernel */
static struct uevent_info uevent; /* udev */

static gboolean uevent_control_cb(GIOChannel *channel,
		GIOCondition cond, void *data)
{
	struct uevent_info *info = data;
	struct udev_device *dev;
	struct uevent_handler *l;
	GList *elem;
	const char *subsystem;
	int len;

	if (!info) {
		_E("data is invalid");
		return TRUE;
	}

	dev = udev_monitor_receive_device(info->mon);
	if (!dev)
		return TRUE;

	subsystem = udev_device_get_subsystem(dev);
	if (!subsystem)
		goto out;

	len = strlen(subsystem);

	for (elem = info->event_list ; elem ; elem = g_list_next(elem)) {
		l = elem->data;
		if (!l)
			continue;
		if (!strncmp(l->subsystem, subsystem, len) &&
		    l->uevent_func)
			l->uevent_func(dev);
	}

out:
	udev_device_unref(dev);
	return TRUE;
}

static int uevent_control_stop(struct uevent_info *info)
{
	struct udev_device *dev;

	if (!info)
		return -EINVAL;

	if (info->eventid) {
		g_source_remove(info->eventid);
		info->eventid = 0;
	}
	if (info->ch) {
		g_io_channel_unref(info->ch);
		info->ch = NULL;
	}
	if (info->mon) {
		dev = udev_monitor_receive_device(info->mon);
		if (dev)
			udev_device_unref(dev);
		udev_monitor_unref(info->mon);
		info->mon = NULL;
	}
	if (udev)
		udev = udev_unref(udev);
	return 0;
}

static int uevent_control_start(const char *type,
		struct uevent_info *info)
{
	struct uevent_handler *l;
	GList *elem;
	int fd;
	int ret;

	if (!info)
		return -EINVAL;

	if (info->mon) {
		_E("%s uevent control routine is alreay started", type);
		return -EINVAL;
	}

	if (!udev) {
		udev = udev_new();
		if (!udev) {
			_E("error create udev");
			return -EINVAL;
		}
	} else
		udev = udev_ref(udev);

	info->mon = udev_monitor_new_from_netlink(udev, type);
	if (info->mon == NULL) {
		_E("error udev_monitor create");
		goto stop;
	}

	_I("Set udev monitor buffer size %d", UDEV_MONITOR_SIZE);
	ret = udev_monitor_set_receive_buffer_size(info->mon,
			UDEV_MONITOR_SIZE);
	if (ret != 0) {
		_E("fail to set receive buffer size");
		goto stop;
	}

	for (elem = info->event_list ; elem ; elem = g_list_next(elem)) {
		l = elem->data;
		ret = udev_monitor_filter_add_match_subsystem_devtype(
				info->mon,
				l->subsystem, NULL);
		if (ret < 0) {
			_E("error apply subsystem filter");
			goto stop;
		}
	}

	ret = udev_monitor_filter_update(info->mon);
	if (ret < 0)
		_E("error udev_monitor_filter_update");

	fd = udev_monitor_get_fd(info->mon);
	if (fd == -1) {
		_E("error udev_monitor_get_fd");
		goto stop;
	}

	info->ch = g_io_channel_unix_new(fd);
	info->eventid = g_io_add_watch(info->ch,
			G_IO_IN, uevent_control_cb, info);
	if (info->eventid == 0) {
		_E("Failed to add channel watch");
		goto stop;
	}

	if (udev_monitor_enable_receiving(info->mon) < 0) {
		_E("error unable to subscribe to udev events");
		goto stop;
	}

	return 0;
stop:
	uevent_control_stop(info);
	return -EINVAL;
}

int uevent_control_kernel_start(void)
{
	return uevent_control_start(EVENT_KERNEL, &kevent);
}

void uevent_control_kernel_stop(void)
{
	uevent_control_stop(&kevent);
}

int uevent_control_udev_start(void)
{
	return uevent_control_start(EVENT_UDEV, &uevent);
}

void uevent_control_udev_stop(void)
{
	uevent_control_stop(&uevent);
}

static int register_uevent_control(struct uevent_info *info,
		struct uevent_handler *uh)
{
	struct uevent_handler *l;
	GList *elem;
	int r;
	bool matched = false;
	int len;

	if (!info || !uh || !uh->subsystem)
		return -EINVAL;

	/* if udev is not initialized, it just will be added list */
	if (!udev || !info->mon)
		goto add_list;

	len = strlen(uh->subsystem);
	/* check if the same subsystem is already added */
	for (elem = info->event_list; elem ; elem = g_list_next(elem)) {
		l = elem->data;
		if (!strncmp(l->subsystem, uh->subsystem, len)) {
			matched = true;
			break;
		}
	}

	/* the first request to add subsystem */
	if (!matched) {
		r = udev_monitor_filter_add_match_subsystem_devtype(info->mon,
				uh->subsystem, NULL);
		if (r < 0) {
			_E("fail to add %s subsystem : %d", uh->subsystem, r);
			return -EPERM;
		}
	}

	r = udev_monitor_filter_update(info->mon);
	if (r < 0)
		_E("fail to update udev monitor filter : %d", r);

add_list:
	info->event_list = g_list_append(info->event_list, uh);
	return 0;
}

static int unregister_uevent_control(struct uevent_info *info,
		const struct uevent_handler *uh)
{
	struct uevent_handler *l;
	GList *n, *next;
	int len;

	if (!info || !uh || !uh->subsystem)
		return -EINVAL;

	len = strlen(uh->subsystem);
	for (n = info->event_list, next = g_list_next(n) ;
			n ; n = next, next = g_list_next(n)) {
		l = n->data;
		if (!strncmp(l->subsystem, uh->subsystem, len) &&
		    l->uevent_func == uh->uevent_func) {
			info->event_list = g_list_delete_link(info->event_list, n);
			return 0;
		}
	}

	return -ENOENT;
}

int register_kernel_event_control(struct uevent_handler *uh)
{
	return register_uevent_control(&kevent, uh);
}

void unregister_kernel_event_control(struct uevent_handler *uh)
{
	unregister_uevent_control(&kevent, uh);
}

int register_udev_event_control(struct uevent_handler *uh)
{
	return register_uevent_control(&uevent, uh);
}

void unregister_udev_event_control(struct uevent_handler *uh)
{
	unregister_uevent_control(&uevent, uh);
}