dvb: Push down BKL into ioctl functions
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / dvb / firewire / firedtv-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
15490795 13#include <linux/device.h>
c81c8b68 14#include <linux/dvb/ca.h>
612262a5
SR
15#include <linux/fs.h>
16#include <linux/module.h>
17
c81c8b68 18#include <dvbdev.h>
c81c8b68 19
a70f81c1 20#include "firedtv.h"
612262a5 21
15490795
SR
22#define EN50221_TAG_APP_INFO_ENQUIRY 0x9f8020
23#define EN50221_TAG_CA_INFO_ENQUIRY 0x9f8030
24#define EN50221_TAG_CA_PMT 0x9f8032
25#define EN50221_TAG_ENTER_MENU 0x9f8022
26
27static int fdtv_ca_ready(struct firedtv_tuner_status *stat)
df4846c3 28{
15490795
SR
29 return stat->ca_initialization_status == 1 &&
30 stat->ca_error_flag == 0 &&
31 stat->ca_dvb_flag == 1 &&
32 stat->ca_module_present_status == 1;
df4846c3
HK
33}
34
15490795 35static int fdtv_get_ca_flags(struct firedtv_tuner_status *stat)
df4846c3
HK
36{
37 int flags = 0;
8ae83cdf 38
15490795 39 if (stat->ca_module_present_status == 1)
df4846c3 40 flags |= CA_CI_MODULE_PRESENT;
15490795
SR
41 if (stat->ca_initialization_status == 1 &&
42 stat->ca_error_flag == 0 &&
43 stat->ca_dvb_flag == 1)
df4846c3
HK
44 flags |= CA_CI_MODULE_READY;
45 return flags;
46}
47
a70f81c1 48static int fdtv_ca_reset(struct firedtv *fdtv)
df4846c3 49{
a70f81c1 50 return avc_ca_reset(fdtv) ? -EFAULT : 0;
df4846c3
HK
51}
52
a70f81c1 53static int fdtv_ca_get_caps(void *arg)
df4846c3 54{
8ae83cdf
SR
55 struct ca_caps *cap = arg;
56
57 cap->slot_num = 1;
58 cap->slot_type = CA_CI;
59 cap->descr_num = 1;
60 cap->descr_type = CA_ECD;
61 return 0;
df4846c3
HK
62}
63
a70f81c1 64static int fdtv_ca_get_slot_info(struct firedtv *fdtv, void *arg)
df4846c3 65{
15490795 66 struct firedtv_tuner_status stat;
8ae83cdf 67 struct ca_slot_info *slot = arg;
df4846c3 68
15490795 69 if (avc_tuner_status(fdtv, &stat))
df4846c3
HK
70 return -EFAULT;
71
8ae83cdf 72 if (slot->num != 0)
df4846c3 73 return -EFAULT;
8ae83cdf
SR
74
75 slot->type = CA_CI;
15490795 76 slot->flags = fdtv_get_ca_flags(&stat);
df4846c3
HK
77 return 0;
78}
79
a70f81c1 80static int fdtv_ca_app_info(struct firedtv *fdtv, void *arg)
df4846c3 81{
8ae83cdf 82 struct ca_msg *reply = arg;
df4846c3 83
15490795 84 return avc_ca_app_info(fdtv, reply->msg, &reply->length) ? -EFAULT : 0;
df4846c3
HK
85}
86
a70f81c1 87static int fdtv_ca_info(struct firedtv *fdtv, void *arg)
df4846c3 88{
8ae83cdf 89 struct ca_msg *reply = arg;
df4846c3 90
a70f81c1 91 return avc_ca_info(fdtv, reply->msg, &reply->length) ? -EFAULT : 0;
df4846c3
HK
92}
93
a70f81c1 94static int fdtv_ca_get_mmi(struct firedtv *fdtv, void *arg)
df4846c3 95{
8ae83cdf 96 struct ca_msg *reply = arg;
df4846c3 97
15490795 98 return avc_ca_get_mmi(fdtv, reply->msg, &reply->length) ? -EFAULT : 0;
df4846c3
HK
99}
100
a70f81c1 101static int fdtv_ca_get_msg(struct firedtv *fdtv, void *arg)
df4846c3 102{
15490795 103 struct firedtv_tuner_status stat;
8ae83cdf 104 int err;
df4846c3 105
a70f81c1 106 switch (fdtv->ca_last_command) {
15490795 107 case EN50221_TAG_APP_INFO_ENQUIRY:
a70f81c1 108 err = fdtv_ca_app_info(fdtv, arg);
df4846c3 109 break;
15490795 110 case EN50221_TAG_CA_INFO_ENQUIRY:
a70f81c1 111 err = fdtv_ca_info(fdtv, arg);
c81c8b68 112 break;
df4846c3 113 default:
15490795 114 if (avc_tuner_status(fdtv, &stat))
df4846c3 115 err = -EFAULT;
15490795 116 else if (stat.ca_mmi == 1)
a70f81c1 117 err = fdtv_ca_get_mmi(fdtv, arg);
df4846c3 118 else {
15490795
SR
119 dev_info(fdtv->device, "unhandled CA message 0x%08x\n",
120 fdtv->ca_last_command);
df4846c3
HK
121 err = -EFAULT;
122 }
123 }
a70f81c1 124 fdtv->ca_last_command = 0;
df4846c3
HK
125 return err;
126}
c81c8b68 127
a70f81c1 128static int fdtv_ca_pmt(struct firedtv *fdtv, void *arg)
df4846c3 129{
8ae83cdf 130 struct ca_msg *msg = arg;
df4846c3 131 int data_pos;
7199e523
HK
132 int data_length;
133 int i;
134
135 data_pos = 4;
136 if (msg->msg[3] & 0x80) {
137 data_length = 0;
15490795 138 for (i = 0; i < (msg->msg[3] & 0x7f); i++)
7199e523
HK
139 data_length = (data_length << 8) + msg->msg[data_pos++];
140 } else {
141 data_length = msg->msg[3];
142 }
df4846c3 143
15490795 144 return avc_ca_pmt(fdtv, &msg->msg[data_pos], data_length) ? -EFAULT : 0;
df4846c3
HK
145}
146
a70f81c1 147static int fdtv_ca_send_msg(struct firedtv *fdtv, void *arg)
df4846c3 148{
8ae83cdf 149 struct ca_msg *msg = arg;
df4846c3 150 int err;
df4846c3 151
8ae83cdf 152 /* Do we need a semaphore for this? */
a70f81c1 153 fdtv->ca_last_command =
8ae83cdf 154 (msg->msg[0] << 16) + (msg->msg[1] << 8) + msg->msg[2];
a70f81c1 155 switch (fdtv->ca_last_command) {
15490795 156 case EN50221_TAG_CA_PMT:
a70f81c1 157 err = fdtv_ca_pmt(fdtv, arg);
df4846c3 158 break;
15490795 159 case EN50221_TAG_APP_INFO_ENQUIRY:
8ae83cdf 160 /* handled in ca_get_msg */
c81c8b68
GKH
161 err = 0;
162 break;
15490795 163 case EN50221_TAG_CA_INFO_ENQUIRY:
8ae83cdf 164 /* handled in ca_get_msg */
c81c8b68
GKH
165 err = 0;
166 break;
15490795 167 case EN50221_TAG_ENTER_MENU:
a70f81c1 168 err = avc_ca_enter_menu(fdtv);
df4846c3
HK
169 break;
170 default:
15490795
SR
171 dev_err(fdtv->device, "unhandled CA message 0x%08x\n",
172 fdtv->ca_last_command);
df4846c3 173 err = -EFAULT;
c81c8b68 174 }
df4846c3
HK
175 return err;
176}
177
16ef8def 178static int fdtv_ca_ioctl(struct file *file, unsigned int cmd, void *arg)
df4846c3 179{
8ae83cdf 180 struct dvb_device *dvbdev = file->private_data;
a70f81c1 181 struct firedtv *fdtv = dvbdev->priv;
15490795 182 struct firedtv_tuner_status stat;
8ae83cdf 183 int err;
df4846c3 184
15490795 185 switch (cmd) {
df4846c3 186 case CA_RESET:
a70f81c1 187 err = fdtv_ca_reset(fdtv);
df4846c3
HK
188 break;
189 case CA_GET_CAP:
a70f81c1 190 err = fdtv_ca_get_caps(arg);
df4846c3
HK
191 break;
192 case CA_GET_SLOT_INFO:
a70f81c1 193 err = fdtv_ca_get_slot_info(fdtv, arg);
df4846c3
HK
194 break;
195 case CA_GET_MSG:
a70f81c1 196 err = fdtv_ca_get_msg(fdtv, arg);
df4846c3
HK
197 break;
198 case CA_SEND_MSG:
a70f81c1 199 err = fdtv_ca_send_msg(fdtv, arg);
df4846c3 200 break;
c81c8b68 201 default:
15490795 202 dev_info(fdtv->device, "unhandled CA ioctl %u\n", cmd);
df4846c3 203 err = -EOPNOTSUPP;
c81c8b68 204 }
df4846c3 205
8ae83cdf 206 /* FIXME Is this necessary? */
15490795 207 avc_tuner_status(fdtv, &stat);
df4846c3 208
c81c8b68
GKH
209 return err;
210}
c81c8b68 211
a70f81c1 212static unsigned int fdtv_ca_io_poll(struct file *file, poll_table *wait)
df4846c3 213{
c81c8b68
GKH
214 return POLLIN;
215}
216
828c0950 217static const struct file_operations fdtv_ca_fops = {
8ae83cdf 218 .owner = THIS_MODULE,
16ef8def 219 .unlocked_ioctl = dvb_generic_ioctl,
8ae83cdf
SR
220 .open = dvb_generic_open,
221 .release = dvb_generic_release,
a70f81c1 222 .poll = fdtv_ca_io_poll,
c81c8b68
GKH
223};
224
a70f81c1 225static struct dvb_device fdtv_ca = {
8ae83cdf
SR
226 .users = 1,
227 .readers = 1,
228 .writers = 1,
a70f81c1
R
229 .fops = &fdtv_ca_fops,
230 .kernel_ioctl = fdtv_ca_ioctl,
c81c8b68
GKH
231};
232
a70f81c1 233int fdtv_ca_register(struct firedtv *fdtv)
df4846c3 234{
15490795 235 struct firedtv_tuner_status stat;
8ae83cdf 236 int err;
c81c8b68 237
15490795 238 if (avc_tuner_status(fdtv, &stat))
df4846c3
HK
239 return -EINVAL;
240
15490795 241 if (!fdtv_ca_ready(&stat))
8ae83cdf
SR
242 return -EFAULT;
243
a70f81c1
R
244 err = dvb_register_device(&fdtv->adapter, &fdtv->cadev,
245 &fdtv_ca, fdtv, DVB_DEVICE_CA);
8ae83cdf 246
15490795
SR
247 if (stat.ca_application_info == 0)
248 dev_err(fdtv->device, "CaApplicationInfo is not set\n");
249 if (stat.ca_date_time_request == 1)
a70f81c1 250 avc_ca_get_time_date(fdtv, &fdtv->ca_time_interval);
df4846c3
HK
251
252 return err;
c81c8b68
GKH
253}
254
a70f81c1 255void fdtv_ca_release(struct firedtv *fdtv)
df4846c3 256{
a70f81c1
R
257 if (fdtv->cadev)
258 dvb_unregister_device(fdtv->cadev);
c81c8b68 259}