firesat-ci.c 5.6 KB
Newer Older
1
/*
2
 * FireDTV driver (formerly known as FireSAT)
3
 *
4 5
 * Copyright (C) 2004 Andreas Monitzer <andy@monitzer.com>
 * Copyright (C) 2008 Henrik Kurelid <henrik@kurelid.se>
6 7 8 9 10 11 12
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License as
 *	published by the Free Software Foundation; either version 2 of
 *	the License, or (at your option) any later version.
 */

G
Greg Kroah-Hartman 已提交
13
#include <linux/dvb/ca.h>
14 15 16
#include <linux/fs.h>
#include <linux/module.h>

G
Greg Kroah-Hartman 已提交
17 18
#include <dvbdev.h>

19 20 21 22
#include "avc_api.h"
#include "firesat.h"
#include "firesat-ci.h"

23 24 25
static int firesat_ca_ready(ANTENNA_INPUT_INFO *info)
{
	return info->CaInitializationStatus == 1 &&
26 27 28
	       info->CaErrorFlag == 0 &&
	       info->CaDvbFlag == 1 &&
	       info->CaModulePresentStatus == 1;
29 30 31 32 33
}

static int firesat_get_ca_flags(ANTENNA_INPUT_INFO *info)
{
	int flags = 0;
34

35 36 37 38 39 40 41 42 43 44 45
	if (info->CaModulePresentStatus == 1)
		flags |= CA_CI_MODULE_PRESENT;
	if (info->CaInitializationStatus == 1 &&
	    info->CaErrorFlag == 0 &&
	    info->CaDvbFlag == 1)
		flags |= CA_CI_MODULE_READY;
	return flags;
}

static int firesat_ca_reset(struct firesat *firesat)
{
46
	return avc_ca_reset(firesat) ? -EFAULT : 0;
47 48
}

49
static int firesat_ca_get_caps(void *arg)
50
{
51 52 53 54 55 56 57
	struct ca_caps *cap = arg;

	cap->slot_num = 1;
	cap->slot_type = CA_CI;
	cap->descr_num = 1;
	cap->descr_type = CA_ECD;
	return 0;
58 59 60 61 62
}

static int firesat_ca_get_slot_info(struct firesat *firesat, void *arg)
{
	ANTENNA_INPUT_INFO info;
63
	struct ca_slot_info *slot = arg;
64

65
	if (avc_tuner_status(firesat, &info))
66 67
		return -EFAULT;

68
	if (slot->num != 0)
69
		return -EFAULT;
70 71 72

	slot->type = CA_CI;
	slot->flags = firesat_get_ca_flags(&info);
73 74 75 76 77
	return 0;
}

static int firesat_ca_app_info(struct firesat *firesat, void *arg)
{
78
	struct ca_msg *reply = arg;
79

80 81
	return
	    avc_ca_app_info(firesat, reply->msg, &reply->length) ? -EFAULT : 0;
82 83 84 85
}

static int firesat_ca_info(struct firesat *firesat, void *arg)
{
86
	struct ca_msg *reply = arg;
87

88
	return avc_ca_info(firesat, reply->msg, &reply->length) ? -EFAULT : 0;
89 90 91 92
}

static int firesat_ca_get_mmi(struct firesat *firesat, void *arg)
{
93
	struct ca_msg *reply = arg;
94

95 96
	return
	    avc_ca_get_mmi(firesat, reply->msg, &reply->length) ? -EFAULT : 0;
97 98 99 100 101
}

static int firesat_ca_get_msg(struct firesat *firesat, void *arg)
{
	ANTENNA_INPUT_INFO info;
102
	int err;
103 104 105 106 107 108 109

	switch (firesat->ca_last_command) {
	case TAG_APP_INFO_ENQUIRY:
		err = firesat_ca_app_info(firesat, arg);
		break;
	case TAG_CA_INFO_ENQUIRY:
		err = firesat_ca_info(firesat, arg);
G
Greg Kroah-Hartman 已提交
110
		break;
111
	default:
112
		if (avc_tuner_status(firesat, &info))
113
			err = -EFAULT;
114
		else if (info.CaMmi == 1)
115 116 117 118 119 120 121 122 123 124
			err = firesat_ca_get_mmi(firesat, arg);
		else {
			printk(KERN_INFO "%s: Unhandled message 0x%08X\n",
			       __func__, firesat->ca_last_command);
			err = -EFAULT;
		}
	}
	firesat->ca_last_command = 0;
	return err;
}
G
Greg Kroah-Hartman 已提交
125

126 127
static int firesat_ca_pmt(struct firesat *firesat, void *arg)
{
128
	struct ca_msg *msg = arg;
129 130
	int data_pos;

131 132
	if (msg->msg[3] & 0x80)
		data_pos = (msg->msg[4] && 0x7F) + 4;
133 134
	else
		data_pos = 4;
135 136 137

	return avc_ca_pmt(firesat, &msg->msg[data_pos],
			  msg->length - data_pos) ? -EFAULT : 0;
138 139 140 141
}

