All pastes #89530 Raw Edit

Something

public text v1 · immutable
#89530 ·published 2006-07-16 11:57 UTC
rendered paste body
diff -dNpru old/lib/libdvbapi/dvbca.c new/lib/libdvbapi/dvbca.c
--- old/lib/libdvbapi/dvbca.c	2006-07-15 21:21:07.000000000 +0200
+++ new/lib/libdvbapi/dvbca.c	2006-07-16 13:53:05.000000000 +0200
@@ -1,7 +1,9 @@
 /*
- * libdvbca - interface onto raw CA devices
+ * dvbapi - low level dvb support
+ * conditional access devices
  *
- * Copyright (C) 2006 Andrew de Quincey (adq_dvb@lidskialf.net)
+ * Copyright (C) 2006 Christoph Pfister <christophpfister@gmail.com>
+ * Copyright (C) 2006 Andrew de Quincey <adq_dvb@lidskialf.net>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -10,150 +12,194 @@
  *
  * This library 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
- * Lesser General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
-#include <stdlib.h>
-#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <linux/dvb/ca.h>
+#include <malloc.h>
 #include <stdio.h>
-#include <sys/param.h>
+#include <string.h>
 #include <sys/ioctl.h>
-#include <fcntl.h>
 #include <unistd.h>
-#include <ctype.h>
-#include <errno.h>
-#include <linux/dvb/ca.h>
+
 #include "dvbca.h"
 
+struct dvbapi_ca_handle {
+	int fd;
+};
 
-int dvbca_open(int adapter, int cadevice)
+int dvbapi_ca_create(struct dvbapi_ca_handle **handle, int adapter, int device)
 {
-	char filename[PATH_MAX+1];
+	struct dvbapi_ca_handle *tmp_handle;
+	char buffer[42];
 	int fd;
 
-	sprintf(filename, "/dev/dvb/adapter%i/ca%i", adapter, cadevice);
-	if ((fd = open(filename, O_RDWR)) < 0) {
-		// if that failed, try a flat /dev structure
-		sprintf(filename, "/dev/dvb%i.ca%i", adapter, cadevice);
-		fd = open(filename, O_RDWR);
+	if (!handle)
+		return -EINVAL;
+
+	if ((tmp_handle = malloc(sizeof(*tmp_handle))) == NULL)
+		return -ENOMEM;
+
+	snprintf(buffer, sizeof(buffer), "/dev/dvb/adapter%i/ca%i", adapter, device);
+	if ((fd = open(buffer, O_RDWR)) < 0) {
+		/* try flat device structure */
+		snprintf(buffer, sizeof(buffer), "/dev/dvb%i/ca%i", adapter, device);
+		if ((fd = open(buffer, O_RDWR)) < 0) {
+			free(tmp_handle);
+			return fd;
+		}
 	}
 
-	return fd;
+	tmp_handle->fd = fd;
+	*handle = tmp_handle;
+	return 0;
 }
 
-int dvbca_reset(int fd, uint8_t slot)
+void dvbapi_ca_destroy(struct dvbapi_ca_handle *handle)
 {
-	return ioctl(fd, CA_RESET, (1 << slot));
+	if (handle) {
+		close(handle->fd);
+		free(handle);
+	}
 }
 
-int dvbca_get_interface_type(int fd, uint8_t slot)
+int dvbapi_ca_fd(struct dvbapi_ca_handle *handle)
 {
-	ca_slot_info_t info;
+	if (!handle)
+		return -EINVAL;
 
-	info.num = slot;
-	if (ioctl(fd, CA_GET_SLOT_INFO, &info))
-		return -1;
+	return handle->fd;
+}
 
-	if (info.type & CA_CI_LINK)
-		return DVBCA_INTERFACE_LINK;
-	if (info.type & CA_CI)
-		return DVBCA_INTERFACE_HLCI;
+int dvbapi_ca_type(struct dvbapi_ca_handle *handle)
+{
+	struct ca_caps caps;
+	int ret_val;
 
-	return -1;
+	if (!handle)
+		return -EINVAL;
+
+	if ((ret_val = ioctl(handle->fd, CA_GET_CAP, &caps)) != 0)
+		return ret_val;
+
+	if (caps.slot_type & CA_CI_LINK)
+		return DVBAPI_CA_LLCI;
+
+	if (caps.slot_type & CA_CI)
+		return DVBAPI_CA_HLCI;
+
+	return -ENOTSUP;
 }
 
-int dvbca_get_cam_state(int fd, uint8_t slot)
+int dvbapi_cam_slots(struct dvbapi_ca_handle *handle)
 {
-	ca_slot_info_t info;
+	struct ca_caps caps;
+	int ret_val;
 
-	info.num = slot;
-	if (ioctl(fd, CA_GET_SLOT_INFO, &info))
-		return -1;
+	if (!handle)
+		return -EINVAL;
 
-	if (info.flags == 0)
-		return DVBCA_CAMSTATE_MISSING;
-	if (info.flags & CA_CI_MODULE_READY)
-		return DVBCA_CAMSTATE_READY;
-	if (info.flags & CA_CI_MODULE_PRESENT)
-		return DVBCA_CAMSTATE_INITIALISING;
+	if ((ret_val = ioctl(handle->fd, CA_GET_CAP, &caps)) != 0)
+		return ret_val;
 
-	return -1;
+	return caps.slot_num;
 }
 
-int dvbca_link_write(int fd, uint8_t slot, uint8_t connection_id,
-		     uint8_t *data, uint16_t data_length)
+int dvbapi_cam_reset(struct dvbapi_ca_handle *handle, int slot)
 {
-	uint8_t *buf = malloc(data_length + 2);
-	if (buf == NULL)
-		return -1;
+	if (!handle || ((unsigned int) (slot + 1) >= 33))
+		return -EINVAL;
 
-	buf[0] = slot;
-	buf[1] = connection_id;
-	memcpy(buf+2, data, data_length);
+	if (slot != -1)
+		return ioctl(handle->fd, CA_RESET, (1 << slot));
+	else
+		return ioctl(handle->fd, CA_RESET, 0);
+}
 
-	int result = write(fd, buf, data_length+2);
-	free(buf);
-	return result;
+int dvbapi_cam_state(struct dvbapi_ca_handle *handle, int slot)
+{
+	struct ca_slot_info info;
+	int ret_val;
+
+	if (!handle || ((unsigned int) slot >= 32))
+		return -EINVAL;
+
+	info.num = slot;
+	if ((ret_val = ioctl(handle->fd, CA_GET_SLOT_INFO, &info)) != 0)
+		return ret_val;
+
+	if (info.flags & CA_CI_MODULE_READY)
+		return DVBAPI_CAM_READY;
+
+	if (info.flags & CA_CI_MODULE_PRESENT)
+		return DVBAPI_CAM_INITIALISING;
+
+	return DVBAPI_CAM_MISSING;
 }
 
