aboutsummaryrefslogtreecommitdiffstats
path: root/src/fw_iso_tx.c
blob: ed91a71adfa297d35ad40fd882ecc0df7d4cec4b (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "fw_iso_ctx_private.h"

/**
 * HinokoFwIsoTx:
 * An object to transmit isochronous packet for single channel.
 *
 * A [class@FwIsoTx] transmits isochronous packets for single channel by IT context in 1394 OHCI.
 * The content of packet is split to two parts; context header and context payload in a manner of
 * Linux FireWire subsystem.
 */
typedef struct {
	struct fw_iso_ctx_state state;
	guint offset;
} HinokoFwIsoTxPrivate;

static void fw_iso_ctx_iface_init(HinokoFwIsoCtxInterface *iface);

G_DEFINE_TYPE_WITH_CODE(HinokoFwIsoTx, hinoko_fw_iso_tx, G_TYPE_OBJECT,
			G_ADD_PRIVATE(HinokoFwIsoTx)
			G_IMPLEMENT_INTERFACE(HINOKO_TYPE_FW_ISO_CTX, fw_iso_ctx_iface_init))

enum fw_iso_tx_sig_type {
	FW_ISO_TX_SIG_TYPE_IRQ = 1,
	FW_ISO_TX_SIG_TYPE_COUNT,
};
static guint fw_iso_tx_sigs[FW_ISO_TX_SIG_TYPE_COUNT] = { 0 };

static void fw_iso_tx_get_property(GObject *obj, guint id, GValue *val, GParamSpec *spec)
{
	HinokoFwIsoTx *self = HINOKO_FW_ISO_TX(obj);
	HinokoFwIsoTxPrivate *priv = hinoko_fw_iso_tx_get_instance_private(self);

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

static void fw_iso_tx_finalize(GObject *obj)
{
	hinoko_fw_iso_ctx_release(HINOKO_FW_ISO_CTX(obj));

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

static void hinoko_fw_iso_tx_class_init(HinokoFwIsoTxClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS(klass);

	gobject_class->get_property = fw_iso_tx_get_property;
	gobject_class->finalize = fw_iso_tx_finalize;

	fw_iso_ctx_class_override_properties(gobject_class);

	/**
	 * HinokoFwIsoTx::interrupted:
	 * @self: A [class@FwIsoTx].
	 * @sec: sec part of isochronous cycle when interrupt occurs.
	 * @cycle: cycle part of of isochronous cycle when interrupt occurs.
	 * @tstamp: (array length=tstamp_length) (element-type guint8): A series of timestamps for
	 *	    packets already handled.
	 * @tstamp_length: the number of bytes for @tstamp.
	 * @count: the number of handled packets.
	 *
	 * Emitted when Linux FireWire subsystem generates interrupt event. There are three cases
	 * for Linux FireWire subsystem to generate the event:
	 *
	 * - When OHCI 1394 controller generates hardware interrupt as a result of processing the
	 *   isochronous packet for the buffer chunk marked to generate hardware interrupt.
	 * - When the number of isochronous packets sent since the last interrupt event reaches
	 *   one quarter of memory page size (usually 4,096 / 4 = 1,024 packets).
	 * - When application calls [method@FwIsoCtx.flush_completions] explicitly.
	 */
	fw_iso_tx_sigs[FW_ISO_TX_SIG_TYPE_IRQ] =
		g_signal_new("interrupted",
			G_OBJECT_CLASS_TYPE(klass),
			G_SIGNAL_RUN_LAST,
			G_STRUCT_OFFSET(HinokoFwIsoTxClass, interrupted),
			NULL, NULL,
			hinoko_sigs_marshal_VOID__UINT_UINT_POINTER_UINT_UINT,
			G_TYPE_NONE,
			5, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_POINTER,
			G_TYPE_UINT, G_TYPE_UINT);
}

static void hinoko_fw_iso_tx_init(HinokoFwIsoTx *self)
{
	HinokoFwIsoTxPrivate *priv = hinoko_fw_iso_tx_get_instance_private(self);

	fw_iso_ctx_state_init(&priv->state);
}

static void fw_iso_tx_stop(HinokoFwIsoCtx *inst)
{
	HinokoFwIsoTx *self;
	HinokoFwIsoTxPrivate *priv;
	gboolean running;

	g_return_if_fail(HINOKO_IS_FW_ISO_TX(inst));
	self = HINOKO_FW_ISO_TX(inst);
	priv = hinoko_fw_iso_tx_get_instance_private(self);

	running = priv->state.running;

	fw_iso_ctx_state_stop(&priv->state);

	if (priv->state.running != running)
		g_signal_emit_by_name(G_OBJECT(inst), "stopped", NULL);
}

void fw_iso_tx_unmap_buffer(HinokoFwIsoCtx *inst)
{
	HinokoFwIsoTx *self;
	HinokoFwIsoTxPrivate *priv;

	g_return_if_fail(HINOKO_IS_FW_ISO_TX(inst));
	self = HINOKO_FW_ISO_TX(inst);
	priv = hinoko_fw_iso_tx_get_instance_private(self);

	fw_iso_ctx_state_unmap_buffer(&priv->state);
}

static void fw_iso_tx_release(HinokoFwIsoCtx *inst)
{
	HinokoFwIsoTx *self;
	HinokoFwIsoTxPrivate *priv;

	g_return_if_fail(HINOKO_IS_FW_ISO_TX(inst));
	self = HINOKO_FW_ISO_TX(inst);
	priv = hinoko_fw_iso_tx_get_instance_private(self);

	fw_iso_ctx_state_release(&priv->state);
}

static gboolean fw_iso_tx_get_cycle_timer(HinokoFwIsoCtx *inst, gint clock_id,
						 HinokoCycleTimer *const *cycle_timer,
						 GError **error)
{
	HinokoFwIsoTx *self;
	HinokoFwIsoTxPrivate *priv;

	g_return_val_if_fail(HINOKO_IS_FW_ISO_TX(inst), FALSE);
	self = HINOKO_FW_ISO_TX(inst);
	priv = hinoko_fw_iso_tx_get_instance_private(self);

	return fw_iso_ctx_state_get_cycle_timer(&priv->state, clock_id, cycle_timer, error);
}

static gboolean fw_iso_tx_flush_completions(HinokoFwIsoCtx *inst, GError **error)
{
	HinokoFwIsoTx *self;
	HinokoFwIsoTxPrivate *priv;

	g_return_val_if_fail(HINOKO_IS_FW_ISO_TX(inst), FALSE);
	self = HINOKO_FW_ISO_TX(inst);
	priv = hinoko_fw_iso_tx_get_instance_private(self);

	return fw_iso_ctx_state_flush_completions(&priv->state, error);
}

gboolean fw_iso_tx_handle_event(HinokoFwIsoCtx *inst, const union fw_cdev_event *event,
				GError **error)
{
	HinokoFwIsoTx *self;
	HinokoFwIsoTxPrivate *priv;

	const struct fw_cdev_event_iso_interrupt *ev;

	g_return_val_if_fail(HINOKO_FW_ISO_TX(inst), FALSE);
	g_return_val_if_fail(event->common.type == FW_CDEV_EVENT_ISO_INTERRUPT, FALSE);

	self = HINOKO_FW_ISO_TX(inst);
	priv = hinoko_fw_iso_tx_get_instance_private(self);

	ev = &event->iso_interrupt;

	guint sec = (ev->cycle & 0x0000e000) >> 13;
	guint cycle = ev->cycle & 0x00001fff;
	unsigned int pkt_count = ev->header_length / 4;

	g_signal_emit(inst, fw_iso_tx_sigs[FW_ISO_TX_SIG_TYPE_IRQ], 0, sec, cycle, ev->header,
		      ev->header_length, pkt_count);

	return fw_iso_ctx_state_queue_chunks(&priv->state, error);
}

gboolean fw_iso_tx_create_source(HinokoFwIsoCtx *inst, GSource **source, GError **error)
{
	HinokoFwIsoTx *self;
	HinokoFwIsoTxPrivate *priv;

	g_return_val_if_fail(HINOKO_IS_FW_ISO_TX(inst), FALSE);
	self = HINOKO_FW_ISO_TX(inst);
	priv = hinoko_fw_iso_tx_get_instance_private(self);

	return fw_iso_ctx_state_create_source(&priv->state, inst, fw_iso_tx_handle_event, source,
					      error);
}

static void fw_iso_ctx_iface_init(HinokoFwIsoCtxInterface *iface)
{
	iface->stop = fw_iso_tx_stop;
	iface->unmap_buffer = fw_iso_tx_unmap_buffer;
	iface->release = fw_iso_tx_release;
	iface->get_cycle_timer = fw_iso_tx_get_cycle_timer;
	iface->flush_completions = fw_iso_tx_flush_completions;
	iface->create_source = fw_iso_tx_create_source;
}

/**
 * hinoko_fw_iso_tx_new:
 *
 * Instantiate [class@FwIsoTx] object and return the instance.
 *
 * Returns: an instance of [class@FwIsoTx].
 */
HinokoFwIsoTx *hinoko_fw_iso_tx_new(void)
{
	return g_object_new(HINOKO_TYPE_FW_ISO_TX, NULL);
}

/**
 * hinoko_fw_iso_tx_allocate:
 * @self: A [class@FwIsoTx].
 * @path: A path to any Linux FireWire character device.
 * @scode: A [enum@FwScode] to indicate speed of isochronous communication.
 * @channel: An isochronous channel to transfer.
 * @header_size: The number of bytes for header of IT context.
 * @error: A [struct@GLib.Error].
 *
 * Allocate an IT context to 1394 OHCI controller. A local node of the node corresponding to the
 * given path is used as the controller, thus any path is accepted as long as process has enough
 * permission for the path.
 */
void hinoko_fw_iso_tx_allocate(HinokoFwIsoTx *self, const char *path,
			       HinokoFwScode scode, guint channel,
			       guint header_size, GError **error)
{
	HinokoFwIsoTxPrivate *priv;

	g_return_if_fail(HINOKO_IS_FW_ISO_TX(self));
	g_return_if_fail(error == NULL || *error == NULL);
	priv = hinoko_fw_iso_tx_get_instance_private(self);

	(void)fw_iso_ctx_state_allocate(&priv->state, path, HINOKO_FW_ISO_CTX_MODE_TX, scode,
					channel, header_size, error);
}

/**
 * hinoko_fw_iso_tx_map_buffer:
 * @self: A [class@FwIsoTx].
 * @maximum_bytes_per_payload: The number of bytes for payload of IT context.
 * @payloads_per_buffer: The number of payloads of IT context in buffer.
 * @error: A [struct@GLib.Error].
 *
 * Map intermediate buffer to share payload of IT context with 1394 OHCI controller.
 */
void hinoko_fw_iso_tx_map_buffer(HinokoFwIsoTx *self,
				 guint maximum_bytes_per_payload,
				 guint payloads_per_buffer,
				 GError **error)
{
	HinokoFwIsoTxPrivate *priv;

	g_return_if_fail(HINOKO_IS_FW_ISO_TX(self));
	g_return_if_fail(error == NULL || *error == NULL);
	priv = hinoko_fw_iso_tx_get_instance_private(self);

	(void)fw_iso_ctx_state_map_buffer(&priv->state, maximum_bytes_per_payload,
					  payloads_per_buffer, error);
}

/**
 * hinoko_fw_iso_tx_start:
 * @self: A [class@FwIsoTx].
 * @cycle_match: (array fixed-size=2) (element-type guint16) (in) (nullable): The isochronous cycle
 *		 to start packet processing. The first element should be the second part of
 *		 isochronous cycle, up to 3. The second element should be the cycle part of
 *		 isochronous cycle, up to 7999.
 * @error: A [struct@GLib.Error].
 *
 * Start IT context.
 *
 * Since: 0.6.
 */
void hinoko_fw_iso_tx_start(HinokoFwIsoTx *self, const guint16 *cycle_match, GError **error)
{
	HinokoFwIsoTxPrivate *priv;

	g_return_if_fail(HINOKO_IS_FW_ISO_TX(self));
	g_return_if_fail(error == NULL || *error == NULL);
	priv = hinoko_fw_iso_tx_get_instance_private(self);

	(void)fw_iso_ctx_state_start(&priv->state, cycle_match, 0, 0, error);
}

/**
 * hinoko_fw_iso_tx_register_packet:
 * @self: A [class@FwIsoTx].
 * @tags: The value of tag field for isochronous packet to register.
 * @sy: The value of sy field for isochronous packet to register.
 * @header: (array length=header_length)(nullable): The header of IT context for isochronous packet.
 * @header_length: The number of bytes for the @header.
 * @payload: (array length=payload_length)(nullable): The payload of IT context for isochronous
 *	     packet.
 * @payload_length: The number of bytes for the @payload.
 * @schedule_interrupt: Whether to schedule hardware interrupt at isochronous cycle for the packet.
 * @error: A [struct@GLib.Error].
 *
 * Register packet data with header and payload for IT context. The caller can schedule hardware
 * interrupt to generate interrupt event. In detail, please refer to documentation about
 * [signal@FwIsoTx::interrupted].
 *
 * Since: 0.6.
 */
void hinoko_fw_iso_tx_register_packet(HinokoFwIsoTx *self,
				HinokoFwIsoCtxMatchFlag tags, guint sy,
				const guint8 *header, guint header_length,
				const guint8 *payload, guint payload_length,
				gboolean schedule_interrupt, GError **error)
{
	HinokoFwIsoTxPrivate *priv;
	const guint8 *frames;
	guint frame_size;
	gboolean skip;

	g_return_if_fail(HINOKO_IS_FW_ISO_TX(self));
	g_return_if_fail((header != NULL && header_length > 0) ||
			 (header == NULL && header_length == 0));
	g_return_if_fail((payload != NULL && payload_length > 0) ||
			 (payload == NULL && payload_length == 0));
	g_return_if_fail(error == NULL || *error == NULL);

	priv = hinoko_fw_iso_tx_get_instance_private(self);

	skip = FALSE;
	if (header_length == 0 && payload_length == 0)
		skip = TRUE;

	if (!fw_iso_ctx_state_register_chunk(&priv->state, skip, tags, sy, header, header_length,
					     payload_length, schedule_interrupt, error))
		return;

	fw_iso_ctx_state_read_frame(&priv->state, priv->offset, payload_length, &frames,
				    &frame_size);
	memcpy((void *)frames, payload, frame_size);
	priv->offset += frame_size;

	if (frame_size != payload_length) {
		guint rest = payload_length - frame_size;

		payload += frame_size;
		fw_iso_ctx_state_read_frame(&priv->state, 0, rest, &frames, &frame_size);
		memcpy((void *)frames, payload, frame_size);

		priv->offset = frame_size;
	}
}