Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | CA-driver for TwinHan DST Frontend/Card |
| 3 | |
| 4 | Copyright (C) 2004, 2005 Manu Abraham (manu@kromtek.com) |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program 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 |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/string.h> |
| 27 | |
| 28 | #include <linux/dvb/ca.h> |
| 29 | #include "dvbdev.h" |
| 30 | #include "dvb_frontend.h" |
| 31 | |
| 32 | #include "dst_ca.h" |
| 33 | #include "dst_common.h" |
| 34 | |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 35 | static unsigned int verbose = 5; |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 36 | module_param(verbose, int, 0644); |
| 37 | MODULE_PARM_DESC(verbose, "verbose startup messages, default is 1 (yes)"); |
| 38 | |
| 39 | static unsigned int debug = 1; |
| 40 | module_param(debug, int, 0644); |
Johannes Stezenbach | 42dc661 | 2005-05-16 21:54:47 -0700 | [diff] [blame] | 41 | MODULE_PARM_DESC(debug, "debug messages, default is 1 (yes)"); |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 42 | |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 43 | #define dprintk if (debug) printk |
| 44 | |
Johannes Stezenbach | 4a2cc12 | 2005-05-16 21:54:45 -0700 | [diff] [blame] | 45 | /* Need some more work */ |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 46 | static int ca_set_slot_descr(void) |
| 47 | { |
| 48 | /* We could make this more graceful ? */ |
| 49 | return -EOPNOTSUPP; |
| 50 | } |
| 51 | |
Johannes Stezenbach | 4a2cc12 | 2005-05-16 21:54:45 -0700 | [diff] [blame] | 52 | /* Need some more work */ |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 53 | static int ca_set_pid(void) |
| 54 | { |
| 55 | /* We could make this more graceful ? */ |
| 56 | return -EOPNOTSUPP; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | static int put_checksum(u8 *check_string, int length) |
| 61 | { |
| 62 | u8 i = 0, checksum = 0; |
| 63 | |
| 64 | if (verbose > 3) { |
| 65 | dprintk("%s: ========================= Checksum calculation ===========================\n", __FUNCTION__); |
| 66 | dprintk("%s: String Length=[0x%02x]\n", __FUNCTION__, length); |
| 67 | |
| 68 | dprintk("%s: String=[", __FUNCTION__); |
| 69 | } |
| 70 | while (i < length) { |
| 71 | if (verbose > 3) |
| 72 | dprintk(" %02x", check_string[i]); |
| 73 | checksum += check_string[i]; |
| 74 | i++; |
| 75 | } |
| 76 | if (verbose > 3) { |
| 77 | dprintk(" ]\n"); |
| 78 | dprintk("%s: Sum=[%02x]\n", __FUNCTION__, checksum); |
| 79 | } |
| 80 | check_string[length] = ~checksum + 1; |
| 81 | if (verbose > 3) { |
| 82 | dprintk("%s: Checksum=[%02x]\n", __FUNCTION__, check_string[length]); |
| 83 | dprintk("%s: ==========================================================================\n", __FUNCTION__); |
| 84 | } |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int dst_ci_command(struct dst_state* state, u8 * data, u8 *ca_string, u8 len, int read) |
| 90 | { |
| 91 | u8 reply; |
| 92 | |
| 93 | dst_comm_init(state); |
| 94 | msleep(65); |
| 95 | |
| 96 | if (write_dst(state, data, len)) { |
| 97 | dprintk("%s: Write not successful, trying to recover\n", __FUNCTION__); |
| 98 | dst_error_recovery(state); |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | if ((dst_pio_disable(state)) < 0) { |
| 103 | dprintk("%s: DST PIO disable failed.\n", __FUNCTION__); |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | if (read_dst(state, &reply, GET_ACK) < 0) { |
| 108 | dprintk("%s: Read not successful, trying to recover\n", __FUNCTION__); |
| 109 | dst_error_recovery(state); |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | if (read) { |
| 114 | if (! dst_wait_dst_ready(state, LONG_DELAY)) { |
| 115 | dprintk("%s: 8820 not ready\n", __FUNCTION__); |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | if (read_dst(state, ca_string, 128) < 0) { /* Try to make this dynamic */ |
| 120 | dprintk("%s: Read not successful, trying to recover\n", __FUNCTION__); |
| 121 | dst_error_recovery(state); |
| 122 | return -1; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | static int dst_put_ci(struct dst_state *state, u8 *data, int len, u8 *ca_string, int read) |
| 131 | { |
| 132 | u8 dst_ca_comm_err = 0; |
| 133 | |
| 134 | while (dst_ca_comm_err < RETRIES) { |
| 135 | dst_comm_init(state); |
| 136 | if (verbose > 2) |
| 137 | dprintk("%s: Put Command\n", __FUNCTION__); |
| 138 | if (dst_ci_command(state, data, ca_string, len, read)) { // If error |
| 139 | dst_error_recovery(state); |
| 140 | dst_ca_comm_err++; // work required here. |
| 141 | } |
| 142 | break; |
| 143 | } |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | |
| 150 | static int ca_get_app_info(struct dst_state *state) |
| 151 | { |
| 152 | static u8 command[8] = {0x07, 0x40, 0x01, 0x00, 0x01, 0x00, 0x00, 0xff}; |
| 153 | |
| 154 | put_checksum(&command[0], command[0]); |
| 155 | if ((dst_put_ci(state, command, sizeof(command), state->messages, GET_REPLY)) < 0) { |
| 156 | dprintk("%s: -->dst_put_ci FAILED !\n", __FUNCTION__); |
| 157 | return -1; |
| 158 | } |
| 159 | if (verbose > 1) { |
| 160 | dprintk("%s: -->dst_put_ci SUCCESS !\n", __FUNCTION__); |
| 161 | |
| 162 | dprintk("%s: ================================ CI Module Application Info ======================================\n", __FUNCTION__); |
| 163 | dprintk("%s: Application Type=[%d], Application Vendor=[%d], Vendor Code=[%d]\n%s: Application info=[%s]\n", |
| 164 | __FUNCTION__, state->messages[7], (state->messages[8] << 8) | state->messages[9], |
Johannes Stezenbach | b98b79d | 2005-05-16 21:54:47 -0700 | [diff] [blame] | 165 | (state->messages[10] << 8) | state->messages[11], __FUNCTION__, (char *)(&state->messages[12])); |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 166 | dprintk("%s: ==================================================================================================\n", __FUNCTION__); |
| 167 | } |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static int ca_get_slot_caps(struct dst_state *state, struct ca_caps *p_ca_caps, void *arg) |
| 173 | { |
| 174 | int i; |
| 175 | u8 slot_cap[256]; |
| 176 | static u8 slot_command[8] = {0x07, 0x40, 0x02, 0x00, 0x02, 0x00, 0x00, 0xff}; |
| 177 | |
| 178 | put_checksum(&slot_command[0], slot_command[0]); |
| 179 | if ((dst_put_ci(state, slot_command, sizeof (slot_command), slot_cap, GET_REPLY)) < 0) { |
| 180 | dprintk("%s: -->dst_put_ci FAILED !\n", __FUNCTION__); |
| 181 | return -1; |
| 182 | } |
| 183 | if (verbose > 1) |
| 184 | dprintk("%s: -->dst_put_ci SUCCESS !\n", __FUNCTION__); |
| 185 | |
| 186 | /* Will implement the rest soon */ |
| 187 | |
| 188 | if (verbose > 1) { |
| 189 | dprintk("%s: Slot cap = [%d]\n", __FUNCTION__, slot_cap[7]); |
| 190 | dprintk("===================================\n"); |
| 191 | for (i = 0; i < 8; i++) |
| 192 | dprintk(" %d", slot_cap[i]); |
| 193 | dprintk("\n"); |
| 194 | } |
| 195 | |
| 196 | p_ca_caps->slot_num = 1; |
| 197 | p_ca_caps->slot_type = 1; |
| 198 | p_ca_caps->descr_num = slot_cap[7]; |
| 199 | p_ca_caps->descr_type = 1; |
| 200 | |
| 201 | |
| 202 | if (copy_to_user((struct ca_caps *)arg, p_ca_caps, sizeof (struct ca_caps))) { |
| 203 | return -EFAULT; |
| 204 | } |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
Johannes Stezenbach | 4a2cc12 | 2005-05-16 21:54:45 -0700 | [diff] [blame] | 209 | /* Need some more work */ |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 210 | static int ca_get_slot_descr(struct dst_state *state, struct ca_msg *p_ca_message, void *arg) |
| 211 | { |
| 212 | return -EOPNOTSUPP; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | static int ca_get_slot_info(struct dst_state *state, struct ca_slot_info *p_ca_slot_info, void *arg) |
| 217 | { |
| 218 | int i; |
| 219 | static u8 slot_command[8] = {0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff}; |
| 220 | |
| 221 | u8 *slot_info = state->rxbuffer; |
| 222 | |
| 223 | put_checksum(&slot_command[0], 7); |
| 224 | if ((dst_put_ci(state, slot_command, sizeof (slot_command), slot_info, GET_REPLY)) < 0) { |
| 225 | dprintk("%s: -->dst_put_ci FAILED !\n", __FUNCTION__); |
| 226 | return -1; |
| 227 | } |
| 228 | if (verbose > 1) |
| 229 | dprintk("%s: -->dst_put_ci SUCCESS !\n", __FUNCTION__); |
| 230 | |
| 231 | /* Will implement the rest soon */ |
| 232 | |
| 233 | if (verbose > 1) { |
| 234 | dprintk("%s: Slot info = [%d]\n", __FUNCTION__, slot_info[3]); |
| 235 | dprintk("===================================\n"); |
| 236 | for (i = 0; i < 8; i++) |
| 237 | dprintk(" %d", slot_info[i]); |
| 238 | dprintk("\n"); |
| 239 | } |
| 240 | |
| 241 | if (slot_info[4] & 0x80) { |
| 242 | p_ca_slot_info->flags = CA_CI_MODULE_PRESENT; |
| 243 | p_ca_slot_info->num = 1; |
| 244 | p_ca_slot_info->type = CA_CI; |
| 245 | } |
| 246 | else if (slot_info[4] & 0x40) { |
| 247 | p_ca_slot_info->flags = CA_CI_MODULE_READY; |
| 248 | p_ca_slot_info->num = 1; |
| 249 | p_ca_slot_info->type = CA_CI; |
| 250 | } |
| 251 | else { |
| 252 | p_ca_slot_info->flags = 0; |
| 253 | } |
| 254 | |
| 255 | if (copy_to_user((struct ca_slot_info *)arg, p_ca_slot_info, sizeof (struct ca_slot_info))) { |
| 256 | return -EFAULT; |
| 257 | } |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | static int ca_get_message(struct dst_state *state, struct ca_msg *p_ca_message, void *arg) |
| 266 | { |
| 267 | u8 i = 0; |
| 268 | u32 command = 0; |
| 269 | |
| 270 | if (copy_from_user(p_ca_message, (void *)arg, sizeof (struct ca_msg))) |
| 271 | return -EFAULT; |
| 272 | |
| 273 | |
| 274 | if (p_ca_message->msg) { |
| 275 | if (verbose > 3) |
| 276 | dprintk("Message = [%02x %02x %02x]\n", p_ca_message->msg[0], p_ca_message->msg[1], p_ca_message->msg[2]); |
| 277 | |
| 278 | for (i = 0; i < 3; i++) { |
| 279 | command = command | p_ca_message->msg[i]; |
| 280 | if (i < 2) |
| 281 | command = command << 8; |
| 282 | } |
| 283 | if (verbose > 3) |
| 284 | dprintk("%s:Command=[0x%x]\n", __FUNCTION__, command); |
| 285 | |
| 286 | switch (command) { |
| 287 | case CA_APP_INFO: |
| 288 | memcpy(p_ca_message->msg, state->messages, 128); |
| 289 | if (copy_to_user((void *)arg, p_ca_message, sizeof (struct ca_msg)) ) |
| 290 | return -EFAULT; |
| 291 | break; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 298 | static int handle_dst_tag(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer, u32 length) |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 299 | { |
Johannes Stezenbach | 4a2cc12 | 2005-05-16 21:54:45 -0700 | [diff] [blame] | 300 | if (state->dst_hw_cap & DST_TYPE_HAS_SESSION) { |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 301 | hw_buffer->msg[2] = p_ca_message->msg[1]; /* MSB */ |
| 302 | hw_buffer->msg[3] = p_ca_message->msg[2]; /* LSB */ |
| 303 | } |
| 304 | else { |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 305 | hw_buffer->msg[0] = (length & 0xff) + 7; |
| 306 | hw_buffer->msg[1] = 0x40; |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 307 | hw_buffer->msg[2] = 0x03; |
| 308 | hw_buffer->msg[3] = 0x00; |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 309 | hw_buffer->msg[4] = 0x03; |
| 310 | hw_buffer->msg[5] = length & 0xff; |
| 311 | hw_buffer->msg[6] = 0x00; |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 312 | } |
| 313 | return 0; |
| 314 | } |
| 315 | |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 316 | |
| 317 | static int write_to_8820(struct dst_state *state, struct ca_msg *hw_buffer, u8 length, u8 reply) |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 318 | { |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 319 | if ((dst_put_ci(state, hw_buffer->msg, length, hw_buffer->msg, reply)) < 0) { |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 320 | dprintk("%s: DST-CI Command failed.\n", __FUNCTION__); |
| 321 | dprintk("%s: Resetting DST.\n", __FUNCTION__); |
| 322 | rdc_reset_state(state); |
| 323 | return -1; |
| 324 | } |
| 325 | if (verbose > 2) |
| 326 | dprintk("%s: DST-CI Command succes.\n", __FUNCTION__); |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 331 | u32 asn_1_decode(u8 *asn_1_array) |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 332 | { |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 333 | u8 length_field = 0, word_count = 0, count = 0; |
| 334 | u32 length = 0; |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 335 | |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 336 | length_field = asn_1_array[0]; |
| 337 | dprintk("%s: Length field=[%02x]\n", __FUNCTION__, length_field); |
| 338 | if (length_field < 0x80) { |
| 339 | length = length_field & 0x7f; |
| 340 | dprintk("%s: Length=[%02x]\n", __FUNCTION__, length); |
| 341 | } else { |
| 342 | word_count = length_field & 0x7f; |
| 343 | for (count = 0; count < word_count; count++) { |
| 344 | length = (length | asn_1_array[count + 1]) << 8; |
| 345 | dprintk("%s: Length=[%04x]\n", __FUNCTION__, length); |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 348 | return length; |
| 349 | } |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 350 | |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 351 | static int init_buffer(u8 *buffer, u32 length) |
| 352 | { |
| 353 | u32 i; |
| 354 | for (i = 0; i < length; i++) |
| 355 | buffer[i] = 0; |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 360 | static int debug_string(u8 *msg, u32 length, u32 offset) |
| 361 | { |
| 362 | u32 i; |
| 363 | |
| 364 | dprintk(" String=[ "); |
| 365 | for (i = offset; i < length; i++) |
| 366 | dprintk("%02x ", msg[i]); |
| 367 | dprintk("]\n"); |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static int copy_string(u8 *destination, u8 *source, u32 dest_offset, u32 source_offset, u32 length) |
| 373 | { |
| 374 | u32 i; |
| 375 | dprintk("%s: Copying [", __FUNCTION__); |
| 376 | for (i = 0; i < length; i++) { |
| 377 | destination[i + dest_offset] = source[i + source_offset]; |
| 378 | dprintk(" %02x", source[i + source_offset]); |
| 379 | } |
| 380 | dprintk("]\n"); |
| 381 | |
| 382 | return i; |
| 383 | } |
| 384 | |
| 385 | static int modify_4_bits(u8 *message, u32 pos) |
| 386 | { |
| 387 | message[pos] &= 0x0f; |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | |
| 393 | |
| 394 | static int ca_set_pmt(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer, u8 reply, u8 query) |
| 395 | { |
| 396 | u32 length = 0, count = 0; |
| 397 | u8 asn_1_words, program_header_length; |
| 398 | u16 program_info_length = 0, es_info_length = 0; |
| 399 | u32 hw_offset = 0, buf_offset = 0, i; |
| 400 | u8 dst_tag_length; |
| 401 | |
| 402 | length = asn_1_decode(&p_ca_message->msg[3]); |
| 403 | dprintk("%s: CA Message length=[%d]\n", __FUNCTION__, length); |
| 404 | dprintk("%s: ASN.1 ", __FUNCTION__); |
| 405 | debug_string(&p_ca_message->msg[4], length, 0); // length does not include tag and length |
| 406 | |
| 407 | init_buffer(hw_buffer->msg, length); |
| 408 | handle_dst_tag(state, p_ca_message, hw_buffer, length); |
| 409 | |
| 410 | hw_offset = 7; |
| 411 | asn_1_words = 1; // just a hack to test, should compute this one |
| 412 | buf_offset = 3; |
| 413 | program_header_length = 6; |
| 414 | dst_tag_length = 7; |
| 415 | |
| 416 | // debug_twinhan_ca_params(state, p_ca_message, hw_buffer, reply, query, length, hw_offset, buf_offset); |
| 417 | // dprintk("%s: Program Header(BUF)", __FUNCTION__); |
| 418 | // debug_string(&p_ca_message->msg[4], program_header_length, 0); |
| 419 | // dprintk("%s: Copying Program header\n", __FUNCTION__); |
| 420 | copy_string(hw_buffer->msg, p_ca_message->msg, hw_offset, (buf_offset + asn_1_words), program_header_length); |
| 421 | buf_offset += program_header_length, hw_offset += program_header_length; |
| 422 | modify_4_bits(hw_buffer->msg, (hw_offset - 2)); |
| 423 | if (state->type_flags & DST_TYPE_HAS_INC_COUNT) { // workaround |
| 424 | dprintk("%s: Probably an ASIC bug !!!\n", __FUNCTION__); |
| 425 | debug_string(hw_buffer->msg, (hw_offset + program_header_length), 0); |
| 426 | hw_buffer->msg[hw_offset - 1] += 1; |
| 427 | } |
| 428 | |
| 429 | // dprintk("%s: Program Header(HW), Count=[%d]", __FUNCTION__, count); |
| 430 | // debug_string(hw_buffer->msg, hw_offset, 0); |
| 431 | |
| 432 | program_info_length = ((program_info_length | (p_ca_message->msg[buf_offset - 1] & 0x0f)) << 8) | p_ca_message->msg[buf_offset]; |
| 433 | dprintk("%s: Program info length=[%02x]\n", __FUNCTION__, program_info_length); |
| 434 | if (program_info_length) { |
| 435 | count = copy_string(hw_buffer->msg, p_ca_message->msg, hw_offset, (buf_offset + 1), (program_info_length + 1) ); // copy next elem, not current |
| 436 | buf_offset += count, hw_offset += count; |
| 437 | // dprintk("%s: Program level ", __FUNCTION__); |
| 438 | // debug_string(hw_buffer->msg, hw_offset, 0); |
| 439 | } |
| 440 | |
| 441 | buf_offset += 1;// hw_offset += 1; |
| 442 | for (i = buf_offset; i < length; i++) { |
| 443 | // dprintk("%s: Stream Header ", __FUNCTION__); |
| 444 | count = copy_string(hw_buffer->msg, p_ca_message->msg, hw_offset, buf_offset, 5); |
| 445 | modify_4_bits(hw_buffer->msg, (hw_offset + 3)); |
| 446 | |
| 447 | hw_offset += 5, buf_offset += 5, i += 4; |
| 448 | // debug_string(hw_buffer->msg, hw_offset, (hw_offset - 5)); |
| 449 | es_info_length = ((es_info_length | (p_ca_message->msg[buf_offset - 1] & 0x0f)) << 8) | p_ca_message->msg[buf_offset]; |
| 450 | dprintk("%s: ES info length=[%02x]\n", __FUNCTION__, es_info_length); |
| 451 | if (es_info_length) { |
| 452 | // copy descriptors @ STREAM level |
| 453 | dprintk("%s: Descriptors @ STREAM level...!!! \n", __FUNCTION__); |
| 454 | } |
| 455 | |
| 456 | } |
| 457 | hw_buffer->msg[length + dst_tag_length] = dst_check_sum(hw_buffer->msg, (length + dst_tag_length)); |
| 458 | // dprintk("%s: Total length=[%d], Checksum=[%02x]\n", __FUNCTION__, (length + dst_tag_length), hw_buffer->msg[length + dst_tag_length]); |
| 459 | debug_string(hw_buffer->msg, (length + dst_tag_length + 1), 0); // dst tags also |
| 460 | write_to_8820(state, hw_buffer, (length + dst_tag_length + 1), reply); // checksum |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 466 | /* Board supports CA PMT reply ? */ |
| 467 | static int dst_check_ca_pmt(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer) |
| 468 | { |
| 469 | int ca_pmt_reply_test = 0; |
| 470 | |
| 471 | /* Do test board */ |
| 472 | /* Not there yet but soon */ |
| 473 | |
| 474 | |
| 475 | /* CA PMT Reply capable */ |
| 476 | if (ca_pmt_reply_test) { |
| 477 | if ((ca_set_pmt(state, p_ca_message, hw_buffer, 1, GET_REPLY)) < 0) { |
| 478 | dprintk("%s: ca_set_pmt.. failed !\n", __FUNCTION__); |
| 479 | return -1; |
| 480 | } |
| 481 | |
| 482 | /* Process CA PMT Reply */ |
| 483 | /* will implement soon */ |
| 484 | dprintk("%s: Not there yet\n", __FUNCTION__); |
| 485 | } |
| 486 | /* CA PMT Reply not capable */ |
| 487 | if (!ca_pmt_reply_test) { |
| 488 | if ((ca_set_pmt(state, p_ca_message, hw_buffer, 0, NO_REPLY)) < 0) { |
| 489 | dprintk("%s: ca_set_pmt.. failed !\n", __FUNCTION__); |
| 490 | return -1; |
| 491 | } |
| 492 | if (verbose > 3) |
| 493 | dprintk("%s: ca_set_pmt.. success !\n", __FUNCTION__); |
| 494 | /* put a dummy message */ |
| 495 | |
| 496 | } |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | static int ca_send_message(struct dst_state *state, struct ca_msg *p_ca_message, void *arg) |
| 501 | { |
| 502 | int i = 0; |
| 503 | unsigned int ca_message_header_len; |
| 504 | |
| 505 | u32 command = 0; |
| 506 | struct ca_msg *hw_buffer; |
| 507 | |
| 508 | if ((hw_buffer = (struct ca_msg *) kmalloc(sizeof (struct ca_msg), GFP_KERNEL)) == NULL) { |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 509 | dprintk("%s: Memory allocation failure\n", __FUNCTION__); |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 510 | return -ENOMEM; |
| 511 | } |
| 512 | if (verbose > 3) |
| 513 | dprintk("%s\n", __FUNCTION__); |
| 514 | |
| 515 | if (copy_from_user(p_ca_message, (void *)arg, sizeof (struct ca_msg))) |
| 516 | return -EFAULT; |
| 517 | |
| 518 | if (p_ca_message->msg) { |
| 519 | ca_message_header_len = p_ca_message->length; /* Restore it back when you are done */ |
| 520 | /* EN50221 tag */ |
| 521 | command = 0; |
| 522 | |
| 523 | for (i = 0; i < 3; i++) { |
| 524 | command = command | p_ca_message->msg[i]; |
| 525 | if (i < 2) |
| 526 | command = command << 8; |
| 527 | } |
| 528 | if (verbose > 3) |
| 529 | dprintk("%s:Command=[0x%x]\n", __FUNCTION__, command); |
| 530 | |
| 531 | switch (command) { |
| 532 | case CA_PMT: |
| 533 | if (verbose > 3) |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 534 | // dprintk("Command = SEND_CA_PMT\n"); |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 535 | dprintk("Command = SEND_CA_PMT\n"); |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 536 | // if ((ca_set_pmt(state, p_ca_message, hw_buffer, 0, 0)) < 0) { |
| 537 | if ((ca_set_pmt(state, p_ca_message, hw_buffer, 0, 0)) < 0) { // code simplification started |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 538 | dprintk("%s: -->CA_PMT Failed !\n", __FUNCTION__); |
| 539 | return -1; |
| 540 | } |
| 541 | if (verbose > 3) |
| 542 | dprintk("%s: -->CA_PMT Success !\n", __FUNCTION__); |
| 543 | // retval = dummy_set_pmt(state, p_ca_message, hw_buffer, 0, 0); |
| 544 | |
| 545 | break; |
| 546 | |
| 547 | case CA_PMT_REPLY: |
| 548 | if (verbose > 3) |
| 549 | dprintk("Command = CA_PMT_REPLY\n"); |
| 550 | /* Have to handle the 2 basic types of cards here */ |
| 551 | if ((dst_check_ca_pmt(state, p_ca_message, hw_buffer)) < 0) { |
| 552 | dprintk("%s: -->CA_PMT_REPLY Failed !\n", __FUNCTION__); |
| 553 | return -1; |
| 554 | } |
| 555 | if (verbose > 3) |
| 556 | dprintk("%s: -->CA_PMT_REPLY Success !\n", __FUNCTION__); |
| 557 | |
| 558 | /* Certain boards do behave different ? */ |
| 559 | // retval = ca_set_pmt(state, p_ca_message, hw_buffer, 1, 1); |
| 560 | |
| 561 | case CA_APP_INFO_ENQUIRY: // only for debugging |
| 562 | if (verbose > 3) |
| 563 | dprintk("%s: Getting Cam Application information\n", __FUNCTION__); |
| 564 | |
| 565 | if ((ca_get_app_info(state)) < 0) { |
| 566 | dprintk("%s: -->CA_APP_INFO_ENQUIRY Failed !\n", __FUNCTION__); |
| 567 | return -1; |
| 568 | } |
| 569 | if (verbose > 3) |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 570 | dprintk("%s: -->CA_APP_INFO_ENQUIRY Success !\n", __FUNCTION__); |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 571 | |
| 572 | break; |
| 573 | } |
| 574 | } |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | static int dst_ca_ioctl(struct inode *inode, struct file *file, unsigned int cmd, void *arg) |
| 579 | { |
| 580 | struct dvb_device* dvbdev = (struct dvb_device*) file->private_data; |
| 581 | struct dst_state* state = (struct dst_state*) dvbdev->priv; |
| 582 | struct ca_slot_info *p_ca_slot_info; |
| 583 | struct ca_caps *p_ca_caps; |
| 584 | struct ca_msg *p_ca_message; |
| 585 | |
| 586 | if ((p_ca_message = (struct ca_msg *) kmalloc(sizeof (struct ca_msg), GFP_KERNEL)) == NULL) { |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 587 | dprintk("%s: Memory allocation failure\n", __FUNCTION__); |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 588 | return -ENOMEM; |
| 589 | } |
| 590 | |
| 591 | if ((p_ca_slot_info = (struct ca_slot_info *) kmalloc(sizeof (struct ca_slot_info), GFP_KERNEL)) == NULL) { |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 592 | dprintk("%s: Memory allocation failure\n", __FUNCTION__); |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 593 | return -ENOMEM; |
| 594 | } |
| 595 | |
| 596 | if ((p_ca_caps = (struct ca_caps *) kmalloc(sizeof (struct ca_caps), GFP_KERNEL)) == NULL) { |
Manu Abraham | 7d53421 | 2005-07-07 17:57:50 -0700 | [diff] [blame^] | 597 | dprintk("%s: Memory allocation failure\n", __FUNCTION__); |
Johannes Stezenbach | 50b215a | 2005-05-16 21:54:41 -0700 | [diff] [blame] | 598 | return -ENOMEM; |
| 599 | } |
| 600 | |
| 601 | /* We have now only the standard ioctl's, the driver is upposed to handle internals. */ |
| 602 | switch (cmd) { |
| 603 | case CA_SEND_MSG: |
| 604 | if (verbose > 1) |
| 605 | dprintk("%s: Sending message\n", __FUNCTION__); |
| 606 | if ((ca_send_message(state, p_ca_message, arg)) < 0) { |
| 607 | dprintk("%s: -->CA_SEND_MSG Failed !\n", __FUNCTION__); |
| 608 | return -1; |
| 609 | } |
| 610 | |
| 611 | break; |
| 612 | |
| 613 | case CA_GET_MSG: |
| 614 | if (verbose > 1) |
| 615 | dprintk("%s: Getting message\n", __FUNCTION__); |
| 616 | if ((ca_get_message(state, p_ca_message, arg)) < 0) { |
| 617 | dprintk("%s: -->CA_GET_MSG Failed !\n", __FUNCTION__); |
| 618 | return -1; |
| 619 | } |
| 620 | if (verbose > 1) |
| 621 | dprintk("%s: -->CA_GET_MSG Success !\n", __FUNCTION__); |
| 622 | |
| 623 | break; |
| 624 | |
| 625 | case CA_RESET: |
| 626 | if (verbose > 1) |
| 627 | dprintk("%s: Resetting DST\n", __FUNCTION__); |
| 628 | dst_error_bailout(state); |
| 629 | msleep(4000); |
| 630 | |
| 631 | break; |
| 632 | |
| 633 | case CA_GET_SLOT_INFO: |
| 634 | if (verbose > 1) |
| 635 | dprintk("%s: Getting Slot info\n", __FUNCTION__); |
| 636 | if ((ca_get_slot_info(state, p_ca_slot_info, arg)) < 0) { |
| 637 | dprintk("%s: -->CA_GET_SLOT_INFO Failed !\n", __FUNCTION__); |
| 638 | return -1; |
| 639 | } |
| 640 | if (verbose > 1) |
| 641 | dprintk("%s: -->CA_GET_SLOT_INFO Success !\n", __FUNCTION__); |
| 642 | |
| 643 | break; |
| 644 | |
| 645 | case CA_GET_CAP: |
| 646 | if (verbose > 1) |
| 647 | dprintk("%s: Getting Slot capabilities\n", __FUNCTION__); |
| 648 | if ((ca_get_slot_caps(state, p_ca_caps, arg)) < 0) { |
| 649 | dprintk("%s: -->CA_GET_CAP Failed !\n", __FUNCTION__); |
| 650 | return -1; |
| 651 | } |
| 652 | if (verbose > 1) |
| 653 | dprintk("%s: -->CA_GET_CAP Success !\n", __FUNCTION__); |
| 654 | |
| 655 | break; |
| 656 | |
| 657 | case CA_GET_DESCR_INFO: |
| 658 | if (verbose > 1) |
| 659 | dprintk("%s: Getting descrambler description\n", __FUNCTION__); |
| 660 | if ((ca_get_slot_descr(state, p_ca_message, arg)) < 0) { |
| 661 | dprintk("%s: -->CA_GET_DESCR_INFO Failed !\n", __FUNCTION__); |
| 662 | return -1; |
| 663 | } |
| 664 | if (verbose > 1) |
| 665 | dprintk("%s: -->CA_GET_DESCR_INFO Success !\n", __FUNCTION__); |
| 666 | |
| 667 | break; |
| 668 | |
| 669 | case CA_SET_DESCR: |
| 670 | if (verbose > 1) |
| 671 | dprintk("%s: Setting descrambler\n", __FUNCTION__); |
| 672 | if ((ca_set_slot_descr()) < 0) { |
| 673 | dprintk("%s: -->CA_SET_DESCR Failed !\n", __FUNCTION__); |
| 674 | return -1; |
| 675 | } |
| 676 | if (verbose > 1) |
| 677 | dprintk("%s: -->CA_SET_DESCR Success !\n", __FUNCTION__); |
| 678 | |
| 679 | break; |
| 680 | |
| 681 | case CA_SET_PID: |
| 682 | if (verbose > 1) |
| 683 | dprintk("%s: Setting PID\n", __FUNCTION__); |
| 684 | if ((ca_set_pid()) < 0) { |
| 685 | dprintk("%s: -->CA_SET_PID Failed !\n", __FUNCTION__); |
| 686 | return -1; |
| 687 | } |
| 688 | if (verbose > 1) |
| 689 | dprintk("%s: -->CA_SET_PID Success !\n", __FUNCTION__); |
| 690 | |
| 691 | default: |
| 692 | return -EOPNOTSUPP; |
| 693 | }; |
| 694 | |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | static int dst_ca_open(struct inode *inode, struct file *file) |
| 699 | { |
| 700 | if (verbose > 4) |
| 701 | dprintk("%s:Device opened [%p]\n", __FUNCTION__, file); |
| 702 | try_module_get(THIS_MODULE); |
| 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | static int dst_ca_release(struct inode *inode, struct file *file) |
| 708 | { |
| 709 | if (verbose > 4) |
| 710 | dprintk("%s:Device closed.\n", __FUNCTION__); |
| 711 | module_put(THIS_MODULE); |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | static int dst_ca_read(struct file *file, char __user * buffer, size_t length, loff_t * offset) |
| 717 | { |
| 718 | int bytes_read = 0; |
| 719 | |
| 720 | if (verbose > 4) |
| 721 | dprintk("%s:Device read.\n", __FUNCTION__); |
| 722 | |
| 723 | return bytes_read; |
| 724 | } |
| 725 | |
| 726 | static int dst_ca_write(struct file *file, const char __user * buffer, size_t length, loff_t * offset) |
| 727 | { |
| 728 | if (verbose > 4) |
| 729 | dprintk("%s:Device write.\n", __FUNCTION__); |
| 730 | |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | static struct file_operations dst_ca_fops = { |
| 735 | .owner = THIS_MODULE, |
| 736 | .ioctl = (void *)dst_ca_ioctl, |
| 737 | .open = dst_ca_open, |
| 738 | .release = dst_ca_release, |
| 739 | .read = dst_ca_read, |
| 740 | .write = dst_ca_write |
| 741 | }; |
| 742 | |
| 743 | static struct dvb_device dvbdev_ca = { |
| 744 | .priv = NULL, |
| 745 | .users = 1, |
| 746 | .readers = 1, |
| 747 | .writers = 1, |
| 748 | .fops = &dst_ca_fops |
| 749 | }; |
| 750 | |
| 751 | int dst_ca_attach(struct dst_state *dst, struct dvb_adapter *dvb_adapter) |
| 752 | { |
| 753 | struct dvb_device *dvbdev; |
| 754 | if (verbose > 4) |
| 755 | dprintk("%s:registering DST-CA device\n", __FUNCTION__); |
| 756 | dvb_register_device(dvb_adapter, &dvbdev, &dvbdev_ca, dst, DVB_DEVICE_CA); |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | EXPORT_SYMBOL(dst_ca_attach); |
| 761 | |
| 762 | MODULE_DESCRIPTION("DST DVB-S/T/C Combo CA driver"); |
| 763 | MODULE_AUTHOR("Manu Abraham"); |
| 764 | MODULE_LICENSE("GPL"); |