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