2
* libdvbca - interface onto raw CA devices
4
* Copyright (C) 2006 Andrew de Quincey (adq_dvb@lidskialf.net)
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24
#include <sys/param.h>
25
#include <sys/ioctl.h>
30
#include <linux/dvb/ca.h>
34
int dvbca_open(int adapter, int cadevice)
36
char filename[PATH_MAX+1];
39
sprintf(filename, "/dev/dvb/adapter%i/ca%i", adapter, cadevice);
40
if ((fd = open(filename, O_RDWR)) < 0) {
41
// if that failed, try a flat /dev structure
42
sprintf(filename, "/dev/dvb%i.ca%i", adapter, cadevice);
43
fd = open(filename, O_RDWR);
49
int dvbca_reset(int fd, uint8_t slot)
51
return ioctl(fd, CA_RESET, (1 << slot));
54
int dvbca_get_interface_type(int fd, uint8_t slot)
59
if (ioctl(fd, CA_GET_SLOT_INFO, &info))
62
if (info.type & CA_CI_LINK)
63
return DVBCA_INTERFACE_LINK;
64
if (info.type & CA_CI)
65
return DVBCA_INTERFACE_HLCI;
70
int dvbca_get_cam_state(int fd, uint8_t slot)
75
if (ioctl(fd, CA_GET_SLOT_INFO, &info))
79
return DVBCA_CAMSTATE_MISSING;
80
if (info.flags & CA_CI_MODULE_READY)
81
return DVBCA_CAMSTATE_READY;
82
if (info.flags & CA_CI_MODULE_PRESENT)
83
return DVBCA_CAMSTATE_INITIALISING;
88
int dvbca_link_write(int fd, uint8_t slot, uint8_t connection_id,
89
uint8_t *data, uint16_t data_length)
91
uint8_t *buf = malloc(data_length + 2);
96
buf[1] = connection_id;
97
memcpy(buf+2, data, data_length);
99
int result = write(fd, buf, data_length+2);
104
int dvbca_link_read(int fd, uint8_t *slot, uint8_t *connection_id,
105
uint8_t *data, uint16_t data_length)
109
uint8_t *buf = malloc(data_length + 2);
113
if ((size = read(fd, buf, data_length+2)) < 2)
117
*connection_id = buf[1];
118
memcpy(data, buf+2, size-2);
124
int dvbca_hlci_write(int fd, uint8_t *data, uint16_t data_length)
128
if (data_length > 256) {
131
memset(&msg, 0, sizeof(msg));
132
msg.length = data_length;
134
memcpy(msg.msg, data, data_length);
136
return ioctl(fd, CA_SEND_MSG, &msg);
139
int dvbca_hlci_read(int fd, uint32_t app_tag, uint8_t *data,
140
uint16_t data_length)
144
if (data_length > 256) {
147
memset(&msg, 0, sizeof(msg));
148
msg.length = data_length;
149
msg.msg[0] = app_tag >> 16;
150
msg.msg[1] = app_tag >> 8;
151
msg.msg[2] = app_tag;
153
int status = ioctl(fd, CA_GET_MSG, &msg);
154
if (status < 0) return status;
156
if (msg.length > data_length) msg.length = data_length;
157
memcpy(data, msg.msg, msg.length);