2007-03-26 16:49:12 -04:00
|
|
|
/* -*- c-basic-offset: 8 -*-
|
|
|
|
*
|
2008-12-09 00:08:35 -05:00
|
|
|
* fw-iso.c -- Emulation of the raw1394 rawiso API on the firewire stack
|
2007-03-26 16:49:12 -04:00
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Kristian Hoegsberg <krh@bitplanet.net>
|
|
|
|
*
|
2009-02-03 12:56:26 -05:00
|
|
|
* This library is licensed under the GNU Lesser General Public License (LGPL),
|
|
|
|
* version 2.1 or later. See the file COPYING.LIB in the distribution for
|
|
|
|
* details.
|
2007-03-26 16:49:12 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2007-03-28 22:55:21 -04:00
|
|
|
#include <stdio.h>
|
2007-03-26 16:49:12 -04:00
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2008-04-30 17:37:28 -04:00
|
|
|
#include "fw.h"
|
|
|
|
#include "raw1394_private.h"
|
2007-03-26 16:49:12 -04:00
|
|
|
|
2007-04-11 18:22:36 -04:00
|
|
|
static int
|
2008-04-23 20:44:36 -04:00
|
|
|
queue_packet(fw_handle_t handle,
|
2007-04-11 18:22:36 -04:00
|
|
|
unsigned int length, unsigned int header_length,
|
|
|
|
unsigned char tag, unsigned char sy)
|
2007-03-26 16:49:12 -04:00
|
|
|
{
|
2007-04-03 13:31:36 -04:00
|
|
|
struct fw_cdev_queue_iso queue_iso;
|
2007-04-11 18:22:36 -04:00
|
|
|
struct fw_cdev_iso_packet *p;
|
|
|
|
int err;
|
2007-03-26 16:49:12 -04:00
|
|
|
|
2007-04-11 18:22:36 -04:00
|
|
|
p = &handle->iso.packets[handle->iso.packet_index];
|
2007-06-13 18:31:59 -04:00
|
|
|
p->control =
|
|
|
|
FW_CDEV_ISO_PAYLOAD_LENGTH(length) |
|
|
|
|
FW_CDEV_ISO_TAG(tag) |
|
|
|
|
FW_CDEV_ISO_SY(sy) |
|
|
|
|
FW_CDEV_ISO_HEADER_LENGTH(header_length);
|
|
|
|
|
|
|
|
if (handle->iso.packet_phase == handle->iso.irq_interval - 1)
|
|
|
|
p->control |= FW_CDEV_ISO_INTERRUPT;
|
2007-04-03 21:00:06 -04:00
|
|
|
|
2007-04-11 18:22:36 -04:00
|
|
|
handle->iso.head += length;
|
|
|
|
handle->iso.packet_count++;
|
|
|
|
handle->iso.packet_phase++;
|
|
|
|
handle->iso.packet_index++;
|
2007-04-03 21:00:06 -04:00
|
|
|
|
2007-04-11 18:22:36 -04:00
|
|
|
if (handle->iso.packet_phase == handle->iso.irq_interval)
|
|
|
|
handle->iso.packet_phase = 0;
|
2007-03-26 16:49:12 -04:00
|
|
|
|
2007-04-11 18:22:36 -04:00
|
|
|
if (handle->iso.head + handle->iso.max_packet_size > handle->iso.buffer_end)
|
|
|
|
handle->iso.head = handle->iso.buffer;
|
2007-03-26 16:49:12 -04:00
|
|
|
|
2007-04-11 18:22:36 -04:00
|
|
|
/* Queue the packets in the kernel if we filled up the packets
|
|
|
|
* array or wrapped the payload buffer. */
|
|
|
|
if (handle->iso.packet_index == handle->iso.irq_interval ||
|
|
|
|
handle->iso.head == handle->iso.buffer) {
|
|
|
|
queue_iso.packets = ptr_to_u64(handle->iso.packets);
|
|
|
|
queue_iso.size = handle->iso.packet_index * sizeof handle->iso.packets[0];
|
|
|
|
queue_iso.data = ptr_to_u64(handle->iso.first_payload);
|
2007-04-17 20:06:14 -04:00
|
|
|
queue_iso.handle = 0;
|
2007-04-11 18:22:36 -04:00
|
|
|
handle->iso.packet_index = 0;
|
|
|
|
handle->iso.first_payload = handle->iso.head;
|
2007-03-28 22:55:21 -04:00
|
|
|
|
2007-04-11 18:22:36 -04:00
|
|
|
err = ioctl(handle->iso.fd, FW_CDEV_IOC_QUEUE_ISO, &queue_iso);
|
|
|
|
if (err < 0)
|
|
|
|
return -1;
|
2007-03-26 16:49:12 -04:00
|
|
|
}
|
2008-09-30 17:05:32 -04:00
|
|
|
return 0;
|
2007-03-26 16:49:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-04-11 18:22:36 -04:00
|
|
|
queue_xmit_packets(raw1394handle_t handle, int limit)
|
2007-03-26 16:49:12 -04:00
|
|
|
{
|
2008-04-23 20:44:36 -04:00
|
|
|
fw_handle_t fwhandle = handle->mode.fw;
|
2007-04-03 21:23:08 -04:00
|
|
|
enum raw1394_iso_disposition d;
|
2007-04-11 18:22:36 -04:00
|
|
|
unsigned char tag, sy;
|
2008-10-17 19:24:31 -04:00
|
|
|
unsigned int len;
|
|
|
|
int cycle = -1;
|
2008-09-30 17:05:32 -04:00
|
|
|
unsigned int dropped = 0;
|
2007-03-26 16:49:12 -04:00
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
if (fwhandle->iso.xmit_handler == NULL)
|
2007-04-03 13:31:36 -04:00
|
|
|
return 0;
|
2007-03-28 22:55:21 -04:00
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
while (fwhandle->iso.packet_count < limit) {
|
2007-04-11 18:22:36 -04:00
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
d = fwhandle->iso.xmit_handler(handle, fwhandle->iso.head,
|
2007-04-11 18:22:36 -04:00
|
|
|
&len, &tag, &sy, cycle, dropped);
|
2007-04-03 13:31:36 -04:00
|
|
|
|
2007-04-03 21:23:08 -04:00
|
|
|
switch (d) {
|
2007-04-11 18:22:36 -04:00
|
|
|
case RAW1394_ISO_OK:
|
2008-04-23 20:44:36 -04:00
|
|
|
queue_packet(fwhandle, len, 0, tag, sy);
|
2007-04-11 18:22:36 -04:00
|
|
|
break;
|
2007-04-03 21:23:08 -04:00
|
|
|
case RAW1394_ISO_DEFER:
|
|
|
|
case RAW1394_ISO_AGAIN:
|
2007-04-03 22:10:39 -04:00
|
|
|
default:
|
2007-04-03 21:23:08 -04:00
|
|
|
return 0;
|
|
|
|
case RAW1394_ISO_ERROR:
|
|
|
|
return -1;
|
|
|
|
case RAW1394_ISO_STOP:
|
2008-04-23 20:44:36 -04:00
|
|
|
fw_iso_stop(fwhandle);
|
2007-04-03 21:23:08 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2007-03-26 16:49:12 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
int fw_iso_xmit_start(raw1394handle_t handle, int start_on_cycle,
|
2007-03-26 16:49:12 -04:00
|
|
|
int prebuffer_packets)
|
|
|
|
{
|
2008-04-23 20:44:36 -04:00
|
|
|
fw_handle_t fwhandle = handle->mode.fw;
|
2007-03-26 16:49:12 -04:00
|
|
|
struct fw_cdev_start_iso start_iso;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (prebuffer_packets == -1)
|
2008-04-23 20:44:36 -04:00
|
|
|
prebuffer_packets = fwhandle->iso.irq_interval;
|
2007-03-26 16:49:12 -04:00
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
fwhandle->iso.prebuffer = prebuffer_packets;
|
|
|
|
fwhandle->iso.start_on_cycle = start_on_cycle;
|
2007-04-03 13:31:36 -04:00
|
|
|
|
2007-04-11 18:22:36 -04:00
|
|
|
queue_xmit_packets(handle, prebuffer_packets);
|
2007-03-26 16:49:12 -04:00
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
if (fwhandle->iso.prebuffer <= fwhandle->iso.packet_count) {
|
2007-04-17 20:06:14 -04:00
|
|
|
start_iso.cycle = start_on_cycle;
|
|
|
|
start_iso.handle = 0;
|
2007-03-26 16:49:12 -04:00
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
retval = ioctl(fwhandle->iso.fd,
|
2007-04-03 13:31:36 -04:00
|
|
|
FW_CDEV_IOC_START_ISO, &start_iso);
|
|
|
|
if (retval < 0)
|
|
|
|
return retval;
|
|
|
|
}
|
2007-03-26 16:49:12 -04:00
|
|
|
|
2008-12-09 00:08:35 -05:00
|
|
|
retval = queue_xmit_packets(handle, fwhandle->iso.buf_packets);
|
|
|
|
|
|
|
|
if (retval)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
fwhandle->iso.state = ISO_ACTIVE;
|
|
|
|
|
|
|
|
return 0;
|
2007-03-26 16:49:12 -04:00
|
|
|
}
|
2007-03-28 22:55:21 -04:00
|
|
|
|
|
|
|
static int
|
2008-04-23 20:44:36 -04:00
|
|
|
queue_recv_packets(fw_handle_t handle)
|
2007-03-28 22:55:21 -04:00
|
|
|
{
|
2007-04-11 18:22:36 -04:00
|
|
|
while (handle->iso.packet_count <= handle->iso.buf_packets)
|
|
|
|
queue_packet(handle, handle->iso.max_packet_size, 4, 0, 0);
|
2007-03-28 22:55:21 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-03-26 16:49:12 -04:00
|
|
|
|
2007-04-03 21:23:08 -04:00
|
|
|
static enum raw1394_iso_disposition
|
2007-03-28 22:55:21 -04:00
|
|
|
flush_recv_packets(raw1394handle_t handle,
|
|
|
|
struct fw_cdev_event_iso_interrupt *interrupt)
|
2007-03-26 16:49:12 -04:00
|
|
|
{
|
2008-04-23 20:44:36 -04:00
|
|
|
fw_handle_t fwhandle = handle->mode.fw;
|
2007-03-26 16:49:12 -04:00
|
|
|
enum raw1394_iso_disposition d;
|
|
|
|
quadlet_t header, *p, *end;
|
|
|
|
unsigned int len, cycle, dropped;
|
|
|
|
unsigned char channel, tag, sy;
|
|
|
|
|
|
|
|
p = interrupt->header;
|
|
|
|
end = (void *) interrupt->header + interrupt->header_length;
|
|
|
|
cycle = interrupt->cycle;
|
2007-03-28 22:55:21 -04:00
|
|
|
dropped = 0;
|
2007-04-03 22:10:39 -04:00
|
|
|
d = RAW1394_ISO_OK;
|
2007-03-26 16:49:12 -04:00
|
|
|
|
|
|
|
while (p < end) {
|
|
|
|
header = be32_to_cpu(*p++);
|
2007-03-31 19:45:39 -04:00
|
|
|
len = header >> 16;
|
|
|
|
tag = (header >> 14) & 0x3;
|
|
|
|
channel = (header >> 8) & 0x3f;
|
|
|
|
sy = header & 0x0f;
|
2007-03-26 16:49:12 -04:00
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
d = fwhandle->iso.recv_handler(handle, fwhandle->iso.tail, len,
|
2007-04-03 21:00:06 -04:00
|
|
|
channel, tag, sy, cycle, dropped);
|
2007-04-03 21:23:08 -04:00
|
|
|
if (d != RAW1394_ISO_OK)
|
|
|
|
/* FIXME: we need to save the headers so we
|
|
|
|
* can restart this loop. */
|
|
|
|
break;
|
2007-03-26 16:49:12 -04:00
|
|
|
cycle++;
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
fwhandle->iso.tail += fwhandle->iso.max_packet_size;
|
|
|
|
fwhandle->iso.packet_count--;
|
2007-04-12 14:11:20 -04:00
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
if (fwhandle->iso.tail + fwhandle->iso.max_packet_size > fwhandle->iso.buffer_end)
|
|
|
|
fwhandle->iso.tail = fwhandle->iso.buffer;
|
2007-04-02 17:33:07 -04:00
|
|
|
}
|
2007-03-31 19:45:39 -04:00
|
|
|
|
2007-04-03 21:23:08 -04:00
|
|
|
switch (d) {
|
|
|
|
case RAW1394_ISO_OK:
|
|
|
|
case RAW1394_ISO_DEFER:
|
2007-04-03 22:10:39 -04:00
|
|
|
default:
|
2007-04-03 21:23:08 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RAW1394_ISO_ERROR:
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
case RAW1394_ISO_STOP:
|
2008-04-23 20:44:36 -04:00
|
|
|
fw_iso_stop(fwhandle);
|
2007-04-03 21:23:08 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
queue_recv_packets(fwhandle);
|
2007-03-28 22:55:21 -04:00
|
|
|
|
2007-03-26 16:49:12 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
int fw_iso_recv_start(fw_handle_t handle, int start_on_cycle,
|
2007-03-26 16:49:12 -04:00
|
|
|
int tag_mask, int sync)
|
|
|
|
{
|
|
|
|
struct fw_cdev_start_iso start_iso;
|
|
|
|
|
2007-04-11 18:22:36 -04:00
|
|
|
queue_recv_packets(handle);
|
2007-03-28 22:55:21 -04:00
|
|
|
|
2007-03-26 16:49:12 -04:00
|
|
|
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;
|
2007-04-17 20:06:14 -04:00
|
|
|
start_iso.handle = 0;
|
2007-03-26 16:49:12 -04:00
|
|
|
|
2008-12-09 00:08:35 -05:00
|
|
|
if (ioctl(handle->iso.fd, FW_CDEV_IOC_START_ISO, &start_iso))
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
handle->iso.state = ISO_ACTIVE;
|
|
|
|
|
|
|
|
return 0;
|
2007-03-26 16:49:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int handle_iso_event(raw1394handle_t handle,
|
|
|
|
struct epoll_closure *closure, __uint32_t events)
|
|
|
|
{
|
2008-04-23 20:44:36 -04:00
|
|
|
fw_handle_t fwhandle = handle->mode.fw;
|
2007-03-26 16:49:12 -04:00
|
|
|
struct fw_cdev_event_iso_interrupt *interrupt;
|
|
|
|
int len;
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
len = read(fwhandle->iso.fd, fwhandle->buffer, sizeof fwhandle->buffer);
|
2007-03-26 16:49:12 -04:00
|
|
|
if (len < 0)
|
|
|
|
return -1;
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
interrupt = (struct fw_cdev_event_iso_interrupt *) fwhandle->buffer;
|
2007-03-28 22:55:21 -04:00
|
|
|
if (interrupt->type != FW_CDEV_EVENT_ISO_INTERRUPT)
|
2007-03-26 16:49:12 -04:00
|
|
|
return 0;
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
switch (fwhandle->iso.type) {
|
2007-03-26 16:49:12 -04:00
|
|
|
case FW_CDEV_ISO_CONTEXT_TRANSMIT:
|
2008-04-23 20:44:36 -04:00
|
|
|
fwhandle->iso.packet_count -= fwhandle->iso.irq_interval;
|
|
|
|
return queue_xmit_packets(handle, fwhandle->iso.buf_packets);
|
2007-03-26 16:49:12 -04:00
|
|
|
case FW_CDEV_ISO_CONTEXT_RECEIVE:
|
2007-03-28 22:55:21 -04:00
|
|
|
return flush_recv_packets(handle, interrupt);
|
2007-03-26 16:49:12 -04:00
|
|
|
default:
|
|
|
|
/* Doesn't happen. */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
int fw_iso_xmit_write(raw1394handle_t handle, unsigned char *data,
|
2007-03-26 16:49:12 -04:00
|
|
|
unsigned int len, unsigned char tag,
|
|
|
|
unsigned char sy)
|
|
|
|
{
|
2008-04-23 20:44:36 -04:00
|
|
|
fw_handle_t fwhandle = handle->mode.fw;
|
2007-04-03 13:31:36 -04:00
|
|
|
struct fw_cdev_start_iso start_iso;
|
2008-09-30 17:05:32 -04:00
|
|
|
int retval;
|
2007-04-03 21:00:06 -04:00
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
if (len > fwhandle->iso.max_packet_size) {
|
2007-04-03 13:31:36 -04:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2007-03-26 16:49:12 -04:00
|
|
|
|
2007-04-03 21:00:06 -04:00
|
|
|
/* Block until we have space for another packet. */
|
2008-04-23 20:44:36 -04:00
|
|
|
while (fwhandle->iso.packet_count + fwhandle->iso.irq_interval >
|
|
|
|
fwhandle->iso.buf_packets)
|
|
|
|
fw_loop_iterate(handle);
|
2007-04-03 13:31:36 -04:00
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
memcpy(fwhandle->iso.head, data, len);
|
|
|
|
if (queue_packet(fwhandle, len, 0, tag, sy) < 0)
|
2007-04-11 18:22:36 -04:00
|
|
|
return -1;
|
2007-03-26 16:49:12 -04:00
|
|
|
|
2007-04-03 13:31:36 -04:00
|
|
|
/* Start the streaming if it's not already running and if
|
|
|
|
* we've buffered up enough packets. */
|
2008-04-23 20:44:36 -04:00
|
|
|
if (fwhandle->iso.prebuffer > 0 &&
|
|
|
|
fwhandle->iso.packet_count >= fwhandle->iso.prebuffer) {
|
2007-04-03 13:31:36 -04:00
|
|
|
/* Set this to 0 to indicate that we're running. */
|
2008-04-23 20:44:36 -04:00
|
|
|
fwhandle->iso.prebuffer = 0;
|
|
|
|
start_iso.cycle = fwhandle->iso.start_on_cycle;
|
2007-04-17 20:06:14 -04:00
|
|
|
start_iso.handle = 0;
|
2007-04-03 13:31:36 -04:00
|
|
|
|
2008-09-30 17:05:32 -04:00
|
|
|
retval = ioctl(fwhandle->iso.fd,
|
2007-04-03 13:31:36 -04:00
|
|
|
FW_CDEV_IOC_START_ISO, &start_iso);
|
2008-09-30 17:05:32 -04:00
|
|
|
if (retval < 0)
|
|
|
|
return retval;
|
2007-04-03 13:31:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2007-03-26 16:49:12 -04:00
|
|
|
}
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
int fw_iso_xmit_sync(raw1394handle_t handle)
|
2007-03-26 16:49:12 -04:00
|
|
|
{
|
2008-04-23 20:44:36 -04:00
|
|
|
fw_handle_t fwhandle = handle->mode.fw;
|
2007-04-03 22:07:40 -04:00
|
|
|
struct fw_cdev_iso_packet skip;
|
|
|
|
struct fw_cdev_queue_iso queue_iso;
|
|
|
|
int len;
|
|
|
|
|
2007-06-13 18:31:59 -04:00
|
|
|
skip.control = FW_CDEV_ISO_INTERRUPT | FW_CDEV_ISO_SKIP;
|
2007-04-03 22:07:40 -04:00
|
|
|
queue_iso.packets = ptr_to_u64(&skip);
|
|
|
|
queue_iso.size = sizeof skip;
|
|
|
|
queue_iso.data = 0;
|
2007-04-17 20:06:14 -04:00
|
|
|
queue_iso.handle = 0;
|
2007-04-03 22:07:40 -04:00
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
len = ioctl(fwhandle->iso.fd, FW_CDEV_IOC_QUEUE_ISO, &queue_iso);
|
2007-04-03 22:07:40 -04:00
|
|
|
if (len < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Now that we've queued the skip packet, we'll get an
|
|
|
|
* interrupt when the transmit buffer is flushed, so all we do
|
|
|
|
* here is wait. */
|
2008-04-23 20:44:36 -04:00
|
|
|
while (fwhandle->iso.packet_count > 0)
|
|
|
|
fw_loop_iterate(handle);
|
2007-04-03 22:07:40 -04:00
|
|
|
|
|
|
|
/* The iso mainloop thinks that interrutps indicate another
|
|
|
|
* irq_interval number of packets was sent, so the skip
|
|
|
|
* interrupt makes it go out of whack. We just reset it. */
|
2008-04-23 20:44:36 -04:00
|
|
|
fwhandle->iso.head = fwhandle->iso.buffer;
|
|
|
|
fwhandle->iso.tail = fwhandle->iso.buffer;
|
|
|
|
fwhandle->iso.first_payload = fwhandle->iso.buffer;
|
|
|
|
fwhandle->iso.packet_phase = 0;
|
|
|
|
fwhandle->iso.packet_count = 0;
|
2007-03-26 16:49:12 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
int fw_iso_recv_flush(fw_handle_t handle)
|
2007-03-26 16:49:12 -04:00
|
|
|
{
|
|
|
|
/* FIXME: huh, we'll need kernel support here... */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-12 14:11:48 -04:00
|
|
|
static unsigned int
|
|
|
|
round_to_power_of_two(unsigned int value)
|
|
|
|
{
|
|
|
|
unsigned int pot;
|
|
|
|
|
|
|
|
pot = 1;
|
|
|
|
while (pot < value)
|
|
|
|
pot <<= 1;
|
|
|
|
|
|
|
|
return pot;
|
|
|
|
}
|
|
|
|
|
2007-03-28 22:55:21 -04:00
|
|
|
static int
|
2008-04-23 20:44:36 -04:00
|
|
|
iso_init(fw_handle_t handle, int type,
|
2007-03-28 22:55:21 -04:00
|
|
|
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)
|
2007-03-26 16:49:12 -04:00
|
|
|
{
|
|
|
|
struct fw_cdev_create_iso_context create;
|
|
|
|
struct epoll_event ep;
|
2007-03-28 22:55:21 -04:00
|
|
|
int retval, prot;
|
2007-03-26 16:49:12 -04:00
|
|
|
|
|
|
|
if (handle->iso.fd != -1) {
|
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-04-03 22:10:39 -04:00
|
|
|
switch (type) {
|
|
|
|
case FW_CDEV_ISO_CONTEXT_TRANSMIT:
|
|
|
|
prot = PROT_READ | PROT_WRITE;
|
|
|
|
break;
|
|
|
|
case FW_CDEV_ISO_CONTEXT_RECEIVE:
|
|
|
|
prot = PROT_READ;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-03-28 22:55:21 -04:00
|
|
|
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;
|
2007-03-26 16:49:12 -04:00
|
|
|
handle->iso.buf_packets = buf_packets;
|
2007-04-12 14:11:48 -04:00
|
|
|
handle->iso.max_packet_size = round_to_power_of_two(max_packet_size);
|
2007-03-26 16:49:12 -04:00
|
|
|
handle->iso.packet_phase = 0;
|
2007-04-02 17:33:07 -04:00
|
|
|
handle->iso.packet_count = 0;
|
2007-03-26 16:49:12 -04:00
|
|
|
handle->iso.packets =
|
2007-03-28 22:55:21 -04:00
|
|
|
malloc(handle->iso.irq_interval * sizeof handle->iso.packets[0]);
|
2007-03-26 16:49:12 -04:00
|
|
|
if (handle->iso.packets == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2009-01-10 15:03:25 -05:00
|
|
|
handle->iso.fd = open(handle->iso.filename, O_RDWR);
|
2007-03-26 16:49:12 -04:00
|
|
|
if (handle->iso.fd < 0) {
|
|
|
|
free(handle->iso.packets);
|
2007-10-24 16:50:24 -04:00
|
|
|
handle->iso.packets = NULL;
|
2007-03-26 16:49:12 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle->iso.closure.func = handle_iso_event;
|
2008-06-16 11:12:00 -04:00
|
|
|
memset(&ep, 0, sizeof(ep));
|
2007-03-26 16:49:12 -04:00
|
|
|
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);
|
2007-10-24 16:50:24 -04:00
|
|
|
handle->iso.packets = NULL;
|
2007-03-26 16:49:12 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-06-16 11:12:00 -04:00
|
|
|
memset(&create, 0, sizeof(create));
|
2007-03-28 22:55:21 -04:00
|
|
|
create.type = type;
|
2007-03-26 16:49:12 -04:00
|
|
|
create.channel = channel;
|
|
|
|
create.speed = speed;
|
2007-03-28 22:55:21 -04:00
|
|
|
create.header_size = 4;
|
2007-03-26 16:49:12 -04:00
|
|
|
|
|
|
|
retval = ioctl(handle->iso.fd,
|
|
|
|
FW_CDEV_IOC_CREATE_ISO_CONTEXT, &create);
|
|
|
|
if (retval < 0) {
|
|
|
|
close(handle->iso.fd);
|
|
|
|
free(handle->iso.packets);
|
2007-10-24 16:50:24 -04:00
|
|
|
handle->iso.packets = NULL;
|
2007-03-26 16:49:12 -04:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle->iso.buffer =
|
2008-04-29 20:27:00 -04:00
|
|
|
mmap(NULL, buf_packets * handle->iso.max_packet_size,
|
2007-03-28 22:55:21 -04:00
|
|
|
prot, MAP_SHARED, handle->iso.fd, 0);
|
2007-03-26 16:49:12 -04:00
|
|
|
|
|
|
|
if (handle->iso.buffer == MAP_FAILED) {
|
|
|
|
close(handle->iso.fd);
|
|
|
|
free(handle->iso.packets);
|
2007-10-24 16:50:24 -04:00
|
|
|
handle->iso.packets = NULL;
|
2007-03-26 16:49:12 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-04-03 21:00:06 -04:00
|
|
|
handle->iso.buffer_end = handle->iso.buffer +
|
2008-04-29 20:27:00 -04:00
|
|
|
buf_packets * handle->iso.max_packet_size;
|
2007-04-03 21:00:06 -04:00
|
|
|
handle->iso.head = handle->iso.buffer;
|
|
|
|
handle->iso.tail = handle->iso.buffer;
|
|
|
|
handle->iso.first_payload = handle->iso.buffer;
|
2008-12-09 00:08:35 -05:00
|
|
|
handle->iso.state = ISO_STOPPED;
|
2007-04-03 21:00:06 -04:00
|
|
|
|
2007-03-26 16:49:12 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
int fw_iso_xmit_init(fw_handle_t handle,
|
2007-03-28 22:55:21 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
int fw_iso_recv_init(fw_handle_t handle,
|
2007-03-26 16:49:12 -04:00
|
|
|
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)
|
|
|
|
{
|
2007-03-28 22:55:21 -04:00
|
|
|
return iso_init(handle, FW_CDEV_ISO_CONTEXT_RECEIVE,
|
|
|
|
NULL, handler, buf_packets, max_packet_size,
|
|
|
|
channel, 0, irq_interval);
|
2007-03-26 16:49:12 -04:00
|
|
|
}
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
int fw_iso_multichannel_recv_init(fw_handle_t handle,
|
2007-03-26 16:49:12 -04:00
|
|
|
raw1394_iso_recv_handler_t handler,
|
|
|
|
unsigned int buf_packets,
|
|
|
|
unsigned int max_packet_size,
|
|
|
|
int irq_interval)
|
|
|
|
{
|
|
|
|
/* FIXME: gah */
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
int fw_iso_recv_listen_channel(fw_handle_t handle,
|
2007-03-26 16:49:12 -04:00
|
|
|
unsigned char channel)
|
|
|
|
{
|
|
|
|
/* FIXME: multichannel */
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
int fw_iso_recv_unlisten_channel(fw_handle_t handle,
|
2007-03-26 16:49:12 -04:00
|
|
|
unsigned char channel)
|
|
|
|
{
|
|
|
|
/* FIXME: multichannel */
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
int fw_iso_recv_set_channel_mask(fw_handle_t handle, u_int64_t mask)
|
2007-03-26 16:49:12 -04:00
|
|
|
{
|
|
|
|
/* FIXME: multichannel */
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
void fw_iso_stop(fw_handle_t handle)
|
2007-03-26 16:49:12 -04:00
|
|
|
{
|
2007-04-17 20:06:14 -04:00
|
|
|
struct fw_cdev_stop_iso stop_iso;
|
|
|
|
|
|
|
|
stop_iso.handle = 0;
|
2007-03-26 16:49:12 -04:00
|
|
|
ioctl(handle->iso.fd, FW_CDEV_IOC_STOP_ISO);
|
2007-04-17 20:06:14 -04:00
|
|
|
|
2007-04-03 21:00:06 -04:00
|
|
|
handle->iso.head = handle->iso.buffer;
|
|
|
|
handle->iso.tail = handle->iso.buffer;
|
|
|
|
handle->iso.first_payload = handle->iso.buffer;
|
2007-04-02 17:33:07 -04:00
|
|
|
handle->iso.packet_phase = 0;
|
|
|
|
handle->iso.packet_count = 0;
|
2008-12-09 00:08:35 -05:00
|
|
|
handle->iso.packet_index = 0;
|
|
|
|
handle->iso.state = ISO_STOPPED;
|
2007-03-26 16:49:12 -04:00
|
|
|
}
|
|
|
|
|
2008-04-23 20:44:36 -04:00
|
|
|
void fw_iso_shutdown(fw_handle_t handle)
|
2007-03-26 16:49:12 -04:00
|
|
|
{
|
|
|
|
munmap(handle->iso.buffer,
|
|
|
|
handle->iso.buf_packets * handle->iso.max_packet_size);
|
2008-12-09 00:08:35 -05:00
|
|
|
if (handle->iso.state != ISO_STOPPED)
|
|
|
|
fw_iso_stop(handle);
|
2007-03-26 16:49:12 -04:00
|
|
|
close(handle->iso.fd);
|
|
|
|
free(handle->iso.packets);
|
2007-10-24 16:50:24 -04:00
|
|
|
handle->iso.packets = NULL;
|
2008-12-09 00:08:35 -05:00
|
|
|
handle->iso.fd = -1;
|
2007-03-26 16:49:12 -04:00
|
|
|
}
|
2008-06-21 09:38:52 -04:00
|
|
|
|
|
|
|
int fw_read_cycle_timer(fw_handle_t handle,
|
|
|
|
u_int32_t *cycle_timer, u_int64_t *local_time)
|
|
|
|
{
|
|
|
|
#ifdef FW_CDEV_IOC_GET_CYCLE_TIMER /* added in kernel 2.6.24 */
|
|
|
|
int err;
|
|
|
|
struct fw_cdev_get_cycle_timer ctr = { 0 };
|
|
|
|
|
2009-01-10 15:03:25 -05:00
|
|
|
err = ioctl(handle->ioctl_fd, FW_CDEV_IOC_GET_CYCLE_TIMER, &ctr);
|
2008-06-21 09:38:52 -04:00
|
|
|
if (!err) {
|
|
|
|
*cycle_timer = ctr.cycle_timer;
|
|
|
|
*local_time = ctr.local_time;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
#else
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
#endif /* defined(FW_CDEV_IOC_GET_CYCLE_TIMER) */
|
|
|
|
}
|