static int firesat_ca_send_msg(struct firesat *firesat, void *arg)
{
142
	struct ca_msg *msg = arg;
143 144
	int err;

145
	/* Do we need a semaphore for this? */
146
	firesat->ca_last_command =
147
		(msg->msg[0] << 16) + (msg->msg[1] << 8) + msg->msg[2];
148 149 150 151 152
	switch (firesat->ca_last_command) {
	case TAG_CA_PMT:
		err = firesat_ca_pmt(firesat, arg);
		break;
	case TAG_APP_INFO_ENQUIRY:
153
		/* handled in ca_get_msg */
G
Greg Kroah-Hartman 已提交
154 155
		err = 0;
		break;
156
	case TAG_CA_INFO_ENQUIRY:
157
		/* handled in ca_get_msg */
G
Greg Kroah-Hartman 已提交
158 159
		err = 0;
		break;
160 161 162 163 164 165 166
	case TAG_ENTER_MENU:
		err = avc_ca_enter_menu(firesat);
		break;
	default:
		printk(KERN_ERR "%s: Unhandled unknown message 0x%08X\n",
		       __func__, firesat->ca_last_command);
		err = -EFAULT;
G
Greg Kroah-Hartman 已提交
167
	}
168 169 170 171 172 173
	return err;
}

static int firesat_ca_ioctl(struct inode *inode, struct file *file,
			    unsigned int cmd, void *arg)
{
174
	struct dvb_device *dvbdev = file->private_data;
175 176
	struct firesat *firesat = dvbdev->priv;
	ANTENNA_INPUT_INFO info;
177
	int err;
178 179 180 181 182 183

	switch(cmd) {
	case CA_RESET:
		err = firesat_ca_reset(firesat);
		break;
	case CA_GET_CAP:
184
		err = firesat_ca_get_caps(arg);
185 186 187 188 189 190 191 192 193 194
		break;
	case CA_GET_SLOT_INFO:
		err = firesat_ca_get_slot_info(firesat, arg);
		break;
	case CA_GET_MSG:
		err = firesat_ca_get_msg(firesat, arg);
		break;
	case CA_SEND_MSG:
		err = firesat_ca_send_msg(firesat, arg);
		break;
G
Greg Kroah-Hartman 已提交
195
	default:
196 197 198
		printk(KERN_INFO "%s: Unhandled ioctl, command: %u\n",__func__,
		       cmd);
		err = -EOPNOTSUPP;
G
Greg Kroah-Hartman 已提交
199
	}
200

201 202
	/* FIXME Is this necessary? */
	avc_tuner_status(firesat, &info);
203

G
Greg Kroah-Hartman 已提交
204 205 206
	return err;
}

207 208
static unsigned int firesat_ca_io_poll(struct file *file, poll_table *wait)
{
G
Greg Kroah-Hartman 已提交
209 210 211 212
	return POLLIN;
}

static struct file_operations firesat_ca_fops = {
213 214 215 216 217
	.owner		= THIS_MODULE,
	.ioctl		= dvb_generic_ioctl,
	.open		= dvb_generic_open,
	.release	= dvb_generic_release,
	.poll		= firesat_ca_io_poll,
G
Greg Kroah-Hartman 已提交
218 219 220
};

static struct dvb_device firesat_ca = {
221 222 223 224 225
	.users		= 1,
	.readers	= 1,
	.writers	= 1,
	.fops		= &firesat_ca_fops,
	.kernel_ioctl	= firesat_ca_ioctl,
G
Greg Kroah-Hartman 已提交
226 227
};

228
int firesat_ca_register(struct firesat *firesat)
229 230
{
	ANTENNA_INPUT_INFO info;
231
	int err;
G
Greg Kroah-Hartman 已提交
232

233
	if (avc_tuner_status(firesat, &info))
234 235
		return -EINVAL;

236 237 238 239 240 241 242 243 244 245 246
	if (!firesat_ca_ready(&info))
		return -EFAULT;

	err = dvb_register_device(&firesat->adapter, &firesat->cadev,
				  &firesat_ca, firesat, DVB_DEVICE_CA);

	if (info.CaApplicationInfo == 0)
		printk(KERN_ERR "%s: CaApplicationInfo is not set.\n",
		       __func__);
	if (info.CaDateTimeRequest == 1)
		avc_ca_get_time_date(firesat, &firesat->ca_time_interval);
247 248

	return err;
G
Greg Kroah-Hartman 已提交
249 250
}

251 252 253
void firesat_ca_release(struct firesat *firesat)
{
	if (firesat->cadev)
254
		dvb_unregister_device(firesat->cadev);
G
Greg Kroah-Hartman 已提交
255
}