-int dvbca_link_read(int fd, uint8_t *slot, uint8_t *connection_id,
-		     uint8_t *data, uint16_t data_length)
+int dvbapi_llci_write(struct dvbapi_ca_handle *handle, char *buffer, int length)
 {
-	int size;
+	int ret_val;
 
-	uint8_t *buf = malloc(data_length + 2);
-	if (buf == NULL)
-		return -1;
+	if (!handle || !buffer || (length < 0))
+		return -EINVAL;
 
-	if ((size = read(fd, buf, data_length+2)) < 2)
-		return -1;
+	if ((ret_val = write(handle->fd, buffer, length)) < 0)
+		return ret_val;
 
-	*slot = buf[0];
-	*connection_id = buf[1];
-	memcpy(data, buf+2, size-2);
-	free(buf);
+	return (ret_val == length) ? 0 : -EIO;
+}
 
-	return size - 2;
+int dvbapi_llci_read(struct dvbapi_ca_handle *handle, char *buffer, int length)
+{
+	if (!handle || !buffer || (length < 0))
+		return -EINVAL;
+
+	return read(handle->fd, buffer, length);
 }
 
-int dvbca_hlci_write(int fd, uint8_t *data, uint16_t data_length)
+int dvbapi_hlci_write(struct dvbapi_ca_handle *handle, int slot, char *buffer, int length)
 {
 	struct ca_msg msg;
 
-	if (data_length > 256) {
-		return -1;
-	}
-	memset(&msg, 0, sizeof(msg));
-	msg.length = data_length;
+	if (!handle || (slot != 0) || !buffer || ((unsigned int) length > 256))
+		return -EINVAL;
 
-	memcpy(msg.msg, data, data_length);
+	memset(&msg, 0, sizeof(msg));
+	msg.length = length;
+	memcpy(msg.msg, buffer, length);
 
-	return ioctl(fd, CA_SEND_MSG, &msg);
+	return ioctl(handle->fd, CA_SEND_MSG, &msg);
 }
 
-int dvbca_hlci_read(int fd, uint32_t app_tag, uint8_t *data,
-		    uint16_t data_length)
+int dvbapi_hlci_read(struct dvbapi_ca_handle *handle, int slot,
+		     int tag, char *buffer, int length)
 {
 	struct ca_msg msg;
+	int ret_val;
+
+	if (!handle || (slot != 0) || !buffer || (length < 0))
+		return -EINVAL;
 
-	if (data_length > 256) {
-		data_length = 256;
-	}
 	memset(&msg, 0, sizeof(msg));
-	msg.length = data_length;
-	msg.msg[0] = app_tag >> 16;
-	msg.msg[1] = app_tag >> 8;
-	msg.msg[2] = app_tag;
+	msg.length = (length <= 256) ? length : 256;
+	msg.msg[0] = tag >> 16;
+	msg.msg[1] = tag >> 8;
+	msg.msg[2] = tag;
 
-	int status = ioctl(fd, CA_GET_MSG, &msg);
-	if (status < 0) return status;
+	if ((ret_val = ioctl(handle->fd, CA_GET_MSG, &msg)) != 0)
+		return ret_val;
 
-	if (msg.length > data_length) msg.length = data_length;
-	memcpy(data, msg.msg, msg.length);
-	return msg.length;
+	if (msg.length < (unsigned int) length)
+		length = msg.length;
+
+	memcpy(buffer, msg.msg, length);
+	return length;
 }
diff -dNpru old/lib/libdvbapi/dvbca.h new/lib/libdvbapi/dvbca.h
--- old/lib/libdvbapi/dvbca.h	2006-07-15 21:21:07.000000000 +0200
+++ new/lib/libdvbapi/dvbca.h	2006-07-16 13:53:05.000000000 +0200
@@ -1,7 +1,9 @@
 /*
- * libdvbca - interface onto raw CA devices
+ * dvbapi - low level dvb support
+ * conditional access devices
  *
- * Copyright (C) 2006 Andrew de Quincey (adq_dvb@lidskialf.net)
+ * Copyright (C) 2006 Christoph Pfister <christophpfister@gmail.com>
+ * Copyright (C) 2006 Andrew de Quincey <adq_dvb@lidskialf.net>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -10,126 +12,150 @@
  *
  * This library 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
- * Lesser General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
-#ifndef LIBDVBCA_H
-#define LIBDVBCA_H 1
+#ifndef DVBAPI_CA_H
+#define DVBAPI_CA_H
 
 #ifdef __cplusplus
-extern "C"
-{
+extern "C" {
 #endif
 
-#include <stdint.h>
+/**
+ * CA device types
+ */
+enum dvbapi_ca_types {
+	DVBAPI_CA_LLCI,
+	DVBAPI_CA_HLCI
+};
 
 /**
- * The types of CA interface we support.
+ * CAM states
  */
-#define DVBCA_INTERFACE_LINK 0
-#define DVBCA_INTERFACE_HLCI 1
+enum dvbapi_cam_states {
+	DVBAPI_CAM_MISSING,
+	DVBAPI_CAM_INITIALISING,
+	DVBAPI_CAM_READY
+};
 
 /**
- * States a CAM in a slot can be in.
+ * CA device handle
  */
-#define DVBCA_CAMSTATE_MISSING 0
-#define DVBCA_CAMSTATE_INITIALISING 1
-#define DVBCA_CAMSTATE_READY 2
+struct dvbapi_ca_handle;
 
+/**
+ * Create a CA device handle (multiple CAMs can be accessed through it)
+ *
+ * @param handle Handle to initialise
+ * @param adapter Selected DVB adapter
+ * @param device Selected CA device on that adapter
+ * @return 0 on success, error code on failure
+ */
+extern int dvbapi_ca_create(struct dvbapi_ca_handle **handle, int adapter, int device);
 
 /**
- * Open a CA device. Multiple CAMs can be accessed through a CA device.
+ * Destroy the CA device handle
  *
- * @param adapter Index of the DVB adapter.
- * @param cadevice Index of the CA device on that adapter (usually 0).
- * @return A unix file descriptor on success, or -1 on failure.
+ * @param handle Handle to destroy
+ * @return nothing
  */
-extern int dvbca_open(int adapter, int cadevice);
+extern void dvbapi_ca_destroy(struct dvbapi_ca_handle *handle);
 
 /**
- * Reset a CAM.
+ * Return the file descriptor - use it only for polling!
  *
- * @param fd File handle opened with dvbca_open.
- * @param slot Slot where the requested CAM is in.
- * @return 0 on success, -1 on failure.
+ * @param handle Handle created with dvbapi_ca_create
+ * @return file descriptor (>= 0) on success, error code on failure
  */
-extern int dvbca_reset(int fd, uint8_t slot);
+extern int dvbapi_ca_fd(struct dvbapi_ca_handle *handle);
 
 /**
- * Get the interface type of a CAM.
+ * Return the type of the CA device
  *
- * @param fd File handle opened with dvbca_open.
- * @param slot Slot where the requested CAM is in.
- * @return One of the DVBCA_INTERFACE_* values, or -1 on failure.
+ * @param handle Handle created with dvbapi_ca_create
+ * @return enum dvbapi_ca_types (>= 0) on success, error code on failure
  */
