2000-06-14 11:01:42 -04:00
|
|
|
/*
|
|
|
|
* libraw1394 - library for raw access to the 1394 bus with the Linux subsystem.
|
|
|
|
*
|
2002-10-13 18:54:49 -04:00
|
|
|
* Copyright (C) 1999,2000,2001,2002 Andreas Bombe
|
2002-11-18 02:40:21 -05:00
|
|
|
* new ISO API by Dan Maas
|
2000-06-14 11:01:42 -04: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.
|
|
|
|
*/
|
1999-12-29 17:24:32 -05:00
|
|
|
|
2000-05-28 17:00:56 -04:00
|
|
|
#include <config.h>
|
1999-12-29 17:24:32 -05:00
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2003-01-05 23:08:00 -05:00
|
|
|
#include <string.h>
|
2003-01-15 08:14:47 -05:00
|
|
|
#include <fcntl.h>
|
2003-01-05 15:58:19 -05:00
|
|
|
#include <sys/ioctl.h>
|
2003-01-05 23:08:00 -05:00
|
|
|
#include <sys/mman.h>
|
2003-07-12 20:49:54 -04:00
|
|
|
#include <malloc.h>
|
1999-12-29 17:24:32 -05:00
|
|
|
|
|
|
|
#include "raw1394.h"
|
|
|
|
#include "kernel-raw1394.h"
|
|
|
|
#include "raw1394_private.h"
|
|
|
|
|
2006-05-09 00:27:38 -04:00
|
|
|
/* This implements
|
|
|
|
x = (x+1) % n
|
|
|
|
using a branch based implementation
|
|
|
|
*/
|
|
|
|
static inline int increment_and_wrap(int x, int n)
|
|
|
|
{
|
|
|
|
++x;
|
|
|
|
if (x >= n)
|
|
|
|
x = 0;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2003-01-05 23:08:00 -05:00
|
|
|
/* old ISO API - kept for backwards compatibility */
|
|
|
|
|
|
|
|
static int do_iso_listen(struct raw1394_handle *handle, int channel)
|
|
|
|
{
|
|
|
|
struct sync_cb_data sd = { 0, 0 };
|
|
|
|
struct raw1394_reqhandle rh = { (req_callback_t)_raw1394_sync_cb, &sd };
|
|
|
|
int err;
|
2003-07-22 10:54:19 -04:00
|
|
|
struct raw1394_request req;
|
2003-01-05 23:08:00 -05:00
|
|
|
|
2003-07-22 10:54:19 -04:00
|
|
|
CLEAR_REQ(&req);
|
|
|
|
req.type = RAW1394_REQ_ISO_LISTEN;
|
|
|
|
req.generation = handle->generation;
|
|
|
|
req.misc = channel;
|
|
|
|
req.tag = ptr2int(&rh);
|
|
|
|
req.recvb = ptr2int(handle->buffer);
|
|
|
|
req.length = HBUF_SIZE;
|
2003-01-05 23:08:00 -05:00
|
|
|
|
2003-07-22 10:54:19 -04:00
|
|
|
err = write(handle->fd, &req, sizeof(req));
|
2003-01-05 23:08:00 -05:00
|
|
|
while (!sd.done) {
|
|
|
|
if (err < 0) return err;
|
|
|
|
err = raw1394_loop_iterate(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (sd.errcode) {
|
|
|
|
case RAW1394_ERROR_ALREADY:
|
|
|
|
errno = EALREADY;
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
case RAW1394_ERROR_INVALID_ARG:
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
errno = 0;
|
|
|
|
return sd.errcode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int raw1394_start_iso_rcv(struct raw1394_handle *handle, unsigned int channel)
|
|
|
|
{
|
|
|
|
if (channel > 63) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return do_iso_listen(handle, channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
int raw1394_stop_iso_rcv(struct raw1394_handle *handle, unsigned int channel)
|
|
|
|
{
|
|
|
|
if (channel > 63) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return do_iso_listen(handle, ~channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* new ISO API */
|
|
|
|
|
|
|
|
|
2002-11-18 02:40:21 -05:00
|
|
|
/* reset the dropped counter each time it is seen */
|
|
|
|
static unsigned int _raw1394_iso_dropped(raw1394handle_t handle)
|
|
|
|
{
|
|
|
|
unsigned int retval = handle->iso_packets_dropped;
|
|
|
|
handle->iso_packets_dropped = 0;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2003-01-05 23:08:00 -05:00
|
|
|
|
2002-11-18 02:40:21 -05:00
|
|
|
/* common code for iso_xmit_init and iso_recv_init */
|
|
|
|
static int do_iso_init(raw1394handle_t handle,
|
|
|
|
unsigned int buf_packets,
|
|
|
|
unsigned int max_packet_size,
|
|
|
|
int channel,
|
|
|
|
enum raw1394_iso_speed speed,
|
2003-11-20 10:47:03 -05:00
|
|
|
enum raw1394_iso_dma_recv_mode mode,
|
2002-11-18 02:40:21 -05:00
|
|
|
int irq_interval,
|
2002-12-23 22:01:11 -05:00
|
|
|
int cmd)
|
2002-11-18 02:40:21 -05:00
|
|
|
{
|
2003-07-12 20:49:54 -04:00
|
|
|
unsigned int stride;
|
2006-04-08 22:13:44 -04:00
|
|
|
int result;
|
2002-11-18 02:40:21 -05:00
|
|
|
|
|
|
|
/* already initialized? */
|
2003-01-15 08:14:47 -05:00
|
|
|
if(handle->iso_mode != ISO_INACTIVE)
|
2002-11-18 02:40:21 -05:00
|
|
|
return -1;
|
2002-12-23 22:01:11 -05:00
|
|
|
|
|
|
|
/* choose a power-of-two stride for the packet data buffer,
|
|
|
|
so that an even number of packets fits on one page */
|
|
|
|
for(stride = 4; stride < max_packet_size; stride *= 2);
|
|
|
|
|
|
|
|
if(stride > getpagesize()) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle->iso_buf_stride = stride;
|
|
|
|
|
|
|
|
handle->iso_status.config.data_buf_size = stride * buf_packets;
|
2002-11-18 02:40:21 -05:00
|
|
|
handle->iso_status.config.buf_packets = buf_packets;
|
|
|
|
handle->iso_status.config.channel = channel;
|
|
|
|
handle->iso_status.config.speed = speed;
|
|
|
|
handle->iso_status.config.irq_interval = irq_interval;
|
2003-11-20 10:47:03 -05:00
|
|
|
handle->iso_status.config.dma_mode = mode;
|
2002-11-18 02:40:21 -05:00
|
|
|
|
2002-12-23 22:01:11 -05:00
|
|
|
if(ioctl(handle->fd, cmd, &handle->iso_status))
|
|
|
|
return -1;
|
2002-11-18 02:40:21 -05:00
|
|
|
|
|
|
|
/* mmap the DMA buffer */
|
2002-12-23 22:01:11 -05:00
|
|
|
/* (we assume the kernel sets buf_size to an even number of pages) */
|
|
|
|
handle->iso_buffer = mmap(NULL,
|
|
|
|
handle->iso_status.config.data_buf_size,
|
|
|
|
PROT_READ | PROT_WRITE,
|
2002-11-18 02:40:21 -05:00
|
|
|
MAP_SHARED, handle->fd, 0);
|
2002-12-23 22:01:11 -05:00
|
|
|
|
2002-11-18 02:40:21 -05:00
|
|
|
if(handle->iso_buffer == (unsigned char*) MAP_FAILED) {
|
|
|
|
handle->iso_buffer = NULL;
|
2003-02-21 23:31:28 -05:00
|
|
|
ioctl(handle->fd, RAW1394_IOC_ISO_SHUTDOWN, 0);
|
2002-11-18 02:40:21 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle->iso_status.overflows = 0;
|
|
|
|
handle->iso_packets_dropped = 0;
|
2002-12-23 22:01:11 -05:00
|
|
|
|
2002-11-18 02:40:21 -05:00
|
|
|
handle->iso_xmit_handler = NULL;
|
|
|
|
handle->iso_recv_handler = NULL;
|
2002-12-23 22:01:11 -05:00
|
|
|
|
2003-01-15 08:14:47 -05:00
|
|
|
handle->iso_state = ISO_STOP;
|
|
|
|
|
2006-04-08 21:26:41 -04:00
|
|
|
handle->iso_packet_infos = malloc(buf_packets * sizeof(struct raw1394_iso_packet_info));
|
|
|
|
if(handle->iso_packet_infos == NULL) {
|
|
|
|
munmap(handle->iso_buffer, handle->iso_status.config.data_buf_size);
|
|
|
|
handle->iso_buffer = NULL;
|
|
|
|
ioctl(handle->fd, RAW1394_IOC_ISO_SHUTDOWN, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-04-08 22:13:44 -04:00
|
|
|
#if _POSIX_MEMLOCK > 0
|
|
|
|
result = mlock(handle->iso_packet_infos, buf_packets * sizeof(struct raw1394_iso_packet_info));
|
|
|
|
/* Ignore a permission error - app is responsible for that, */
|
|
|
|
if (result < 0 && result != -EPERM) {
|
|
|
|
munmap(handle->iso_buffer, handle->iso_status.config.data_buf_size);
|
|
|
|
handle->iso_buffer = NULL;
|
|
|
|
ioctl(handle->fd, RAW1394_IOC_ISO_SHUTDOWN, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2006-04-08 21:26:41 -04:00
|
|
|
return 0;
|
2002-11-18 02:40:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int raw1394_iso_xmit_init(raw1394handle_t handle,
|
|
|
|
raw1394_iso_xmit_handler_t handler,
|
|
|
|
unsigned int buf_packets,
|
|
|
|
unsigned int max_packet_size,
|
2002-12-23 22:01:11 -05:00
|
|
|
unsigned char channel,
|
2002-11-18 02:40:21 -05:00
|
|
|
enum raw1394_iso_speed speed,
|
|
|
|
int irq_interval)
|
|
|
|
{
|
2003-11-20 10:47:03 -05:00
|
|
|
if (do_iso_init(handle, buf_packets, max_packet_size, channel, speed, RAW1394_DMA_DEFAULT,
|
2003-02-21 23:31:28 -05:00
|
|
|
irq_interval, RAW1394_IOC_ISO_XMIT_INIT))
|
2002-11-18 02:40:21 -05:00
|
|
|
return -1;
|
2002-12-23 22:01:11 -05:00
|
|
|
|
2003-01-15 08:14:47 -05:00
|
|
|
handle->iso_mode = ISO_XMIT;
|
2002-11-18 02:40:21 -05:00
|
|
|
handle->iso_xmit_handler = handler;
|
2002-12-23 22:01:11 -05:00
|
|
|
handle->next_packet = 0;
|
|
|
|
|
2002-11-18 02:40:21 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int raw1394_iso_recv_init(raw1394handle_t handle,
|
|
|
|
raw1394_iso_recv_handler_t handler,
|
|
|
|
unsigned int buf_packets,
|
|
|
|
unsigned int max_packet_size,
|
2002-12-23 22:01:11 -05:00
|
|
|
unsigned char channel,
|
2003-11-20 10:47:03 -05:00
|
|
|
enum raw1394_iso_dma_recv_mode mode,
|
2002-11-18 02:40:21 -05:00
|
|
|
int irq_interval)
|
|
|
|
{
|
|
|
|
/* any speed will work */
|
2003-11-20 10:47:03 -05:00
|
|
|
if (do_iso_init(handle, buf_packets, max_packet_size, channel, RAW1394_ISO_SPEED_100, mode,
|
2003-02-21 23:31:28 -05:00
|
|
|
irq_interval, RAW1394_IOC_ISO_RECV_INIT))
|
2002-11-18 02:40:21 -05:00
|
|
|
return -1;
|
|
|
|
|
2003-01-15 08:14:47 -05:00
|
|
|
handle->iso_mode = ISO_RECV;
|
2002-11-18 02:40:21 -05:00
|
|
|
handle->iso_recv_handler = handler;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-12-23 22:01:11 -05:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
/* any speed will work */
|
2003-11-20 10:47:03 -05:00
|
|
|
if (do_iso_init(handle, buf_packets, max_packet_size, -1, RAW1394_ISO_SPEED_100,
|
|
|
|
RAW1394_DMA_BUFFERFILL,
|
2003-02-21 23:31:28 -05:00
|
|
|
irq_interval, RAW1394_IOC_ISO_RECV_INIT))
|
2002-12-23 22:01:11 -05:00
|
|
|
return -1;
|
|
|
|
|
2003-01-15 08:14:47 -05:00
|
|
|
handle->iso_mode = ISO_RECV;
|
2002-12-23 22:01:11 -05:00
|
|
|
handle->iso_recv_handler = handler;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int raw1394_iso_recv_listen_channel(raw1394handle_t handle, unsigned char channel)
|
|
|
|
{
|
2003-07-17 13:33:39 -04:00
|
|
|
if (handle->iso_mode != ISO_RECV) {
|
2003-01-15 08:14:47 -05:00
|
|
|
errno = EINVAL;
|
2002-12-23 22:01:11 -05:00
|
|
|
return -1;
|
2003-01-15 08:14:47 -05:00
|
|
|
}
|
2002-12-23 22:01:11 -05:00
|
|
|
|
2003-02-21 23:31:28 -05:00
|
|
|
return ioctl(handle->fd, RAW1394_IOC_ISO_RECV_LISTEN_CHANNEL, channel);
|
2002-12-23 22:01:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int raw1394_iso_recv_unlisten_channel(raw1394handle_t handle, unsigned char channel)
|
|
|
|
{
|
2003-07-17 13:33:39 -04:00
|
|
|
if (handle->iso_mode != ISO_RECV) {
|
2003-01-15 08:14:47 -05:00
|
|
|
errno = EINVAL;
|
2002-12-23 22:01:11 -05:00
|
|
|
return -1;
|
2003-01-15 08:14:47 -05:00
|
|
|
}
|
2002-12-23 22:01:11 -05:00
|
|
|
|
2003-02-21 23:31:28 -05:00
|
|
|
return ioctl(handle->fd, RAW1394_IOC_ISO_RECV_UNLISTEN_CHANNEL, channel);
|
2002-12-23 22:01:11 -05:00
|
|
|
}
|
|
|
|
|
2003-07-17 13:33:39 -04:00
|
|
|
int raw1394_iso_recv_flush(raw1394handle_t handle)
|
|
|
|
{
|
|
|
|
if (handle->iso_mode != ISO_RECV) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioctl(handle->fd, RAW1394_IOC_ISO_RECV_FLUSH, 0);
|
|
|
|
}
|
|
|
|
|
2002-12-23 22:01:11 -05:00
|
|
|
int raw1394_iso_recv_set_channel_mask(raw1394handle_t handle, u_int64_t mask)
|
|
|
|
{
|
2003-07-17 13:33:39 -04:00
|
|
|
if (handle->iso_mode != ISO_RECV) {
|
2003-01-15 08:14:47 -05:00
|
|
|
errno = EINVAL;
|
2002-12-23 22:01:11 -05:00
|
|
|
return -1;
|
2003-01-15 08:14:47 -05:00
|
|
|
}
|
2002-12-23 22:01:11 -05:00
|
|
|
|
2003-02-21 23:31:28 -05:00
|
|
|
return ioctl(handle->fd, RAW1394_IOC_ISO_RECV_SET_CHANNEL_MASK, (void*) &mask);
|
2002-12-23 22:01:11 -05:00
|
|
|
}
|
|
|
|
|
2003-01-15 01:36:19 -05:00
|
|
|
int raw1394_iso_recv_start(raw1394handle_t handle, int start_on_cycle, int tag_mask, int sync)
|
2002-11-18 02:40:21 -05:00
|
|
|
{
|
2003-01-15 01:36:19 -05:00
|
|
|
int args[3];
|
|
|
|
|
2003-01-15 08:14:47 -05:00
|
|
|
if(handle->iso_mode != ISO_RECV) {
|
|
|
|
errno = EINVAL;
|
2002-11-18 02:40:21 -05:00
|
|
|
return -1;
|
2003-01-15 08:14:47 -05:00
|
|
|
}
|
2002-12-23 22:01:11 -05:00
|
|
|
|
2003-01-15 01:36:19 -05:00
|
|
|
args[0] = start_on_cycle;
|
|
|
|
args[1] = tag_mask;
|
|
|
|
args[2] = sync;
|
|
|
|
|
2003-02-21 23:31:28 -05:00
|
|
|
if(ioctl(handle->fd, RAW1394_IOC_ISO_RECV_START, &args[0]))
|
2002-11-18 02:40:21 -05:00
|
|
|
return -1;
|
|
|
|
|
2003-01-15 08:14:47 -05:00
|
|
|
handle->iso_state = ISO_GO;
|
2002-11-18 02:40:21 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _raw1394_iso_xmit_queue_packets(raw1394handle_t handle)
|
|
|
|
{
|
|
|
|
struct raw1394_iso_status *stat = &handle->iso_status;
|
2002-12-23 22:01:11 -05:00
|
|
|
struct raw1394_iso_packets packets;
|
|
|
|
int retval = -1;
|
2003-01-15 08:14:47 -05:00
|
|
|
int stop_sync = 0;
|
2002-12-23 22:01:11 -05:00
|
|
|
|
2003-01-15 08:14:47 -05:00
|
|
|
if(handle->iso_mode != ISO_XMIT) {
|
|
|
|
errno = EINVAL;
|
2002-12-23 22:01:11 -05:00
|
|
|
goto out;
|
2003-01-15 08:14:47 -05:00
|
|
|
}
|
2002-12-23 22:01:11 -05:00
|
|
|
|
2005-07-03 23:00:50 -04:00
|
|
|
/* ensure stat->n_packets is sane */
|
|
|
|
if (stat->n_packets > stat->config.buf_packets)
|
|
|
|
stat->n_packets = stat->config.buf_packets;
|
|
|
|
|
2002-12-23 22:01:11 -05:00
|
|
|
/* we could potentially send up to stat->n_packets packets */
|
|
|
|
packets.n_packets = 0;
|
2006-04-08 21:26:41 -04:00
|
|
|
packets.infos = handle->iso_packet_infos;
|
2002-12-23 22:01:11 -05:00
|
|
|
if(packets.infos == NULL)
|
|
|
|
goto out;
|
|
|
|
|
2005-06-23 13:09:33 -04:00
|
|
|
while(stat->n_packets > 1) {
|
2002-11-18 02:40:21 -05:00
|
|
|
enum raw1394_iso_disposition disp;
|
|
|
|
unsigned int len;
|
|
|
|
|
2002-12-23 22:01:11 -05:00
|
|
|
struct raw1394_iso_packet_info *info = &packets.infos[packets.n_packets];
|
|
|
|
|
|
|
|
info->offset = handle->iso_buf_stride * handle->next_packet;
|
2002-11-18 02:40:21 -05:00
|
|
|
|
|
|
|
/* call handler */
|
|
|
|
disp = handle->iso_xmit_handler(handle,
|
2002-12-23 22:01:11 -05:00
|
|
|
handle->iso_buffer + info->offset,
|
|
|
|
&len,
|
|
|
|
&info->tag, &info->sy,
|
|
|
|
stat->xmit_cycle,
|
2002-11-18 02:40:21 -05:00
|
|
|
_raw1394_iso_dropped(handle));
|
|
|
|
info->len = len;
|
|
|
|
|
|
|
|
/* advance packet cursors and cycle counter */
|
|
|
|
stat->n_packets--;
|
2006-05-09 00:27:38 -04:00
|
|
|
handle->next_packet = increment_and_wrap(handle->next_packet, stat->config.buf_packets);
|
2002-12-23 22:01:11 -05:00
|
|
|
if(stat->xmit_cycle != -1)
|
2006-05-09 00:27:38 -04:00
|
|
|
stat->xmit_cycle = increment_and_wrap(stat->xmit_cycle, 8000);
|
2002-12-23 22:01:11 -05:00
|
|
|
packets.n_packets++;
|
2002-11-18 02:40:21 -05:00
|
|
|
|
|
|
|
if(disp == RAW1394_ISO_DEFER) {
|
2002-12-16 00:11:45 -05:00
|
|
|
/* queue an event so that we don't hang in the next read() */
|
2003-02-21 23:31:28 -05:00
|
|
|
if(ioctl(handle->fd, RAW1394_IOC_ISO_QUEUE_ACTIVITY, 0))
|
2002-12-23 22:01:11 -05:00
|
|
|
goto out_produce;
|
2006-03-03 20:17:46 -05:00
|
|
|
break;
|
|
|
|
} else if(disp == RAW1394_ISO_AGAIN) {
|
|
|
|
/* the last packet was not ready, decrement counter */
|
|
|
|
packets.n_packets--;
|
|
|
|
|
|
|
|
/* queue an event so that we don't hang in the next read() */
|
|
|
|
if(ioctl(handle->fd, RAW1394_IOC_ISO_QUEUE_ACTIVITY, 0))
|
|
|
|
goto out_produce;
|
2002-11-18 02:40:21 -05:00
|
|
|
break;
|
2003-01-15 08:14:47 -05:00
|
|
|
} else if(disp == RAW1394_ISO_STOP) {
|
|
|
|
stop_sync = 1;
|
|
|
|
break;
|
|
|
|
} else if(disp == RAW1394_ISO_STOP_NOSYNC) {
|
|
|
|
raw1394_iso_stop(handle);
|
|
|
|
break;
|
2002-11-18 02:40:21 -05:00
|
|
|
} else if(disp == RAW1394_ISO_ERROR) {
|
2002-12-23 22:01:11 -05:00
|
|
|
goto out_produce;
|
2002-11-18 02:40:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-23 22:01:11 -05:00
|
|
|
/* success */
|
|
|
|
retval = 0;
|
|
|
|
|
|
|
|
out_produce:
|
|
|
|
if(packets.n_packets > 0) {
|
2003-02-21 23:31:28 -05:00
|
|
|
if(ioctl(handle->fd, RAW1394_IOC_ISO_XMIT_PACKETS, &packets))
|
2002-12-23 22:01:11 -05:00
|
|
|
retval = -1;
|
2002-11-18 02:40:21 -05:00
|
|
|
}
|
2003-01-15 08:14:47 -05:00
|
|
|
out:
|
|
|
|
if(stop_sync) {
|
|
|
|
if(raw1394_iso_xmit_sync(handle))
|
|
|
|
return -1;
|
|
|
|
raw1394_iso_stop(handle);
|
|
|
|
}
|
|
|
|
|
2002-11-18 02:40:21 -05:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2003-01-15 08:14:47 -05:00
|
|
|
int raw1394_iso_xmit_write(raw1394handle_t handle, unsigned char *data, unsigned int len,
|
|
|
|
unsigned char tag, unsigned char sy)
|
|
|
|
{
|
|
|
|
struct raw1394_iso_status *stat = &handle->iso_status;
|
|
|
|
struct raw1394_iso_packets packets;
|
|
|
|
struct raw1394_iso_packet_info info;
|
|
|
|
|
|
|
|
if(handle->iso_mode != ISO_XMIT || handle->iso_xmit_handler != NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait until buffer space is available */
|
2005-07-04 00:04:22 -04:00
|
|
|
while(handle->iso_status.n_packets <= 1) {
|
2003-01-15 08:14:47 -05:00
|
|
|
/* if the file descriptor has been set non-blocking,
|
|
|
|
return immediately */
|
|
|
|
if(fcntl(handle->fd, F_GETFL) & O_NONBLOCK) {
|
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(raw1394_loop_iterate(handle)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy the data to the packet buffer */
|
|
|
|
info.offset = handle->next_packet * handle->iso_buf_stride;
|
|
|
|
info.len = len;
|
|
|
|
info.tag = tag;
|
|
|
|
info.sy = sy;
|
|
|
|
|
|
|
|
memcpy(handle->iso_buffer + info.offset, data, len);
|
|
|
|
|
|
|
|
packets.n_packets = 1;
|
|
|
|
packets.infos = &info;
|
|
|
|
|
2003-02-21 23:31:28 -05:00
|
|
|
if(ioctl(handle->fd, RAW1394_IOC_ISO_XMIT_PACKETS, &packets))
|
2003-01-15 08:14:47 -05:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
stat->n_packets--;
|
2006-05-09 00:27:38 -04:00
|
|
|
handle->next_packet = increment_and_wrap(handle->next_packet, stat->config.buf_packets);
|
2003-01-15 08:14:47 -05:00
|
|
|
if(stat->xmit_cycle != -1)
|
2006-05-09 00:27:38 -04:00
|
|
|
stat->xmit_cycle = increment_and_wrap(stat->xmit_cycle, 8000);
|
2003-01-15 08:14:47 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2002-11-18 02:40:21 -05:00
|
|
|
|
|
|
|
int raw1394_iso_xmit_start(raw1394handle_t handle, int start_on_cycle, int prebuffer_packets)
|
|
|
|
{
|
|
|
|
int args[2];
|
2002-12-23 22:01:11 -05:00
|
|
|
|
2003-01-15 08:14:47 -05:00
|
|
|
if(handle->iso_mode != ISO_XMIT) {
|
|
|
|
errno = EINVAL;
|
2002-11-18 02:40:21 -05:00
|
|
|
return -1;
|
2003-01-15 08:14:47 -05:00
|
|
|
}
|
2002-11-18 02:40:21 -05:00
|
|
|
|
|
|
|
args[0] = start_on_cycle;
|
|
|
|
args[1] = prebuffer_packets;
|
2002-12-23 22:01:11 -05:00
|
|
|
|
2003-02-21 23:31:28 -05:00
|
|
|
if(ioctl(handle->fd, RAW1394_IOC_ISO_XMIT_START, &args[0]))
|
2002-11-18 02:40:21 -05:00
|
|
|
return -1;
|
2002-12-23 22:01:11 -05:00
|
|
|
|
2003-01-15 08:14:47 -05:00
|
|
|
handle->iso_state = ISO_GO;
|
2002-11-18 02:40:21 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-15 08:14:47 -05:00
|
|
|
int raw1394_iso_xmit_sync(raw1394handle_t handle)
|
|
|
|
{
|
|
|
|
if(handle->iso_mode != ISO_XMIT) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2003-02-21 23:31:28 -05:00
|
|
|
return ioctl(handle->fd, RAW1394_IOC_ISO_XMIT_SYNC, 0);
|
2003-01-15 08:14:47 -05:00
|
|
|
}
|
|
|
|
|
2002-11-18 02:40:21 -05:00
|
|
|
void raw1394_iso_stop(raw1394handle_t handle)
|
|
|
|
{
|
2003-01-15 08:14:47 -05:00
|
|
|
if(handle->iso_mode == ISO_INACTIVE) {
|
2002-11-18 02:40:21 -05:00
|
|
|
return;
|
2003-01-15 08:14:47 -05:00
|
|
|
}
|
2002-12-23 22:01:11 -05:00
|
|
|
|
2003-02-21 23:31:28 -05:00
|
|
|
ioctl(handle->fd, RAW1394_IOC_ISO_XMIT_RECV_STOP, 0);
|
2003-01-15 08:14:47 -05:00
|
|
|
handle->iso_state = ISO_STOP;
|
2002-11-18 02:40:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void raw1394_iso_shutdown(raw1394handle_t handle)
|
|
|
|
{
|
|
|
|
if(handle->iso_buffer) {
|
2002-12-23 22:01:11 -05:00
|
|
|
munmap(handle->iso_buffer, handle->iso_status.config.data_buf_size);
|
2002-11-18 02:40:21 -05:00
|
|
|
handle->iso_buffer = NULL;
|
|
|
|
}
|
2003-01-15 08:14:47 -05:00
|
|
|
|
|
|
|
if(handle->iso_mode != ISO_INACTIVE) {
|
|
|
|
raw1394_iso_stop(handle);
|
2003-02-21 23:31:28 -05:00
|
|
|
ioctl(handle->fd, RAW1394_IOC_ISO_SHUTDOWN, 0);
|
2003-01-15 08:14:47 -05:00
|
|
|
}
|
|
|
|
|
2006-04-08 21:26:41 -04:00
|
|
|
if(handle->iso_packet_infos) {
|
2006-04-08 22:13:44 -04:00
|
|
|
#if _POSIX_MEMLOCK > 0
|
|
|
|
munlock(handle->iso_packet_infos,
|
|
|
|
handle->iso_status.config.buf_packets *
|
|
|
|
sizeof(struct raw1394_iso_packet_info));
|
|
|
|
#endif
|
2006-04-08 21:26:41 -04:00
|
|
|
free(handle->iso_packet_infos);
|
|
|
|
handle->iso_packet_infos = NULL;
|
|
|
|
}
|
|
|
|
|
2003-01-15 08:14:47 -05:00
|
|
|
handle->iso_mode = ISO_INACTIVE;
|
2002-11-18 02:40:21 -05:00
|
|
|
}
|
|
|
|
|
2007-02-14 00:29:18 -05:00
|
|
|
int raw1394_read_cycle_timer(raw1394handle_t handle,
|
|
|
|
u_int32_t *cycle_timer, u_int64_t *local_time)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct raw1394_cycle_timer ctr = { 0 };
|
|
|
|
|
|
|
|
err = ioctl(handle->fd, RAW1394_IOC_GET_CYCLE_TIMER, &ctr);
|
|
|
|
if (!err) {
|
|
|
|
*cycle_timer = ctr.cycle_timer;
|
|
|
|
*local_time = ctr.local_time;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2002-11-18 02:40:21 -05:00
|
|
|
static int _raw1394_iso_recv_packets(raw1394handle_t handle)
|
|
|
|
{
|
|
|
|
struct raw1394_iso_status *stat = &handle->iso_status;
|
2002-12-23 22:01:11 -05:00
|
|
|
struct raw1394_iso_packets packets;
|
|
|
|
|
|
|
|
int retval = -1, packets_done = 0;
|
2002-11-18 02:40:21 -05:00
|
|
|
|
2003-01-15 08:14:47 -05:00
|
|
|
if(handle->iso_mode != ISO_RECV) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-12-23 22:01:11 -05:00
|
|
|
/* ask the kernel to fill an array with packet info structs */
|
|
|
|
packets.n_packets = stat->n_packets;
|
2006-04-08 21:26:41 -04:00
|
|
|
packets.infos = handle->iso_packet_infos;
|
2002-12-23 22:01:11 -05:00
|
|
|
if(packets.infos == NULL)
|
|
|
|
goto out;
|
|
|
|
|
2003-02-21 23:31:28 -05:00
|
|
|
if(ioctl(handle->fd, RAW1394_IOC_ISO_RECV_PACKETS, &packets) < 0)
|
2006-04-08 21:26:41 -04:00
|
|
|
goto out;
|
2002-12-23 22:01:11 -05:00
|
|
|
|
2002-11-18 02:40:21 -05:00
|
|
|
while(stat->n_packets > 0) {
|
|
|
|
struct raw1394_iso_packet_info *info;
|
|
|
|
enum raw1394_iso_disposition disp;
|
|
|
|
|
2002-12-23 22:01:11 -05:00
|
|
|
info = &packets.infos[packets_done];
|
2002-11-18 02:40:21 -05:00
|
|
|
|
|
|
|
/* call handler */
|
|
|
|
disp = handle->iso_recv_handler(handle,
|
2002-12-23 22:01:11 -05:00
|
|
|
handle->iso_buffer + info->offset,
|
2002-11-18 02:40:21 -05:00
|
|
|
info->len, info->channel,
|
|
|
|
info->tag, info->sy,
|
|
|
|
info->cycle,
|
|
|
|
_raw1394_iso_dropped(handle));
|
|
|
|
|
|
|
|
/* advance packet cursors */
|
|
|
|
stat->n_packets--;
|
|
|
|
packets_done++;
|
|
|
|
|
|
|
|
if(disp == RAW1394_ISO_DEFER) {
|
2002-12-16 00:11:45 -05:00
|
|
|
/* queue an event so that we don't hang in the next read() */
|
2003-02-21 23:31:28 -05:00
|
|
|
if(ioctl(handle->fd, RAW1394_IOC_ISO_QUEUE_ACTIVITY, 0))
|
2002-12-23 22:01:11 -05:00
|
|
|
goto out_consume;
|
2002-11-18 02:40:21 -05:00
|
|
|
break;
|
2003-01-15 08:14:47 -05:00
|
|
|
} else if(disp == RAW1394_ISO_STOP || disp == RAW1394_ISO_STOP_NOSYNC) {
|
|
|
|
raw1394_iso_stop(handle);
|
|
|
|
break;
|
2002-11-18 02:40:21 -05:00
|
|
|
} else if(disp == RAW1394_ISO_ERROR) {
|
2002-12-23 22:01:11 -05:00
|
|
|
goto out_consume;
|
2002-11-18 02:40:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-23 22:01:11 -05:00
|
|
|
/* success */
|
|
|
|
retval = 0;
|
|
|
|
|
|
|
|
out_consume:
|
2002-11-18 02:40:21 -05:00
|
|
|
if(packets_done > 0) {
|
2003-02-21 23:31:28 -05:00
|
|
|
if(ioctl(handle->fd, RAW1394_IOC_ISO_RECV_RELEASE_PACKETS, packets_done))
|
2002-12-23 22:01:11 -05:00
|
|
|
retval = -1;
|
2002-11-18 02:40:21 -05:00
|
|
|
}
|
2002-12-23 22:01:11 -05:00
|
|
|
out:
|
2002-11-18 02:40:21 -05:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* run the ISO state machine; called from raw1394_loop_iterate() */
|
|
|
|
int _raw1394_iso_iterate(raw1394handle_t handle)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2003-01-15 08:14:47 -05:00
|
|
|
if(handle->iso_mode == ISO_INACTIVE)
|
2002-11-18 02:40:21 -05:00
|
|
|
return 0;
|
|
|
|
|
2003-02-21 23:31:28 -05:00
|
|
|
err = ioctl(handle->fd, RAW1394_IOC_ISO_GET_STATUS, &handle->iso_status);
|
2002-11-18 02:40:21 -05:00
|
|
|
if(err != 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
handle->iso_packets_dropped += handle->iso_status.overflows;
|
|
|
|
|
2003-01-15 08:14:47 -05:00
|
|
|
if(handle->iso_state == ISO_GO) {
|
|
|
|
if(handle->iso_mode == ISO_XMIT) {
|
|
|
|
if(handle->iso_xmit_handler) {
|
|
|
|
return _raw1394_iso_xmit_queue_packets(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(handle->iso_mode == ISO_RECV) {
|
|
|
|
if(handle->iso_recv_handler) {
|
|
|
|
return _raw1394_iso_recv_packets(handle);
|
|
|
|
}
|
|
|
|
}
|
2002-11-18 02:40:21 -05:00
|
|
|
}
|
2002-12-23 22:01:11 -05:00
|
|
|
|
2002-11-18 02:40:21 -05:00
|
|
|
return 0;
|
|
|
|
}
|