aboutsummaryrefslogtreecommitdiffstats
path: root/src/fw_iso_resource_once.c
blob: e0eec5b18b19eb0ad9cead493de0dd942c5991b1 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "fw_iso_resource_private.h"

/**
 * HinokoFwIsoResourceOnce:
 * An object to initiate requests and listen events of isochronous resource allocation/deallocation
 * by one shot.
 *
 * The [class@FwIsoResourceOnce] is an object to initiate requests and listen events of isochronous
 * resource allocation/deallocation by file descriptor owned internally. The allocated resource
 * is left even if this object is destroyed, thus application is responsible for deallocation.
 */
typedef struct {
	struct fw_iso_resource_state state;
} HinokoFwIsoResourceOncePrivate;

static void fw_iso_resource_iface_init(HinokoFwIsoResourceInterface *iface);

G_DEFINE_TYPE_WITH_CODE(HinokoFwIsoResourceOnce, hinoko_fw_iso_resource_once, G_TYPE_OBJECT,
			G_ADD_PRIVATE(HinokoFwIsoResourceOnce)
                        G_IMPLEMENT_INTERFACE(HINOKO_TYPE_FW_ISO_RESOURCE, fw_iso_resource_iface_init))

static void fw_iso_resource_once_get_property(GObject *obj, guint id, GValue *val, GParamSpec *spec)
{
	HinokoFwIsoResourceOnce *self = HINOKO_FW_ISO_RESOURCE_ONCE(obj);
	HinokoFwIsoResourceOncePrivate *priv =
			hinoko_fw_iso_resource_once_get_instance_private(self);

	fw_iso_resource_state_get_property(&priv->state, obj, id, val, spec);
}

static void fw_iso_resource_once_finalize(GObject *obj)
{
	HinokoFwIsoResourceOnce *self = HINOKO_FW_ISO_RESOURCE_ONCE(obj);
	HinokoFwIsoResourceOncePrivate *priv =
		hinoko_fw_iso_resource_once_get_instance_private(self);

	fw_iso_resource_state_release(&priv->state);

	G_OBJECT_CLASS(hinoko_fw_iso_resource_once_parent_class)->finalize(obj);
}

static void hinoko_fw_iso_resource_once_class_init(HinokoFwIsoResourceOnceClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS(klass);

	gobject_class->get_property = fw_iso_resource_once_get_property;
	gobject_class->finalize = fw_iso_resource_once_finalize;

	fw_iso_resource_class_override_properties(gobject_class);
}

static void hinoko_fw_iso_resource_once_init(HinokoFwIsoResourceOnce *self)
{
	HinokoFwIsoResourceOncePrivate *priv =
		hinoko_fw_iso_resource_once_get_instance_private(self);

	fw_iso_resource_state_init(&priv->state);
}

static gboolean fw_iso_resource_once_open(HinokoFwIsoResource *inst, const gchar *path,
					  gint open_flag, GError **error)
{
	HinokoFwIsoResourceOnce *self;
	HinokoFwIsoResourceOncePrivate *priv;

	g_return_val_if_fail(HINOKO_IS_FW_ISO_RESOURCE_ONCE(inst), FALSE);
	self = HINOKO_FW_ISO_RESOURCE_ONCE(inst);
	priv = hinoko_fw_iso_resource_once_get_instance_private(self);

	return fw_iso_resource_state_open(&priv->state, path, open_flag, error);
}

static void handle_iso_resource_event(HinokoFwIsoResourceOnce *self,
				      const struct fw_cdev_event_iso_resource *ev)
{
	const char *signal_name;
	guint channel;
	guint bandwidth;
	GError *error;

	parse_iso_resource_event(ev, &channel, &bandwidth, &signal_name, &error);

	g_signal_emit_by_name(self, signal_name, channel, bandwidth, error);

	if (error != NULL)
		g_clear_error(&error);
}

static void handle_bus_reset_event(HinokoFwIsoResourceOnce *self,
				   const struct fw_cdev_event_bus_reset *ev)
{
	HinokoFwIsoResourceOncePrivate *priv =
		hinoko_fw_iso_resource_once_get_instance_private(self);
	gboolean need_notify = (priv->state.bus_state.generation != ev->generation);

	memcpy(&priv->state.bus_state, ev, sizeof(*ev));

	if (need_notify)
		g_object_notify(G_OBJECT(self), GENERATION_PROP_NAME);
}