-extern int dvbca_get_interface_type(int fd, uint8_t slot);
+extern int dvbapi_ca_type(struct dvbapi_ca_handle *handle);
 
 /**
- * Get the state of a CAM.
+ * Return the number of CAM slots
  *
- * @param fd File handle opened with dvbca_open.
- * @param slot Slot where the requested CAM is in.
- * @return One of the DVBCA_CAMSTATE_* values, or -1 on failure.
+ * @param handle Handle created with dvbapi_ca_create
+ * @return Number of CAM slots (>= 0) on success, error code on failure
  */
-extern int dvbca_get_cam_state(int fd, uint8_t slot);
+extern int dvbapi_cam_slots(struct dvbapi_ca_handle *handle);
 
 /**
- * Write a message to a CAM using a link-layer interface.
+ * Reset a CAM
  *
- * @param fd File handle opened with dvbca_open.
- * @param slot Slot where the requested CAM is in.
- * @param connection_id Connection ID of the message.
- * @param data Data to write.
- * @param data_length Number of bytes to write.
- * @return 0 on success, or -1 on failure.
+ * @param handle Handle created with dvbapi_ca_create
+ * @param slot Selected CAM (-1 to reset all CAMs)
+ * @return 0 on success, error code on failure
  */
-extern int dvbca_link_write(int fd, uint8_t slot, uint8_t connection_id,
-			    uint8_t *data, uint16_t data_length);
+extern int dvbapi_cam_reset(struct dvbapi_ca_handle *handle, int slot);
 
 /**
- * Read a message from a CAM using a link-layer interface.
+ * Get the state of a CAM
  *
- * @param fd File handle opened with dvbca_open.
- * @param slot Slot where the responding CAM is in.
- * @param connection_id Destination for the connection ID the message came from.
- * @param data Data that was read.
- * @param data_length Max number of bytes to read.
- * @return Number of bytes read on success, or -1 on failure.
+ * @param handle Handle created with dvbapi_ca_create
+ * @param slot Selected CAM
+ * @return enum dvbapi_cam_states (>= 0) on success, error code on failure
  */
-extern int dvbca_link_read(int fd, uint8_t *slot, uint8_t *connection_id,
-			   uint8_t *data, uint16_t data_length);
+extern int dvbapi_cam_state(struct dvbapi_ca_handle *handle, int slot);
 
-// FIXME how do we determine which CAM slot of a CA is meant?
 /**
- * Write a message to a CAM using an HLCI interface.
+ * Write a message to a CAM using llci
  *
- * @param fd File handle opened with dvbca_open.
- * @param data Data to write.
- * @param data_length Number of bytes to write.
- * @return 0 on success, or -1 on failure.
+ * @param handle Handle created with dvbapi_ca_create
+ * @param buffer Data buffer
+ * @param length Number of bytes to write
+ * @return 0 on success, error code on failure
  */
-extern int dvbca_hlci_write(int fd, uint8_t *data, uint16_t data_length);
+extern int dvbapi_llci_write(struct dvbapi_ca_handle *handle, char *buffer, int length);
 
-// FIXME how do we determine which CAM slot of a CA is meant?
 /**
- * Read a message from a CAM using an HLCI interface.
+ * Read a message from a CAM using llci
  *
- * @param fd File handle opened with dvbca_open.
- * @param app_tag Application layer tag giving the message type to read.
- * @param data Data that was read.
- * @param data_length Max number of bytes to read.
- * @return Number of bytes read on success, or -1 on failure.
+ * @param handle Handle created with dvbapi_ca_create
+ * @param buffer Data buffer
+ * @param length Maximal number of bytes to read
+ * @return Number of bytes read (>= 0) on success, error code on failure
  */
-extern int dvbca_hlci_read(int fd, uint32_t app_tag, uint8_t *data,
-			   uint16_t data_length);
+extern int dvbapi_llci_read(struct dvbapi_ca_handle *handle, char *buffer, int length);
+
+/**
+ * Write a message to a CAM using hlci
+ *
+ * @param handle Handle created with dvbapi_ca_create
+ * @param slot Selected CAM
+ * @param buffer Data buffer
+ * @param length Number of bytes to write
+ * @return 0 on success, error code on failure
+ */
+extern int dvbapi_hlci_write(struct dvbapi_ca_handle *handle,
+			     int slot, char *buffer, int length);
+
+/**
+ * Read a message from a CAM using hlci
+ *
+ * @param handle Handle created with dvbapi_ca_create
+ * @param slot Selected CAM
+ * @param tag Application layer tag specifying the message to read
+ * @param buffer Data buffer
+ * @param length Maximal number of bytes to read
+ * @return Number of bytes read (>= 0) on success, error code on failure
+ */
+extern int dvbapi_hlci_read(struct dvbapi_ca_handle *handle, int slot,
+			    int tag, char *buffer, int length);
 
 #ifdef __cplusplus
 }
 #endif
 
