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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
/* -*- c-basic-offset: 8 -*-
*
* raw1394-iso.c -- Emulation of the raw1394 rawiso API on the juju stack
*
* Copyright (C) 2007 Kristian Hoegsberg <krh@bitplanet.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <string.h>
#include <sys/mman.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include "juju.h"
static int
refill_xmit_buffer(raw1394handle_t handle, struct fw_cdev_queue_iso *queue_iso)
{
int i;
struct fw_cdev_iso_packet *p = handle->iso.packets;
enum raw1394_iso_disposition d;
unsigned int len, dropped;
unsigned char tag, sy, *data, *buffer;
int cycle;
buffer = handle->iso.buffer +
handle->iso.packet_index * handle->iso.max_packet_size;
data = buffer;
for (i = 0; i < handle->iso.irq_interval; i++) {
cycle = -1;
dropped = 0;
d = handle->iso.xmit_handler(handle, data,
&len, &tag, &sy, cycle, dropped);
/* FIXME: handle the different dispositions. */
p->payload_length = len;
p->interrupt = handle->iso.packet_phase == 0;
p->skip = 0;
p->tag = tag;
p->sy = sy;
p->header_length = 0;
data += handle->iso.max_packet_size;
handle->iso.packet_index++;
handle->iso.packet_phase++;
if (handle->iso.packet_index == handle->iso.buf_packets) {
handle->iso.packet_index = 0;
break;
}
if (handle->iso.packet_phase == handle->iso.irq_interval)
handle->iso.packet_phase = 0;
}
queue_iso->packets = ptr_to_u64(handle->iso.packets);
queue_iso->size =
handle->iso.irq_interval * sizeof handle->iso.packets[0];
queue_iso->data = ptr_to_u64(buffer);
return 0;
}
static int
flush_xmit_packets(raw1394handle_t handle, int limit)
{
struct fw_cdev_queue_iso queue_iso;
int len;
handle->iso.packet_index -= handle->iso.irq_interval;
while (handle->iso.packet_index + handle->iso.irq_interval <= limit) {
if (handle->iso.queue_iso.size == 0)
refill_xmit_buffer(handle, &queue_iso);
len = ioctl(handle->iso.fd, FW_CDEV_IOC_QUEUE_ISO, &queue_iso);
if (len < 0)
return -1;
if (handle->iso.queue_iso.size > 0)
break;
}
return 0;
}
int raw1394_iso_xmit_start(raw1394handle_t handle, int start_on_cycle,
int prebuffer_packets)
{
struct fw_cdev_start_iso start_iso;
int retval;
if (prebuffer_packets == -1)
prebuffer_packets = handle->iso.irq_interval;
flush_xmit_packets(handle, prebuffer_packets);
start_iso.cycle = start_on_cycle;
retval = ioctl(handle->iso.fd, FW_CDEV_IOC_START_ISO, &start_iso);
if (retval < 0)
return retval;
return flush_xmit_packets(handle, handle->iso.buf_packets);
}
static int
queue_recv_packets(raw1394handle_t handle)
{
int i;
struct fw_cdev_queue_iso queue_iso;
struct fw_cdev_iso_packet *p = handle->iso.packets;
unsigned int len;
unsigned char *data, *buffer;
buffer = handle->iso.buffer +
handle->iso.packet_index * handle->iso.max_packet_size;
data = buffer;
for (i = 0; i < handle->iso.irq_interval; i++, p++) {
p->payload_length = handle->iso.max_packet_size;
p->interrupt = handle->iso.packet_phase == handle->iso.irq_interval - 1;
p->skip = 0;
p->tag = 0;
p->sy = 0;
p->header_length = 4;
data += handle->iso.max_packet_size;
handle->iso.packet_index++;
handle->iso.packet_phase++;
if (handle->iso.packet_index == handle->iso.buf_packets)
handle->iso.packet_index = 0;
if (handle->iso.packet_phase == handle->iso.irq_interval)
handle->iso.packet_phase = 0;
}
queue_iso.packets = ptr_to_u64(handle->iso.packets);
queue_iso.size =
handle->iso.irq_interval * sizeof handle->iso.packets[0];
queue_iso.data = ptr_to_u64(buffer);
len = ioctl(handle->iso.fd, FW_CDEV_IOC_QUEUE_ISO, &queue_iso);
if (len < 0)
return -1;
return 0;
}
static int
flush_recv_packets(raw1394handle_t handle,
struct fw_cdev_event_iso_interrupt *interrupt)
{
enum raw1394_iso_disposition d;
quadlet_t header, *p, *end;
unsigned int len, cycle, dropped;
unsigned char channel, tag, sy;
unsigned char *data;
p = interrupt->header;
end = (void *) interrupt->header + interrupt->header_length;
cycle = interrupt->cycle;
dropped = 0;
/* FIXME: compute real buffer index. */
data = handle->iso.buffer +
handle->iso.packet_tail * handle->iso.max_packet_size;
while (p < end) {
header = be32_to_cpu(*p++);
len = header >> 8;
channel = header >> 8;
tag = header >> 8;
sy = header >> 8;
printf("len=%d, channel=%d, tag=%d, sy=%d\n",
len, channel, tag, sy);
d = handle->iso.recv_handler(handle, data, len, channel,
tag, sy, cycle, dropped);
data += handle->iso.max_packet_size;
cycle++;
}
queue_recv_packets(handle);
return 0;
}
int raw1394_iso_recv_start(raw1394handle_t handle, int start_on_cycle,
int tag_mask, int sync)
{
struct fw_cdev_start_iso start_iso;
while (handle->iso.packet_index + handle->iso.irq_interval <
handle->iso.buf_packets)
queue_recv_packets(handle);
start_iso.cycle = start_on_cycle;
start_iso.tags =
tag_mask == -1 ? FW_CDEV_ISO_CONTEXT_MATCH_ALL_TAGS : tag_mask;
/* sync is documented as 'not used' */
start_iso.sync = 0;
return ioctl(handle->iso.fd, FW_CDEV_IOC_START_ISO, &start_iso);
}
static int handle_iso_event(raw1394handle_t handle,
struct epoll_closure *closure, __uint32_t events)
{
struct fw_cdev_event_iso_interrupt *interrupt;
int len;
len = read(handle->iso.fd, handle->buffer, sizeof handle->buffer);
if (len < 0)
return -1;
interrupt = (struct fw_cdev_event_iso_interrupt *) handle->buffer;
if (interrupt->type != FW_CDEV_EVENT_ISO_INTERRUPT)
return 0;
switch (handle->iso.type) {
case FW_CDEV_ISO_CONTEXT_TRANSMIT:
return flush_xmit_packets(handle, handle->iso.buf_packets);
case FW_CDEV_ISO_CONTEXT_RECEIVE:
return flush_recv_packets(handle, interrupt);
default:
/* Doesn't happen. */
return -1;
}
}
int raw1394_iso_xmit_write(raw1394handle_t handle, unsigned char *data,
unsigned int len, unsigned char tag,
unsigned char sy)
{
struct fw_cdev_iso_packet packet;
packet.payload_length = len;
packet.interrupt = handle->iso.packet_phase == 0;
packet.skip = 0;
packet.tag = tag;
packet.sy = sy;
packet.header_length = 0;
handle->iso.packet_phase++;
if (handle->iso.packet_phase == handle->iso.irq_interval)
handle->iso.packet_phase = 0;
/* FIXME: circular buffer goo. */
memcpy(handle->iso.head, data, len);
handle->iso.head += len;
return -1;
}
int raw1394_iso_xmit_sync(raw1394handle_t handle)
{
/* FIXME: queue a skip packet and wait for that interrupt. */
return 0;
}
int raw1394_iso_recv_flush(raw1394handle_t handle)
{
/* FIXME: huh, we'll need kernel support here... */
return 0;
}
static int
iso_init(raw1394handle_t handle, int type,
raw1394_iso_xmit_handler_t xmit_handler,
raw1394_iso_recv_handler_t recv_handler,
unsigned int buf_packets,
unsigned int max_packet_size,
unsigned char channel,
enum raw1394_iso_speed speed,
int irq_interval)
{
struct fw_cdev_create_iso_context create;
struct epoll_event ep;
int retval, prot;
if (handle->iso.fd != -1) {
errno = EBUSY;
return -1;
}
handle->iso.type = type;
if (irq_interval < 0)
handle->iso.irq_interval = 256;
else
handle->iso.irq_interval = irq_interval;
handle->iso.xmit_handler = xmit_handler;
handle->iso.recv_handler = recv_handler;
handle->iso.buf_packets = buf_packets;
handle->iso.max_packet_size = max_packet_size;
handle->iso.packet_index = 0;
handle->iso.packet_phase = 0;
handle->iso.packet_tail = 0;
handle->iso.queue_iso.size = 0;
handle->iso.packets =
malloc(handle->iso.irq_interval * sizeof handle->iso.packets[0]);
if (handle->iso.packets == NULL)
return -1;
handle->iso.fd = open(handle->local_filename, O_RDWR);
if (handle->iso.fd < 0) {
free(handle->iso.packets);
return -1;
}
handle->iso.closure.func = handle_iso_event;
ep.events = EPOLLIN;
ep.data.ptr = &handle->iso.closure;
if (epoll_ctl(handle->epoll_fd, EPOLL_CTL_ADD,
handle->iso.fd, &ep) < 0) {
close(handle->iso.fd);
free(handle->iso.packets);
return -1;
}
create.type = type;
create.channel = channel;
create.speed = speed;
create.header_size = 4;
retval = ioctl(handle->iso.fd,
FW_CDEV_IOC_CREATE_ISO_CONTEXT, &create);
if (retval < 0) {
close(handle->iso.fd);
free(handle->iso.packets);
return retval;
}
switch (type) {
case FW_CDEV_ISO_CONTEXT_TRANSMIT:
prot = PROT_READ | PROT_WRITE;
break;
case FW_CDEV_ISO_CONTEXT_RECEIVE:
prot = PROT_READ;
break;
}
handle->iso.buffer =
mmap(NULL, buf_packets * max_packet_size,
prot, MAP_SHARED, handle->iso.fd, 0);
if (handle->iso.buffer == MAP_FAILED) {
close(handle->iso.fd);
free(handle->iso.packets);
return -1;
}
return 0;
}
int raw1394_iso_xmit_init(raw1394handle_t handle,
raw1394_iso_xmit_handler_t handler,
unsigned int buf_packets,
unsigned int max_packet_size,
unsigned char channel,
enum raw1394_iso_speed speed,
int irq_interval)
{
return iso_init(handle, FW_CDEV_ISO_CONTEXT_TRANSMIT,
handler, NULL, buf_packets, max_packet_size,
channel, speed, irq_interval);
}
int raw1394_iso_recv_init(raw1394handle_t handle,
raw1394_iso_recv_handler_t handler,
unsigned int buf_packets,
unsigned int max_packet_size,
unsigned char channel,
enum raw1394_iso_dma_recv_mode mode,
int irq_interval)
{
return iso_init(handle, FW_CDEV_ISO_CONTEXT_RECEIVE,
NULL, handler, buf_packets, max_packet_size,
channel, 0, irq_interval);
}
int raw1394_iso_multichannel_recv_init(raw1394handle_t handle,
raw1394_iso_recv_handler_t handler,
unsigned int buf_packets,
unsigned int max_packet_size,
int irq_interval)
{
/* FIXME: gah */
errno = ENOSYS;
return -1;
}
int raw1394_iso_recv_listen_channel(raw1394handle_t handle,
unsigned char channel)
{
/* FIXME: multichannel */
errno = ENOSYS;
return -1;
}
int raw1394_iso_recv_unlisten_channel(raw1394handle_t handle,
unsigned char channel)
{
/* FIXME: multichannel */
errno = ENOSYS;
return -1;
}
int raw1394_iso_recv_set_channel_mask(raw1394handle_t handle, u_int64_t mask)
{
/* FIXME: multichannel */
errno = ENOSYS;
return -1;
}
void raw1394_iso_stop(raw1394handle_t handle)
{
ioctl(handle->iso.fd, FW_CDEV_IOC_STOP_ISO);
}
void raw1394_iso_shutdown(raw1394handle_t handle)
{
munmap(handle->iso.buffer,
handle->iso.buf_packets * handle->iso.max_packet_size);
close(handle->iso.fd);
free(handle->iso.packets);
}
|