void fw_iso_resource_once_handle_event(HinokoFwIsoResource *inst, const union fw_cdev_event *event)
{
	HinokoFwIsoResourceOnce *self;

	g_return_if_fail(HINOKO_IS_FW_ISO_RESOURCE_ONCE(inst));
	self = HINOKO_FW_ISO_RESOURCE_ONCE(inst);

	switch (event->common.type) {
	case FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED:
	case FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED:
		handle_iso_resource_event(self, &event->iso_resource);
		break;
	case FW_CDEV_EVENT_BUS_RESET:
		handle_bus_reset_event(self, &event->bus_reset);
		break;
	default:
		break;
	}
}

static gboolean fw_iso_resource_once_create_source(HinokoFwIsoResource *inst, GSource **source,
						   GError **error)
{
	HinokoFwIsoResourceOnce *self;
	HinokoFwIsoResourceOncePrivate *priv;
	guint generation;

	g_return_val_if_fail(HINOKO_IS_FW_ISO_RESOURCE_ONCE(inst), FALSE);
	self = HINOKO_FW_ISO_RESOURCE_ONCE(inst);
	priv = hinoko_fw_iso_resource_once_get_instance_private(self);

	generation = priv->state.bus_state.generation;

	if (!fw_iso_resource_state_create_source(&priv->state, inst,
						 fw_iso_resource_once_handle_event, source, error))
		return FALSE;

	if (generation != priv->state.bus_state.generation)
		g_object_notify(G_OBJECT(self), GENERATION_PROP_NAME);

	return TRUE;
}

static void fw_iso_resource_iface_init(HinokoFwIsoResourceInterface *iface)
{
	iface->open = fw_iso_resource_once_open;
	iface->create_source = fw_iso_resource_once_create_source;

}

/**
 * hinoko_fw_iso_resource_once_new:
 *
 * Allocate and return an instance of [class@FwIsoResourceOnce].
 *
 * Returns: A [class@FwIsoResourceOnce].
 *
 * Sine: 0.7.
 */
HinokoFwIsoResourceOnce *hinoko_fw_iso_resource_once_new()
{
	return g_object_new(HINOKO_TYPE_FW_ISO_RESOURCE_ONCE, NULL);
}

/**
 * hinoko_fw_iso_resource_once_allocate_async:
 * @self: A [class@FwIsoResourceOnce].
 * @channel_candidates: (array length=channel_candidates_count): The array with elements for
 *			numeric number for isochronous channel to be allocated.
 * @channel_candidates_count: The number of channel candidates.
 * @bandwidth: The amount of bandwidth to be allocated.
 * @error: A [struct@GLib.Error]. Error can be generated with domain of Hinoko.FwIsoResourceError.
 *
 * Initiate allocation of isochronous resource without any wait. When the allocation finishes,
 * [signal@FwIsoResource::allocated] signal is emit to notify the result, channel, and bandwidth.
 *
 * Since: 0.7.
 */
void hinoko_fw_iso_resource_once_allocate_async(HinokoFwIsoResourceOnce *self,
						guint8 *channel_candidates,
						gsize channel_candidates_count,
						guint bandwidth, GError **error)
{
	HinokoFwIsoResourceOncePrivate *priv;

	struct fw_cdev_allocate_iso_resource res = {0};
	int i;

	g_return_if_fail(HINOKO_IS_FW_ISO_RESOURCE_ONCE(self));
	g_return_if_fail(error == NULL || *error == NULL);

	g_return_if_fail(channel_candidates != NULL);
	g_return_if_fail(channel_candidates_count > 0);
	g_return_if_fail(bandwidth > 0);

	priv = hinoko_fw_iso_resource_once_get_instance_private(self);
	if (priv->state.fd < 0) {
		generate_coded_error(error, HINOKO_FW_ISO_RESOURCE_ERROR_NOT_OPENED);
		return;
	}

	for (i = 0; i < channel_candidates_count; ++i) {
		if (channel_candidates[i] < 64)
			res.channels |= 1ull << channel_candidates[i];
	}
	res.bandwidth = bandwidth;

	if (ioctl(priv->state.fd, FW_CDEV_IOC_ALLOCATE_ISO_RESOURCE_ONCE, &res) < 0)
		generate_ioctl_error(error, errno, FW_CDEV_IOC_ALLOCATE_ISO_RESOURCE_ONCE);
}