-#endif // LIBDVBCA_H
+#endif /* DVBAPI_CA_H */
diff -dNpru old/lib/libdvben50221/en50221_stdcam.c new/lib/libdvben50221/en50221_stdcam.c
--- old/lib/libdvben50221/en50221_stdcam.c	2006-07-15 21:21:07.000000000 +0200
+++ new/lib/libdvben50221/en50221_stdcam.c	2006-07-16 13:53:05.000000000 +0200
@@ -32,23 +32,22 @@ struct en50221_stdcam *en50221_stdcam_cr
 					     struct en50221_session_layer *sl)
 {
 	struct en50221_stdcam *result = NULL;
+	struct dvbapi_ca_handle *handle;
 
-	int cafd = dvbca_open(adapter, 0);
-	if (cafd == -1)
+	if (dvbapi_ca_create(&handle, adapter, 0) != 0)
 		return NULL;
 
-	int ca_type = dvbca_get_interface_type(cafd, slotnum);
-	switch(ca_type) {
-	case DVBCA_INTERFACE_LINK:
-		result = en50221_stdcam_llci_create(cafd, slotnum, tl, sl);
+	switch(dvbapi_ca_type(handle)) {
+	case DVBAPI_CA_LLCI:
+		result = en50221_stdcam_llci_create(handle, slotnum, tl, sl);
 		break;
 
-	case DVBCA_INTERFACE_HLCI:
-		result = en50221_stdcam_hlci_create(cafd, slotnum);
+	case DVBAPI_CA_HLCI:
+		result = en50221_stdcam_hlci_create(handle, slotnum);
 		break;
 	}
 
 	if (result == NULL)
-		close(cafd);
+		dvbapi_ca_destroy(handle);
 	return result;
 }
diff -dNpru old/lib/libdvben50221/en50221_stdcam.h new/lib/libdvben50221/en50221_stdcam.h
--- old/lib/libdvben50221/en50221_stdcam.h	2006-07-15 21:21:07.000000000 +0200
+++ new/lib/libdvben50221/en50221_stdcam.h	2006-07-16 13:53:05.000000000 +0200
@@ -53,7 +53,7 @@ struct en50221_stdcam {
 	void (*dvbtime)(struct en50221_stdcam *stdcam, time_t dvbtime);
 
 	/* destroy the stdcam instance */
-	void (*destroy)(struct en50221_stdcam *stdcam, int closefd);
+	void (*destroy)(struct en50221_stdcam *stdcam);
 };
 
 /**
@@ -65,7 +65,7 @@ struct en50221_stdcam {
  * @param sl Session layer instance to use.
  * @return en50221_stdcam instance, or NULL on error.
  */
-extern struct en50221_stdcam *en50221_stdcam_llci_create(int cafd, int slotnum,
+extern struct en50221_stdcam *en50221_stdcam_llci_create(struct dvbapi_ca_handle *handle, int slotnum,
 						  struct en50221_transport_layer *tl,
 						  struct en50221_session_layer *sl);
 
@@ -76,7 +76,7 @@ extern struct en50221_stdcam *en50221_st
  * @param slotnum Slotnum on that CA device.
  * @return en50221_stdcam instance, or NULL on error.
  */
-extern struct en50221_stdcam *en50221_stdcam_hlci_create(int cafd, int slotnum);
+extern struct en50221_stdcam *en50221_stdcam_hlci_create(struct dvbapi_ca_handle *handle, int slotnum);
 
 /**
  * Convenience method to create a STDCAM interface for a ca device on a particular adapter.
diff -dNpru old/lib/libdvben50221/en50221_stdcam_hlci.c new/lib/libdvben50221/en50221_stdcam_hlci.c
--- old/lib/libdvben50221/en50221_stdcam_hlci.c	2006-07-15 21:21:07.000000000 +0200
+++ new/lib/libdvben50221/en50221_stdcam_hlci.c	2006-07-16 13:53:05.000000000 +0200
@@ -33,13 +33,13 @@
 struct en50221_stdcam_hlci {
 	struct en50221_stdcam stdcam;
 
-	int cafd;
+	struct dvbapi_ca_handle *handle;
 	int slotnum;
 	int initialised;
 	struct en50221_app_send_functions sendfuncs;
 };
 
-static void en50221_stdcam_hlci_destroy(struct en50221_stdcam *stdcam, int closefd);
+static void en50221_stdcam_hlci_destroy(struct en50221_stdcam *stdcam);
 static enum en50221_stdcam_status en50221_stdcam_hlci_poll(struct en50221_stdcam *stdcam);
 static int hlci_cam_added(struct en50221_stdcam_hlci *hlci);
 static int hlci_send_data(void *arg, uint16_t session_number,
@@ -50,7 +50,7 @@ static int hlci_send_datav(void *arg, ui
 
 
 
-struct en50221_stdcam *en50221_stdcam_hlci_create(int cafd, int slotnum)
+struct en50221_stdcam *en50221_stdcam_hlci_create(struct dvbapi_ca_handle *handle, int slotnum)
 {
 	// try and allocate space for the HLCI stdcam
 	struct en50221_stdcam_hlci *hlci =
@@ -77,11 +77,11 @@ struct en50221_stdcam *en50221_stdcam_hl
 	hlci->stdcam.destroy = en50221_stdcam_hlci_destroy;
 	hlci->stdcam.poll = en50221_stdcam_hlci_poll;
 	hlci->slotnum = slotnum;
-	hlci->cafd = cafd;
+	hlci->handle = handle;
 	return &hlci->stdcam;
 }
 
-static void en50221_stdcam_hlci_destroy(struct en50221_stdcam *stdcam, int closefd)
+static void en50221_stdcam_hlci_destroy(struct en50221_stdcam *stdcam)
 {
 	struct en50221_stdcam_hlci *hlci = (struct en50221_stdcam_hlci *) stdcam;
 
@@ -92,8 +92,7 @@ static void en50221_stdcam_hlci_destroy(
 	if (hlci->stdcam.mmi_resource)
 		en50221_app_mmi_destroy(hlci->stdcam.mmi_resource);
 
-	if (closefd)
-		close(hlci->cafd);
+	dvbapi_ca_destroy(hlci->handle);
 
 	free(hlci);
 }
@@ -102,16 +101,16 @@ static enum en50221_stdcam_status en5022
 {
 	struct en50221_stdcam_hlci *hlci = (struct en50221_stdcam_hlci *) stdcam;
 
-	switch(dvbca_get_cam_state(hlci->cafd, hlci->slotnum)) {
-	case DVBCA_CAMSTATE_MISSING:
-		hlci->initialised = 0;
-		break;
-
-	case DVBCA_CAMSTATE_READY:
-	case DVBCA_CAMSTATE_INITIALISING:
+	switch(dvbapi_cam_state(hlci->handle, hlci->slotnum)) {
+	case DVBAPI_CAM_INITIALISING:
+	case DVBAPI_CAM_READY:
 		if (!hlci->initialised)
 			hlci_cam_added(hlci);
 		break;
+
+	default:
+		hlci->initialised = 0;
+		break;
 	}
 
 	// delay to prevent busy loop
@@ -134,7 +133,7 @@ static int hlci_cam_added(struct en50221
 	if (en50221_app_ai_enquiry(hlci->stdcam.ai_resource, 0)) {
 		return -EIO;
 	}
-	if ((size = dvbca_hlci_read(hlci->cafd, TAG_APP_INFO, buf, sizeof(buf))) < 0) {
+	if ((size = dvbapi_hlci_read(hlci->handle, hlci->slotnum, TAG_APP_INFO, buf, sizeof(buf))) < 0) {
 		return size;
 	}
 	if (en50221_app_ai_message(hlci->stdcam.ai_resource, 0, 0, EN50221_APP_AI_RESOURCEID, buf, size)) {
@@ -181,7 +180,7 @@ static int hlci_send_data(void *arg, uin
 	(void) session_number;
 	struct en50221_stdcam_hlci *hlci = arg;
 
-	return dvbca_hlci_write(hlci->cafd, data, data_length);
+	return dvbapi_hlci_write(hlci->handle, hlci->slotnum, data, data_length);
 }
 
 static int hlci_send_datav(void *arg, uint16_t session_number,
@@ -210,7 +209,7 @@ static int hlci_send_datav(void *arg, ui
 	}
 
 	// sendit and cleanup
-	int status = dvbca_hlci_write(hlci->cafd, buf, data_size);
+	int status = dvbapi_hlci_write(hlci->handle, hlci->slotnum, buf, data_size);
 	free(buf);
 	return status;
 }
diff -dNpru old/lib/libdvben50221/en50221_stdcam_llci.c new/lib/libdvben50221/en50221_stdcam_llci.c
--- old/lib/libdvben50221/en50221_stdcam_llci.c	2006-07-16 13:52:04.000000000 +0200
+++ new/lib/libdvben50221/en50221_stdcam_llci.c	2006-07-16 13:53:05.000000000 +0200
@@ -55,7 +55,7 @@ struct llci_resource {
 struct en50221_stdcam_llci {
 	struct en50221_stdcam stdcam;
 
-	int cafd;
+	struct dvbapi_ca_handle *handle;
 	int slotnum;
 	int state;
 
@@ -77,7 +77,7 @@ struct en50221_stdcam_llci {
 
 static enum en50221_stdcam_status en50221_stdcam_llci_poll(struct en50221_stdcam *stdcam);
 static void en50221_stdcam_llci_dvbtime(struct en50221_stdcam *stdcam, time_t dvbtime);
-static void en50221_stdcam_llci_destroy(struct en50221_stdcam *stdcam, int closefd);
+static void en50221_stdcam_llci_destroy(struct en50221_stdcam *stdcam);
 static void llci_cam_added(struct en50221_stdcam_llci *llci);
 static void llci_cam_in_reset(struct en50221_stdcam_llci *llci);
 static void llci_cam_removed(struct en50221_stdcam_llci *llci);
@@ -94,7 +94,7 @@ static int llci_rm_changed_callback(void
 static int llci_datetime_enquiry_callback(void *arg, uint8_t _slot_id, uint16_t session_number, uint8_t response_interval);
 
 
-struct en50221_stdcam *en50221_stdcam_llci_create(int cafd, int slotnum,
+struct en50221_stdcam *en50221_stdcam_llci_create(struct dvbapi_ca_handle *handle, int slotnum,
 						  struct en50221_transport_layer *tl,
 						  struct en50221_session_layer *sl)
 {
@@ -171,7 +171,7 @@ struct en50221_stdcam *en50221_stdcam_ll
 	llci->stdcam.destroy = en50221_stdcam_llci_destroy;
 	llci->stdcam.poll = en50221_stdcam_llci_poll;
 	llci->stdcam.dvbtime = en50221_stdcam_llci_dvbtime;
-	llci->cafd = cafd;
+	llci->handle = handle;
 	llci->slotnum = slotnum;
 	llci->tl = tl;
 	llci->sl = sl;
@@ -187,7 +187,7 @@ static void en50221_stdcam_llci_dvbtime(
 	llci->datetime_dvbtime = dvbtime;
 }
 
-static void en50221_stdcam_llci_destroy(struct en50221_stdcam *stdcam, int closefd)
+static void en50221_stdcam_llci_destroy(struct en50221_stdcam *stdcam)
 {
 	struct en50221_stdcam_llci *llci = (struct en50221_stdcam_llci *) stdcam;
 
@@ -206,8 +206,7 @@ static void en50221_stdcam_llci_destroy(
 	if (llci->stdcam.mmi_resource)
 		en50221_app_mmi_destroy(llci->stdcam.mmi_resource);
 
-	if (closefd)
-		close(llci->cafd);
+	dvbapi_ca_destroy(llci->handle);
 
 	free(llci);
 }
@@ -219,18 +218,21 @@ static enum en50221_stdcam_status en5022
 {
 	struct en50221_stdcam_llci *llci = (struct en50221_stdcam_llci *) stdcam;
 
-	switch(dvbca_get_cam_state(llci->cafd, llci->slotnum)) {
-	case DVBCA_CAMSTATE_MISSING:
-		if (llci->state != EN50221_STDCAM_CAM_NONE)
-			llci_cam_removed(llci);
-		break;
-
-	case DVBCA_CAMSTATE_READY:
+	switch(dvbapi_cam_state(llci->handle, llci->slotnum)) {
+	case DVBAPI_CAM_READY:
 		if (llci->state == EN50221_STDCAM_CAM_NONE)
 			llci_cam_added(llci);
 		else if (llci->state == EN50221_STDCAM_CAM_INRESET)
 			llci_cam_in_reset(llci);
 		break;
+
+	case DVBAPI_CAM_INITIALISING:
+		break;
+
+	default:
+		if (llci->state != EN50221_STDCAM_CAM_NONE)
+			llci_cam_removed(llci);
+		break;
 	}
 
 	// poll the stack
@@ -261,18 +263,18 @@ static void llci_cam_added(struct en5022
 	}
 
 	// reset the CAM
-	dvbca_reset(llci->cafd, llci->slotnum);
+	dvbapi_cam_reset(llci->handle, llci->slotnum);
 	llci->state = EN50221_STDCAM_CAM_INRESET;
 }
 
 static void llci_cam_in_reset(struct en50221_stdcam_llci *llci)
 {
-	if (dvbca_get_cam_state(llci->cafd, llci->slotnum) != DVBCA_CAMSTATE_READY) {
+	if (dvbapi_cam_state(llci->handle, llci->slotnum) != DVBAPI_CAM_READY) {
 		return;
 	}
 
 	// register the slot
-	if ((llci->tl_slot_id = en50221_tl_register_slot(llci->tl, llci->cafd, llci->slotnum,
+	if ((llci->tl_slot_id = en50221_tl_register_slot(llci->tl, llci->handle, llci->slotnum,
 	      						 LLCI_RESPONSE_TIMEOUT_MS, LLCI_POLL_DELAY_MS)) < 0) {
 		llci->state = EN50221_STDCAM_CAM_BAD;
 		return;
diff -dNpru old/lib/libdvben50221/en50221_transport.c new/lib/libdvben50221/en50221_transport.c
--- old/lib/libdvben50221/en50221_transport.c	2006-07-15 21:21:07.000000000 +0200
+++ new/lib/libdvben50221/en50221_transport.c	2006-07-16 13:53:05.000000000 +0200
@@ -69,7 +69,7 @@ struct en50221_connection {
 };
 
 struct en50221_slot {
-	int ca_hndl;
+	struct dvbapi_ca_handle *handle;
 	uint8_t slot;		// CAM slot
 	struct en50221_connection *connections;
 
@@ -165,7 +165,7 @@ struct en50221_transport_layer *en50221_
 
 	// set them up
 	for (i = 0; i < max_slots; i++) {
-		tl->slots[i].ca_hndl = -1;
+		tl->slots[i].handle = NULL;
 
 		// create the connections for this slot
 		tl->slots[i].connections =
@@ -245,7 +245,7 @@ void en50221_tl_destroy(struct en50221_t
 // this can be called from the user-space app to
 // register new slots that we should work with
 int en50221_tl_register_slot(struct en50221_transport_layer *tl,
-			     int ca_hndl, uint8_t slot,
+			     struct dvbapi_ca_handle *handle, uint8_t slot,
 			     uint32_t response_timeout,
 			     uint32_t poll_delay)
 {
@@ -257,7 +257,7 @@ int en50221_tl_register_slot(struct en50
 	int i;
 	int16_t slot_id = -1;
 	for (i = 0; i < tl->max_slots; i++) {
-		if (tl->slots[i].ca_hndl == -1) {
+		if (tl->slots[i].handle == NULL) {
 			slot_id = i;
 			break;
 		}
@@ -269,7 +269,7 @@ int en50221_tl_register_slot(struct en50
 	}
 	// set up the slot struct
 	pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
-	tl->slots[slot_id].ca_hndl = ca_hndl;
+	tl->slots[slot_id].handle = handle;
 	tl->slots[slot_id].slot = slot;
 	tl->slots[slot_id].response_timeout = response_timeout;
 	tl->slots[slot_id].poll_delay = poll_delay;
@@ -293,7 +293,7 @@ void en50221_tl_destroy_slot(struct en50
 
 	// clear the slot
 	pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
-	tl->slots[slot_id].ca_hndl = -1;
+	tl->slots[slot_id].handle = NULL;
 	for (i = 0; i < tl->max_connections_per_slot; i++) {
 		tl->slots[slot_id].connections[i].state = T_STATE_IDLE;
 		tl->slots[slot_id].connections[i].tx_time.tv_sec = 0;
@@ -332,7 +332,7 @@ void en50221_tl_destroy_slot(struct en50
 
 int en50221_tl_poll(struct en50221_transport_layer *tl)
 {
-	uint8_t data[4096];
+	uint8_t internal_data[4096];
 	int slot_id;
 	int j;
 
@@ -340,8 +340,8 @@ int en50221_tl_poll(struct en50221_trans
 	pthread_mutex_lock(&tl->global_lock);
 	if (tl->slots_changed) {
 		for (slot_id = 0; slot_id < tl->max_slots; slot_id++) {
-			if (tl->slots[slot_id].ca_hndl != -1) {
-				tl->slot_pollfds[slot_id].fd = tl->slots[slot_id].ca_hndl;
+			if (tl->slots[slot_id].handle != NULL) {
+				tl->slot_pollfds[slot_id].fd = dvbapi_ca_fd(tl->slots[slot_id].handle);
 				tl->slot_pollfds[slot_id].events = POLLIN | POLLPRI | POLLERR;
 				tl->slot_pollfds[slot_id].revents = 0;
 			} else {
@@ -365,39 +365,40 @@ int en50221_tl_poll(struct en50221_trans
 
 		// check if this slot is still used and get its handle
 		pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
-		if (tl->slots[slot_id].ca_hndl == -1) {
+		if (tl->slots[slot_id].handle == NULL) {
 			pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
 			continue;
 		}
-		int ca_hndl = tl->slots[slot_id].ca_hndl;
+		struct dvbapi_ca_handle *handle = tl->slots[slot_id].handle;
 
 		if (tl->slot_pollfds[slot_id].revents & (POLLPRI | POLLIN)) {
 			// read data
 			uint8_t r_slot_id;
 			uint8_t connection_id;
-			int readcnt = dvbca_link_read(ca_hndl, &r_slot_id,
-						      &connection_id,
-						      data, sizeof(data));
-			if (readcnt < 0) {
+			int readcnt = dvbapi_llci_read(handle, internal_data, sizeof(internal_data));
+			if (readcnt < 2) {
 				tl->error_slot = slot_id;
 				tl->error = EN50221ERR_CAREAD;
 				pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
 				return -1;
 			}
+			r_slot_id = internal_data[0];
+			connection_id = internal_data[1];
+			readcnt -= 2;
 			// process it if we got some
 			if (readcnt > 0) {
 				if (tl->slots[slot_id].slot != r_slot_id) {
 					// this message is for an other CAM of the same CA
 					int new_slot_id;
 					for (new_slot_id = 0; new_slot_id < tl->max_slots; new_slot_id++) {
-						if ((tl->slots[new_slot_id].ca_hndl == ca_hndl) &&
+						if ((tl->slots[new_slot_id].handle == handle) &&
 						    (tl->slots[new_slot_id].slot == r_slot_id))
 							break;
 					}
 					if (new_slot_id != tl->max_slots) {
 						// we found the requested CAM
 						pthread_mutex_lock(&tl->slots[new_slot_id].slot_lock);
-						if (en50221_tl_process_data(tl, new_slot_id, data, readcnt)) {
+						if (en50221_tl_process_data(tl, new_slot_id, internal_data + 2, readcnt)) {
 							pthread_mutex_unlock(&tl->slots[new_slot_id].slot_lock);
 							pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
 							return -1;
@@ -409,7 +410,7 @@ int en50221_tl_poll(struct en50221_trans
 						return -1;
 					}
 				} else
-				    if (en50221_tl_process_data(tl, slot_id, data, readcnt)) {
+				    if (en50221_tl_process_data(tl, slot_id, internal_data + 2, readcnt)) {
 					pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
 					return -1;
 				}
@@ -445,10 +446,10 @@ int en50221_tl_poll(struct en50221_trans
 					}
 
 					// send the message
-					if (dvbca_link_write(tl->slots[slot_id].ca_hndl,
-					    		     tl->slots[slot_id].slot,
-							     j,
-							     msg->data, msg->length) < 0) {
+					internal_data[0] = tl->slots[slot_id].slot;
+					internal_data[1] = j;
+					memcpy(internal_data + 2, msg->data, msg->length);
+					if (dvbapi_llci_write(tl->slots[slot_id].handle, internal_data, msg->length + 2) < 0) {
 						free(msg);
 						pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
 						tl->error_slot = slot_id;
@@ -546,7 +547,7 @@ int en50221_tl_send_data(struct en50221_
 	}
 
 	pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
-	if (tl->slots[slot_id].ca_hndl == -1) {
+	if (tl->slots[slot_id].handle == NULL) {
 		tl->error = EN50221ERR_BADSLOTID;
 		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
 		return -1;
@@ -617,7 +618,7 @@ int en50221_tl_send_datav(struct en50221
 	}
 
 	pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
-	if (tl->slots[slot_id].ca_hndl == -1) {
+	if (tl->slots[slot_id].handle == NULL) {
 		tl->error = EN50221ERR_BADSLOTID;
 		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
 		return -1;
@@ -687,7 +688,7 @@ int en50221_tl_new_tc(struct en50221_tra
 	}
 
 	pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
-	if (tl->slots[slot_id].ca_hndl == -1) {
+	if (tl->slots[slot_id].handle == NULL) {
 		tl->error = EN50221ERR_BADSLOTID;
 		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
 		return -1;
@@ -734,7 +735,7 @@ int en50221_tl_del_tc(struct en50221_tra
 	}
 
 	pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
-	if (tl->slots[slot_id].ca_hndl == -1) {
+	if (tl->slots[slot_id].handle == NULL) {
 		tl->error = EN50221ERR_BADSLOTID;
 		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
 		return -1;
@@ -786,7 +787,7 @@ int en50221_tl_get_connection_state(stru
 	}
 
 	pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
-	if (tl->slots[slot_id].ca_hndl == -1) {
+	if (tl->slots[slot_id].handle == NULL) {
 		tl->error = EN50221ERR_BADSLOTID;
 		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
 		return -1;
@@ -814,13 +815,13 @@ static int en50221_tl_poll_tc(struct en5
 		     tx_time, 0);
 
 	// send command
-	uint8_t hdr[3];
-	hdr[0] = T_DATA_LAST;
-	hdr[1] = 1;
-	hdr[2] = connection_id;
-	if (dvbca_link_write(tl->slots[slot_id].ca_hndl,
-	    		     tl->slots[slot_id].slot,
-			     connection_id, hdr, 3) < 0) {
+	uint8_t hdr[5];
+	hdr[0] = tl->slots[slot_id].slot;
+	hdr[1] = connection_id;
+	hdr[2] = T_DATA_LAST;
+	hdr[3] = 1;
+	hdr[4] = connection_id;
+	if (dvbapi_llci_write(tl->slots[slot_id].handle, hdr, 5) < 0) {
 		tl->error_slot = slot_id;
 		tl->error = EN50221ERR_CAWRITE;
 		return -1;
@@ -989,13 +990,13 @@ static int en50221_tl_handle_delete_tc(s
 		tl->slots[slot_id].connections[connection_id].buffer_length = 0;
 
 		// send the reply
-		uint8_t hdr[3];
-		hdr[0] = T_D_T_C_REPLY;
-		hdr[1] = 1;
-		hdr[2] = connection_id;
-		if (dvbca_link_write(tl->slots[slot_id].ca_hndl,
-		    		     tl->slots[slot_id].slot,
-				     connection_id, hdr, 3) < 0) {
+		uint8_t hdr[5];
+		hdr[0] = tl->slots[slot_id].slot;
+		hdr[1] = connection_id;
+		hdr[2] = T_D_T_C_REPLY;
+		hdr[3] = 1;
+		hdr[4] = connection_id;
+		if (dvbapi_llci_write(tl->slots[slot_id].handle, hdr, 5) < 0) {
 			tl->error_slot = slot_id;
 			tl->error = EN50221ERR_CAWRITE;
 			return -1;
@@ -1045,19 +1046,21 @@ static int en50221_tl_handle_request_tc(
 {
 	// allocate a new connection if possible
 	int conid = en50221_tl_alloc_new_tc(tl, slot_id);
-	int ca_hndl = tl->slots[slot_id].ca_hndl;
+	struct dvbapi_ca_handle *handle = tl->slots[slot_id].handle;
 	if (conid == -1) {
 		print(LOG_LEVEL, ERROR, 1,
 		      "Too many connections requested by module on slot %02x\n",
 		      slot_id);
 
 		// send the error
-		uint8_t hdr[4];
-		hdr[0] = T_T_C_ERROR;
-		hdr[1] = 2;
-		hdr[2] = connection_id;
-		hdr[3] = 1;
-		if (dvbca_link_write(ca_hndl, tl->slots[slot_id].slot, connection_id, hdr, 4) < 0) {
+		uint8_t hdr[6];
+		hdr[0] = tl->slots[slot_id].slot;
+		hdr[1] = connection_id;
+		hdr[2] = T_T_C_ERROR;
+		hdr[3] = 2;
+		hdr[4] = connection_id;
+		hdr[5] = 1;
+		if (dvbapi_llci_write(handle, hdr, 6) < 0) {
 			tl->error_slot = slot_id;
 			tl->error = EN50221ERR_CAWRITE;
 			return -1;
@@ -1066,12 +1069,14 @@ static int en50221_tl_handle_request_tc(
 		    tv_sec = 0;
 	} else {
 		// send the NEW_T_C on the connection we received it on
-		uint8_t hdr[4];
-		hdr[0] = T_NEW_T_C;
-		hdr[1] = 2;
-		hdr[2] = connection_id;
-		hdr[3] = conid;
-		if (dvbca_link_write(ca_hndl, tl->slots[slot_id].slot, connection_id, hdr, 4) < 0) {
+		uint8_t hdr[6];
+		hdr[0] = tl->slots[slot_id].slot;
+		hdr[1] = connection_id;
+		hdr[2] = T_NEW_T_C;
+		hdr[3] = 2;
+		hdr[4] = connection_id;
+		hdr[5] = conid;
+		if (dvbapi_llci_write(handle, hdr, 6) < 0) {
 			tl->slots[slot_id].connections[conid].state = T_STATE_IDLE;
 			tl->error_slot = slot_id;
 			tl->error = EN50221ERR_CAWRITE;
@@ -1080,10 +1085,12 @@ static int en50221_tl_handle_request_tc(
 		tl->slots[slot_id].connections[connection_id].tx_time.tv_sec = 0;
 
 		// send the CREATE_T_C on the new connnection
-		hdr[0] = T_CREATE_T_C;
-		hdr[1] = 1;
-		hdr[2] = conid;
-		if (dvbca_link_write(ca_hndl, tl->slots[slot_id].slot, conid, hdr, 3) < 0) {
+		hdr[0] = tl->slots[slot_id].slot;
+		hdr[1] = conid;
+		hdr[2] = T_CREATE_T_C;
+		hdr[3] = 1;
+		hdr[4] = conid;
+		if (dvbapi_llci_write(handle, hdr, 5) < 0) {
 			tl->slots[slot_id].connections[conid].state = T_STATE_IDLE;
 			tl->error_slot = slot_id;
 			tl->error = EN50221ERR_CAWRITE;
@@ -1234,14 +1241,16 @@ static int en50221_tl_handle_sb(struct e
 	}
 	// tell it to send the data if it says there is some
 	if (data[0] & 0x80) {
-		int ca_hndl = tl->slots[slot_id].ca_hndl;
+		struct dvbapi_ca_handle *handle = tl->slots[slot_id].handle;
 
 		// send the RCV
-		uint8_t hdr[3];
-		hdr[0] = T_RCV;
-		hdr[1] = 1;
-		hdr[2] = connection_id;
-		if (dvbca_link_write(ca_hndl, tl->slots[slot_id].slot, connection_id, hdr, 3) < 0) {
+		uint8_t hdr[5];
+		hdr[0] = tl->slots[slot_id].slot;
+		hdr[1] = connection_id;
+		hdr[2] = T_RCV;
+		hdr[3] = 1;
+		hdr[4] = connection_id;
+		if (dvbapi_llci_write(handle, hdr, 5) < 0) {
 			tl->error_slot = slot_id;
 			tl->error = EN50221ERR_CAWRITE;
 			return -1;
diff -dNpru old/lib/libdvben50221/en50221_transport.h new/lib/libdvben50221/en50221_transport.h
--- old/lib/libdvben50221/en50221_transport.h	2006-07-15 21:21:07.000000000 +0200
+++ new/lib/libdvben50221/en50221_transport.h	2006-07-16 13:53:05.000000000 +0200
@@ -33,6 +33,8 @@ extern "C" {
 #include <stdint.h>
 #include <sys/uio.h>
 
+#include <libdvbapi/dvbca.h>
+
 /**
  * Callback reasons.
  */
@@ -106,7 +108,7 @@ extern void en50221_tl_destroy(struct en
  * @return slot_id on sucess, or -1 on error.
  */
 extern int en50221_tl_register_slot(struct en50221_transport_layer *tl,
-				    int ca_hndl, uint8_t slot,
+				    struct dvbapi_ca_handle *handle, uint8_t slot,
 				    uint32_t response_timeout,
 				    uint32_t poll_delay);
 
diff -dNpru old/test/libdvben50221/test-app.c new/test/libdvben50221/test-app.c
--- old/test/libdvben50221/test-app.c	2006-07-15 21:21:07.000000000 +0200
+++ new/test/libdvben50221/test-app.c	2006-07-16 13:53:05.000000000 +0200
@@ -193,22 +193,22 @@ int main(int argc, char * argv[])
     }
 
     // find CAMs
-    int cafd;
-    if (((cafd = dvbca_open(adapterid, 0)) < 0) || (dvbca_get_cam_state(cafd, DEFAULT_SLOT) == DVBCA_CAMSTATE_MISSING)) {
+    struct dvbapi_ca_handle *handle;
+    if ((dvbapi_ca_create(&handle, adapterid, 0) != 0) || (dvbapi_cam_state(handle, DEFAULT_SLOT) == DVBAPI_CAM_MISSING)) {
         fprintf(stderr, "Unable to open CAM on adapter %i\n", adapterid);
         exit(1);
     }
 
     // reset it and wait
-    dvbca_reset(cafd, DEFAULT_SLOT);
+    dvbapi_cam_reset(handle, DEFAULT_SLOT);
     printf("Found a CAM on adapter%i... waiting...\n", adapterid);
-    while(dvbca_get_cam_state(cafd, DEFAULT_SLOT) != DVBCA_CAMSTATE_READY) {
+    while(dvbapi_cam_state(handle, DEFAULT_SLOT) != DVBAPI_CAM_READY) {
         usleep(1000);
     }
 
     // register it with the CA stack
     int slot_id = 0;
-    if ((slot_id = en50221_tl_register_slot(tl, cafd, DEFAULT_SLOT, 1000, 100)) < 0) {
+    if ((slot_id = en50221_tl_register_slot(tl, handle, DEFAULT_SLOT, 1000, 100)) < 0) {
         fprintf(stderr, "Slot registration failed\n");
         exit(1);
     }
diff -dNpru old/test/libdvben50221/test-session.c new/test/libdvben50221/test-session.c
--- old/test/libdvben50221/test-session.c	2006-07-15 21:21:07.000000000 +0200
+++ new/test/libdvben50221/test-session.c	2006-07-16 13:53:05.000000000 +0200
@@ -55,24 +55,24 @@ int main(int argc, char * argv[])
 
     // find CAMs
     int slot_count = 0;
-    int cafd= -1;
+    struct dvbapi_ca_handle *handle;
     for(i=0; i<20; i++) {
-        if ((cafd = dvbca_open(i, 0)) > 0) {
-            if (dvbca_get_cam_state(cafd, DEFAULT_SLOT) == DVBCA_CAMSTATE_MISSING) {
-                close(cafd);
+        if (dvbapi_ca_create(&handle, i, 0) == 0) {
+            if (dvbapi_cam_state(handle, DEFAULT_SLOT) == DVBAPI_CAM_MISSING) {
+                dvbapi_ca_destroy(handle);
                 continue;
             }
 
             // reset it and wait
-            dvbca_reset(cafd, DEFAULT_SLOT);
+            dvbapi_cam_reset(handle, DEFAULT_SLOT);
             printf("Found a CAM on adapter%i... waiting...\n", i);
-            while(dvbca_get_cam_state(cafd, DEFAULT_SLOT) != DVBCA_CAMSTATE_READY) {
+            while(dvbapi_cam_state(handle, DEFAULT_SLOT) != DVBAPI_CAM_READY) {
                 usleep(1000);
             }
 
             // register it with the CA stack
             int slot_id = 0;
-            if ((slot_id = en50221_tl_register_slot(tl, cafd, DEFAULT_SLOT, 1000, 100)) < 0) {
+            if ((slot_id = en50221_tl_register_slot(tl, handle, DEFAULT_SLOT, 1000, 100)) < 0) {
                 fprintf(stderr, "Slot registration failed\n");
                 exit(1);
             }
diff -dNpru old/test/libdvben50221/test-transport.c new/test/libdvben50221/test-transport.c
--- old/test/libdvben50221/test-transport.c	2006-07-15 21:21:07.000000000 +0200
+++ new/test/libdvben50221/test-transport.c	2006-07-16 13:53:05.000000000 +0200
@@ -53,24 +53,24 @@ int main(int argc, char * argv[])
 
     // find CAMs
     int slot_count = 0;
-    int cafd= -1;
+    struct dvbapi_ca_handle *handle;
     for(i=0; i<20; i++) {
-        if ((cafd = dvbca_open(i, 0)) > 0) {
-            if (dvbca_get_cam_state(cafd, DEFAULT_SLOT) == DVBCA_CAMSTATE_MISSING) {
-                close(cafd);
+        if (dvbapi_ca_create(&handle, i, 0) == 0) {
+            if (dvbapi_cam_state(handle, DEFAULT_SLOT) == DVBAPI_CAM_MISSING) {
+                dvbapi_ca_destroy(handle);
                 continue;
             }
 
             // reset it and wait
-            dvbca_reset(cafd, DEFAULT_SLOT);
+            dvbapi_cam_reset(handle, DEFAULT_SLOT);
             printf("Found a CAM on adapter%i... waiting...\n", i);
-            while(dvbca_get_cam_state(cafd, DEFAULT_SLOT) != DVBCA_CAMSTATE_READY) {
+            while(dvbapi_cam_state(handle, DEFAULT_SLOT) != DVBAPI_CAM_READY) {
                 usleep(1000);
             }
 
             // register it with the CA stack
             int slot_id = 0;
-            if ((slot_id = en50221_tl_register_slot(tl, cafd, DEFAULT_SLOT, 1000, 100)) < 0) {
+            if ((slot_id = en50221_tl_register_slot(tl, handle, DEFAULT_SLOT, 1000, 100)) < 0) {
                 fprintf(stderr, "Slot registration failed\n");
                 exit(1);
             }
diff -dNpru old/util/gnutv/gnutv_ca.c new/util/gnutv/gnutv_ca.c
--- old/util/gnutv/gnutv_ca.c	2006-07-16 13:51:53.000000000 +0200
+++ new/util/gnutv/gnutv_ca.c	2006-07-16 13:53:46.000000000 +0200
@@ -144,7 +144,7 @@ void gnutv_ca_stop(void)
 
 	// destroy the stdcam
 	if (stdcam->destroy)
-		stdcam->destroy(stdcam, 1);
+		stdcam->destroy(stdcam);
 
 	// destroy session layer
 	en50221_sl_destroy(sl);
diff -dNpru old/util/zap/zap_ca.c new/util/zap/zap_ca.c
--- old/util/zap/zap_ca.c	2006-07-16 13:51:53.000000000 +0200
+++ new/util/zap/zap_ca.c	2006-07-16 13:53:05.000000000 +0200
@@ -105,7 +105,7 @@ void zap_ca_stop(void)
 
 	// destroy the stdcam
 	if (stdcam->destroy)
-		stdcam->destroy(stdcam, 1);
+		stdcam->destroy(stdcam);
 }
 
 int zap_ca_new_pmt(struct mpeg_pmt_section *pmt)