firedtv: cleanups and minor fixes
[linux-2.6-block.git] / drivers / media / dvb / firesat / firesat-ci.c
CommitLineData
df4846c3 1/*
612262a5 2 * FireDTV driver (formerly known as FireSAT)
df4846c3 3 *
612262a5
SR
4 * Copyright (C) 2004 Andreas Monitzer <andy@monitzer.com>
5 * Copyright (C) 2008 Henrik Kurelid <henrik@kurelid.se>
df4846c3
HK
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 */
12
c81c8b68 13#include <linux/dvb/ca.h>
612262a5
SR
14#include <linux/fs.h>
15#include <linux/module.h>
16
c81c8b68 17#include <dvbdev.h>
c81c8b68 18
612262a5
SR
19#include "avc_api.h"
20#include "firesat.h"
21#include "firesat-ci.h"
22
df4846c3
HK
23static int firesat_ca_ready(ANTENNA_INPUT_INFO *info)
24{
df4846c3 25 return info->CaInitializationStatus == 1 &&
8ae83cdf
SR
26 info->CaErrorFlag == 0 &&
27 info->CaDvbFlag == 1 &&
28 info->CaModulePresentStatus == 1;
df4846c3
HK
29}
30
31static int firesat_get_ca_flags(ANTENNA_INPUT_INFO *info)
32{
33 int flags = 0;
8ae83cdf 34
df4846c3
HK
35 if (info->CaModulePresentStatus == 1)
36 flags |= CA_CI_MODULE_PRESENT;
37 if (info->CaInitializationStatus == 1 &&
38 info->CaErrorFlag == 0 &&
39 info->CaDvbFlag == 1)
40 flags |= CA_CI_MODULE_READY;
41 return flags;
42}
43
44static int firesat_ca_reset(struct firesat *firesat)
45{
8ae83cdf 46 return avc_ca_reset(firesat) ? -EFAULT : 0;
df4846c3
HK
47}
48
8ae83cdf 49static int firesat_ca_get_caps(void *arg)
df4846c3 50{
8ae83cdf
SR
51 struct ca_caps *cap = arg;
52
53 cap->slot_num = 1;
54 cap->slot_type = CA_CI;
55 cap->descr_num = 1;
56 cap->descr_type = CA_ECD;
57 return 0;
df4846c3
HK
58}
59
60static int firesat_ca_get_slot_info(struct firesat *firesat, void *arg)
61{
62 ANTENNA_INPUT_INFO info;
8ae83cdf 63 struct ca_slot_info *slot = arg;
df4846c3 64
8ae83cdf 65 if (avc_tuner_status(firesat, &info))
df4846c3
HK
66 return -EFAULT;
67
8ae83cdf 68 if (slot->num != 0)
df4846c3 69 return -EFAULT;
8ae83cdf
SR
70
71 slot->type = CA_CI;
72 slot->flags = firesat_get_ca_flags(&info);
df4846c3
HK
73 return 0;
74}
75
76static int firesat_ca_app_info(struct firesat *firesat, void *arg)
77{
8ae83cdf 78 struct ca_msg *reply = arg;
df4846c3 79
8ae83cdf
SR
80 return
81 avc_ca_app_info(firesat, reply->msg, &reply->length) ? -EFAULT : 0;
df4846c3
HK
82}
83
84static int firesat_ca_info(struct firesat *firesat, void *arg)
85{
8ae83cdf 86 struct ca_msg *reply = arg;
df4846c3 87
8ae83cdf 88 return avc_ca_info(firesat, reply->msg, &reply->length) ? -EFAULT : 0;
df4846c3
HK
89}
90
91static int firesat_ca_get_mmi(struct firesat *firesat, void *arg)
92{
8ae83cdf 93 struct ca_msg *reply = arg;
df4846c3 94
8ae83cdf
SR
95 return
96 avc_ca_get_mmi(firesat, reply->msg, &reply->length) ? -EFAULT : 0;
df4846c3
HK
97}
98
99static int firesat_ca_get_msg(struct firesat *firesat, void *arg)
100{
df4846c3 101 ANTENNA_INPUT_INFO info;
8ae83cdf 102 int err;
df4846c3
HK
103
104 switch (firesat->ca_last_command) {
105 case TAG_APP_INFO_ENQUIRY:
106 err = firesat_ca_app_info(firesat, arg);
107 break;
108 case TAG_CA_INFO_ENQUIRY:
109 err = firesat_ca_info(firesat, arg);
c81c8b68 110 break;
df4846c3 111 default:
8ae83cdf 112 if (avc_tuner_status(firesat, &info))
df4846c3 113 err = -EFAULT;
8ae83cdf 114 else if (info.CaMmi == 1)
df4846c3 115 err = firesat_ca_get_mmi(firesat, arg);
df4846c3
HK
116 else {
117 printk(KERN_INFO "%s: Unhandled message 0x%08X\n",
118 __func__, firesat->ca_last_command);
119 err = -EFAULT;
120 }
121 }
122 firesat->ca_last_command = 0;
123 return err;
124}
c81c8b68 125
df4846c3
HK
126static int firesat_ca_pmt(struct firesat *firesat, void *arg)
127{
8ae83cdf 128 struct ca_msg *msg = arg;
df4846c3
HK
129 int data_pos;
130
8ae83cdf
SR
131 if (msg->msg[3] & 0x80)
132 data_pos = (msg->msg[4] && 0x7F) + 4;
df4846c3
HK
133 else
134 data_pos = 4;
8ae83cdf
SR
135
136 return avc_ca_pmt(firesat, &msg->msg[data_pos],
137 msg->length - data_pos) ? -EFAULT : 0;
df4846c3
HK
138}
139
140static int firesat_ca_send_msg(struct firesat *firesat, void *arg)
141{
8ae83cdf 142 struct ca_msg *msg = arg;
df4846c3 143 int err;
df4846c3 144
8ae83cdf 145 /* Do we need a semaphore for this? */
df4846c3 146 firesat->ca_last_command =
8ae83cdf 147 (msg->msg[0] << 16) + (msg->msg[1] << 8) + msg->msg[2];
df4846c3
HK
148 switch (firesat->ca_last_command) {
149 case TAG_CA_PMT:
df4846c3
HK
150 err = firesat_ca_pmt(firesat, arg);
151 break;
152 case TAG_APP_INFO_ENQUIRY:
8ae83cdf 153 /* handled in ca_get_msg */
c81c8b68
GKH
154 err = 0;
155 break;
df4846c3 156 case TAG_CA_INFO_ENQUIRY:
8ae83cdf 157 /* handled in ca_get_msg */
c81c8b68
GKH
158 err = 0;
159 break;
df4846c3 160 case TAG_ENTER_MENU:
df4846c3
HK
161 err = avc_ca_enter_menu(firesat);
162 break;
163 default:
164 printk(KERN_ERR "%s: Unhandled unknown message 0x%08X\n",
165 __func__, firesat->ca_last_command);
166 err = -EFAULT;
c81c8b68 167 }
df4846c3
HK
168 return err;
169}
170
171static int firesat_ca_ioctl(struct inode *inode, struct file *file,
172 unsigned int cmd, void *arg)
173{
8ae83cdf 174 struct dvb_device *dvbdev = file->private_data;
df4846c3 175 struct firesat *firesat = dvbdev->priv;
df4846c3 176 ANTENNA_INPUT_INFO info;
8ae83cdf 177 int err;
df4846c3
HK
178
179 switch(cmd) {
180 case CA_RESET:
181 err = firesat_ca_reset(firesat);
182 break;
183 case CA_GET_CAP:
8ae83cdf 184 err = firesat_ca_get_caps(arg);
df4846c3
HK
185 break;
186 case CA_GET_SLOT_INFO:
187 err = firesat_ca_get_slot_info(firesat, arg);
188 break;
189 case CA_GET_MSG:
190 err = firesat_ca_get_msg(firesat, arg);
191 break;
192 case CA_SEND_MSG:
193 err = firesat_ca_send_msg(firesat, arg);
194 break;
c81c8b68 195 default:
df4846c3
HK
196 printk(KERN_INFO "%s: Unhandled ioctl, command: %u\n",__func__,
197 cmd);
198 err = -EOPNOTSUPP;
c81c8b68 199 }
df4846c3 200
8ae83cdf
SR
201 /* FIXME Is this necessary? */
202 avc_tuner_status(firesat, &info);
df4846c3 203
c81c8b68
GKH
204 return err;
205}
c81c8b68 206
df4846c3
HK
207static unsigned int firesat_ca_io_poll(struct file *file, poll_table *wait)
208{
c81c8b68
GKH
209 return POLLIN;
210}
211
212static struct file_operations firesat_ca_fops = {
8ae83cdf
SR
213 .owner = THIS_MODULE,
214 .ioctl = dvb_generic_ioctl,
215 .open = dvb_generic_open,
216 .release = dvb_generic_release,
217 .poll = firesat_ca_io_poll,
c81c8b68
GKH
218};
219
220static struct dvb_device firesat_ca = {
8ae83cdf
SR
221 .users = 1,
222 .readers = 1,
223 .writers = 1,
224 .fops = &firesat_ca_fops,
225 .kernel_ioctl = firesat_ca_ioctl,
c81c8b68
GKH
226};
227
8ae83cdf 228int firesat_ca_register(struct firesat *firesat)
df4846c3 229{
df4846c3 230 ANTENNA_INPUT_INFO info;
8ae83cdf 231 int err;
c81c8b68 232
8ae83cdf 233 if (avc_tuner_status(firesat, &info))
df4846c3
HK
234 return -EINVAL;
235
8ae83cdf
SR
236 if (!firesat_ca_ready(&info))
237 return -EFAULT;
238
239 err = dvb_register_device(&firesat->adapter, &firesat->cadev,
240 &firesat_ca, firesat, DVB_DEVICE_CA);
241
242 if (info.CaApplicationInfo == 0)
243 printk(KERN_ERR "%s: CaApplicationInfo is not set.\n",
244 __func__);
245 if (info.CaDateTimeRequest == 1)
246 avc_ca_get_time_date(firesat, &firesat->ca_time_interval);
df4846c3
HK
247
248 return err;
c81c8b68
GKH
249}
250
df4846c3
HK
251void firesat_ca_release(struct firesat *firesat)
252{
253 if (firesat->cadev)
8ae83cdf 254 dvb_unregister_device(firesat->cadev);
c81c8b68 255}