/**
 * hinoko_fw_iso_resource_once_deallocate_async:
 * @self: A [class@FwIsoResourceOnce].
 * @channel: The channel number to be deallocated.
 * @bandwidth: The amount of bandwidth to be deallocated.
 * @error: A [struct@GLib.Error]. Error can be generated with domain of Hinoko.FwIsoResourceError.
 *
 * Initiate deallocation of isochronous resource without any wait. When the
 * deallocation finishes, [signal@FwIsoResource::deallocated] signal is emit to notify the result,
 * channel, and bandwidth.
 *
 * Since: 0.7.
 */
void hinoko_fw_iso_resource_once_deallocate_async(HinokoFwIsoResourceOnce *self, guint channel,
						  guint bandwidth, GError **error)
{
	HinokoFwIsoResourceOncePrivate *priv;

	struct fw_cdev_allocate_iso_resource res = {0};

	g_return_if_fail(HINOKO_IS_FW_ISO_RESOURCE_ONCE(self));
	g_return_if_fail(error == NULL || *error == NULL);

	g_return_if_fail(channel < 64);
	g_return_if_fail(bandwidth > 0);

	priv = hinoko_fw_iso_resource_once_get_instance_private(self);
	if (priv->state.fd < 0) {
		generate_coded_error(error, HINOKO_FW_ISO_RESOURCE_ERROR_NOT_OPENED);
		return;
	}

	res.channels = 1ull << channel;
	res.bandwidth = bandwidth;

	if (ioctl(priv->state.fd, FW_CDEV_IOC_DEALLOCATE_ISO_RESOURCE_ONCE, &res) < 0)
		generate_ioctl_error(error, errno, FW_CDEV_IOC_DEALLOCATE_ISO_RESOURCE_ONCE);
}

/**
 * hinoko_fw_iso_resource_once_allocate_sync:
 * @self: A [class@FwIsoResourceOnce].
 * @channel_candidates: (array length=channel_candidates_count): The array with elements for
 *			numeric number for isochronous channel to be allocated.
 * @channel_candidates_count: The number of channel candidates.
 * @bandwidth: The amount of bandwidth to be allocated.
 * @timeout_ms: The timeout to wait for allocated event.
 * @error: A [struct@GLib.Error]. Error can be generated with domain of Hinoko.FwIsoResourceError.
 *
 * Initiate allocation of isochronous resource and wait for [signal@FwIsoResource::allocated]
 * signal.
 *
 * Since: 0.7.
 */
void hinoko_fw_iso_resource_once_allocate_sync(HinokoFwIsoResourceOnce *self,
					       guint8 *channel_candidates,
					       gsize channel_candidates_count,
					       guint bandwidth, guint timeout_ms,
					       GError **error)
{
	struct fw_iso_resource_waiter w;

	g_return_if_fail(HINOKO_IS_FW_ISO_RESOURCE_ONCE(self));
	g_return_if_fail(error == NULL || *error == NULL);

	fw_iso_resource_waiter_init(&w, HINOKO_FW_ISO_RESOURCE(self), ALLOCATED_SIGNAL_NAME,
				    timeout_ms);

	hinoko_fw_iso_resource_once_allocate_async(self, channel_candidates,
						   channel_candidates_count, bandwidth, error);

	(void)fw_iso_resource_waiter_wait(&w, HINOKO_FW_ISO_RESOURCE(self), error);
}

/**
 * hinoko_fw_iso_resource_once_deallocate_sync:
 * @self: A [class@FwIsoResourceOnce].
 * @channel: The channel number to be deallocated.
 * @bandwidth: The amount of bandwidth to be deallocated.
 * @timeout_ms: The timeout to wait for deallocated event.
 * @error: A [struct@GLib.Error]. Error can be generated with domain of Hinoko.FwIsoResourceError.
 *
 * Initiate deallocation of isochronous resource and wait for [signal@FwIsoResource::deallocated]
 * signal.
 *
 * Since: 0.7.
 */
void hinoko_fw_iso_resource_once_deallocate_sync(HinokoFwIsoResourceOnce *self, guint channel,
						 guint bandwidth, guint timeout_ms,
						 GError **error)
{
	struct fw_iso_resource_waiter w;

	g_return_if_fail(HINOKO_IS_FW_ISO_RESOURCE_ONCE(self));
	g_return_if_fail(error == NULL || *error == NULL);

	fw_iso_resource_waiter_init(&w, HINOKO_FW_ISO_RESOURCE(self), DEALLOCATED_SIGNAL_NAME,
				    timeout_ms);

	hinoko_fw_iso_resource_once_deallocate_async(self, channel, bandwidth, error);

	(void)fw_iso_resource_waiter_wait(&w, HINOKO_FW_ISO_RESOURCE(self), error);
}