2 CA-driver for TwinHan DST Frontend/Card
4 Copyright (C) 2004, 2005 Manu Abraham (manu@kromtek.com)
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.
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.
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.
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
28 #include <linux/dvb/ca.h>
30 #include "dvb_frontend.h"
33 #include "dst_common.h"
35 static unsigned int verbose = 5;
36 module_param(verbose, int, 0644);
37 MODULE_PARM_DESC(verbose, "verbose startup messages, default is 1 (yes)");
39 static unsigned int debug = 1;
40 module_param(debug, int, 0644);
41 MODULE_PARM_DESC(debug, "debug messages, default is 1 (yes)");
43 #define dprintk if (debug) printk
45 /* Need some more work */
46 static int ca_set_slot_descr(void)
48 /* We could make this more graceful ? */
52 /* Need some more work */
53 static int ca_set_pid(void)
55 /* We could make this more graceful ? */
60 static int put_checksum(u8 *check_string, int length)
62 u8 i = 0, checksum = 0;
65 dprintk("%s: ========================= Checksum calculation ===========================\n", __FUNCTION__);
66 dprintk("%s: String Length=[0x%02x]\n", __FUNCTION__, length);
68 dprintk("%s: String=[", __FUNCTION__);
72 dprintk(" %02x", check_string[i]);
73 checksum += check_string[i];
78 dprintk("%s: Sum=[%02x]\n", __FUNCTION__, checksum);
80 check_string[length] = ~checksum + 1;
82 dprintk("%s: Checksum=[%02x]\n", __FUNCTION__, check_string[length]);
83 dprintk("%s: ==========================================================================\n", __FUNCTION__);
89 static int dst_ci_command(struct dst_state* state, u8 * data, u8 *ca_string, u8 len, int read)
96 if (write_dst(state, data, len)) {
97 dprintk("%s: Write not successful, trying to recover\n", __FUNCTION__);
98 dst_error_recovery(state);
102 if ((dst_pio_disable(state)) < 0) {
103 dprintk("%s: DST PIO disable failed.\n", __FUNCTION__);
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);
114 if (! dst_wait_dst_ready(state, LONG_DELAY)) {
115 dprintk("%s: 8820 not ready\n", __FUNCTION__);
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);
130 static int dst_put_ci(struct dst_state *state, u8 *data, int len, u8 *ca_string, int read)
132 u8 dst_ca_comm_err = 0;
134 while (dst_ca_comm_err < RETRIES) {
135 dst_comm_init(state);
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.
150 static int ca_get_app_info(struct dst_state *state)
152 static u8 command[8] = {0x07, 0x40, 0x01, 0x00, 0x01, 0x00, 0x00, 0xff};
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__);
160 dprintk("%s: -->dst_put_ci SUCCESS !\n", __FUNCTION__);
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],
165 (state->messages[10] << 8) | state->messages[11], __FUNCTION__, (char *)(&state->messages[12]));
166 dprintk("%s: ==================================================================================================\n", __FUNCTION__);
172 static int ca_get_slot_caps(struct dst_state *state, struct ca_caps *p_ca_caps, void *arg)
176 static u8 slot_command[8] = {0x07, 0x40, 0x02, 0x00, 0x02, 0x00, 0x00, 0xff};
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__);
184 dprintk("%s: -->dst_put_ci SUCCESS !\n", __FUNCTION__);
186 /* Will implement the rest soon */
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]);
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;
202 if (copy_to_user((struct ca_caps *)arg, p_ca_caps, sizeof (struct ca_caps))) {
209 /* Need some more work */
210 static int ca_get_slot_descr(struct dst_state *state, struct ca_msg *p_ca_message, void *arg)
216 static int ca_get_slot_info(struct dst_state *state, struct ca_slot_info *p_ca_slot_info, void *arg)
219 static u8 slot_command[8] = {0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff};
221 u8 *slot_info = state->rxbuffer;
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__);
229 dprintk("%s: -->dst_put_ci SUCCESS !\n", __FUNCTION__);
231 /* Will implement the rest soon */
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]);
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;
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;
252 p_ca_slot_info->flags = 0;
255 if (copy_to_user((struct ca_slot_info *)arg, p_ca_slot_info, sizeof (struct ca_slot_info))) {
265 static int ca_get_message(struct dst_state *state, struct ca_msg *p_ca_message, void *arg)
270 if (copy_from_user(p_ca_message, (void *)arg, sizeof (struct ca_msg)))
274 if (p_ca_message->msg) {
276 dprintk("Message = [%02x %02x %02x]\n", p_ca_message->msg[0], p_ca_message->msg[1], p_ca_message->msg[2]);
278 for (i = 0; i < 3; i++) {
279 command = command | p_ca_message->msg[i];
281 command = command << 8;
284 dprintk("%s:Command=[0x%x]\n", __FUNCTION__, command);
288 memcpy(p_ca_message->msg, state->messages, 128);
289 if (copy_to_user((void *)arg, p_ca_message, sizeof (struct ca_msg)) )
298 static int handle_dst_tag(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer, u32 length)
300 if (state->dst_hw_cap & DST_TYPE_HAS_SESSION) {
301 hw_buffer->msg[2] = p_ca_message->msg[1]; /* MSB */
302 hw_buffer->msg[3] = p_ca_message->msg[2]; /* LSB */
305 hw_buffer->msg[0] = (length & 0xff) + 7;
306 hw_buffer->msg[1] = 0x40;
307 hw_buffer->msg[2] = 0x03;
308 hw_buffer->msg[3] = 0x00;
309 hw_buffer->msg[4] = 0x03;
310 hw_buffer->msg[5] = length & 0xff;
311 hw_buffer->msg[6] = 0x00;
317 static int write_to_8820(struct dst_state *state, struct ca_msg *hw_buffer, u8 length, u8 reply)
319 if ((dst_put_ci(state, hw_buffer->msg, length, hw_buffer->msg, reply)) < 0) {
320 dprintk("%s: DST-CI Command failed.\n", __FUNCTION__);
321 dprintk("%s: Resetting DST.\n", __FUNCTION__);
322 rdc_reset_state(state);
326 dprintk("%s: DST-CI Command succes.\n", __FUNCTION__);
331 u32 asn_1_decode(u8 *asn_1_array)
333 u8 length_field = 0, word_count = 0, count = 0;
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);
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);
351 static int init_buffer(u8 *buffer, u32 length)
354 for (i = 0; i < length; i++)
360 static int debug_string(u8 *msg, u32 length, u32 offset)
364 dprintk(" String=[ ");
365 for (i = offset; i < length; i++)
366 dprintk("%02x ", msg[i]);
372 static int copy_string(u8 *destination, u8 *source, u32 dest_offset, u32 source_offset, u32 length)
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]);
385 static int modify_4_bits(u8 *message, u32 pos)
387 message[pos] &= 0x0f;
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)
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;
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
407 init_buffer(hw_buffer->msg, length);
408 handle_dst_tag(state, p_ca_message, hw_buffer, length);
411 asn_1_words = 1; // just a hack to test, should compute this one
413 program_header_length = 6;
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;
429 // dprintk("%s: Program Header(HW), Count=[%d]", __FUNCTION__, count);
430 // debug_string(hw_buffer->msg, hw_offset, 0);
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);
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));
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__);
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
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)
469 int ca_pmt_reply_test = 0;
472 /* Not there yet but soon */
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__);
482 /* Process CA PMT Reply */
483 /* will implement soon */
484 dprintk("%s: Not there yet\n", __FUNCTION__);
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__);
493 dprintk("%s: ca_set_pmt.. success !\n", __FUNCTION__);
494 /* put a dummy message */
500 static int ca_send_message(struct dst_state *state, struct ca_msg *p_ca_message, void *arg)
503 unsigned int ca_message_header_len;
506 struct ca_msg *hw_buffer;
508 if ((hw_buffer = (struct ca_msg *) kmalloc(sizeof (struct ca_msg), GFP_KERNEL)) == NULL) {
509 dprintk("%s: Memory allocation failure\n", __FUNCTION__);
513 dprintk("%s\n", __FUNCTION__);
515 if (copy_from_user(p_ca_message, (void *)arg, sizeof (struct ca_msg)))
518 if (p_ca_message->msg) {
519 ca_message_header_len = p_ca_message->length; /* Restore it back when you are done */
523 for (i = 0; i < 3; i++) {
524 command = command | p_ca_message->msg[i];
526 command = command << 8;
529 dprintk("%s:Command=[0x%x]\n", __FUNCTION__, command);
534 // dprintk("Command = SEND_CA_PMT\n");
535 dprintk("Command = SEND_CA_PMT\n");
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
538 dprintk("%s: -->CA_PMT Failed !\n", __FUNCTION__);
542 dprintk("%s: -->CA_PMT Success !\n", __FUNCTION__);
543 // retval = dummy_set_pmt(state, p_ca_message, hw_buffer, 0, 0);
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__);
556 dprintk("%s: -->CA_PMT_REPLY Success !\n", __FUNCTION__);
558 /* Certain boards do behave different ? */
559 // retval = ca_set_pmt(state, p_ca_message, hw_buffer, 1, 1);
561 case CA_APP_INFO_ENQUIRY: // only for debugging
563 dprintk("%s: Getting Cam Application information\n", __FUNCTION__);
565 if ((ca_get_app_info(state)) < 0) {
566 dprintk("%s: -->CA_APP_INFO_ENQUIRY Failed !\n", __FUNCTION__);
570 dprintk("%s: -->CA_APP_INFO_ENQUIRY Success !\n", __FUNCTION__);
578 static int dst_ca_ioctl(struct inode *inode, struct file *file, unsigned int cmd, void *arg)
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;
586 if ((p_ca_message = (struct ca_msg *) kmalloc(sizeof (struct ca_msg), GFP_KERNEL)) == NULL) {
587 dprintk("%s: Memory allocation failure\n", __FUNCTION__);
591 if ((p_ca_slot_info = (struct ca_slot_info *) kmalloc(sizeof (struct ca_slot_info), GFP_KERNEL)) == NULL) {
592 dprintk("%s: Memory allocation failure\n", __FUNCTION__);
596 if ((p_ca_caps = (struct ca_caps *) kmalloc(sizeof (struct ca_caps), GFP_KERNEL)) == NULL) {
597 dprintk("%s: Memory allocation failure\n", __FUNCTION__);
601 /* We have now only the standard ioctl's, the driver is upposed to handle internals. */
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__);
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__);
621 dprintk("%s: -->CA_GET_MSG Success !\n", __FUNCTION__);
627 dprintk("%s: Resetting DST\n", __FUNCTION__);
628 dst_error_bailout(state);
633 case CA_GET_SLOT_INFO:
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__);
641 dprintk("%s: -->CA_GET_SLOT_INFO Success !\n", __FUNCTION__);
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__);
653 dprintk("%s: -->CA_GET_CAP Success !\n", __FUNCTION__);
657 case CA_GET_DESCR_INFO:
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__);
665 dprintk("%s: -->CA_GET_DESCR_INFO Success !\n", __FUNCTION__);
671 dprintk("%s: Setting descrambler\n", __FUNCTION__);
672 if ((ca_set_slot_descr()) < 0) {
673 dprintk("%s: -->CA_SET_DESCR Failed !\n", __FUNCTION__);
677 dprintk("%s: -->CA_SET_DESCR Success !\n", __FUNCTION__);
683 dprintk("%s: Setting PID\n", __FUNCTION__);
684 if ((ca_set_pid()) < 0) {
685 dprintk("%s: -->CA_SET_PID Failed !\n", __FUNCTION__);
689 dprintk("%s: -->CA_SET_PID Success !\n", __FUNCTION__);
698 static int dst_ca_open(struct inode *inode, struct file *file)
701 dprintk("%s:Device opened [%p]\n", __FUNCTION__, file);
702 try_module_get(THIS_MODULE);
707 static int dst_ca_release(struct inode *inode, struct file *file)
710 dprintk("%s:Device closed.\n", __FUNCTION__);
711 module_put(THIS_MODULE);
716 static int dst_ca_read(struct file *file, char __user * buffer, size_t length, loff_t * offset)
721 dprintk("%s:Device read.\n", __FUNCTION__);
726 static int dst_ca_write(struct file *file, const char __user * buffer, size_t length, loff_t * offset)
729 dprintk("%s:Device write.\n", __FUNCTION__);
734 static struct file_operations dst_ca_fops = {
735 .owner = THIS_MODULE,
736 .ioctl = (void *)dst_ca_ioctl,
738 .release = dst_ca_release,
740 .write = dst_ca_write
743 static struct dvb_device dvbdev_ca = {
751 int dst_ca_attach(struct dst_state *dst, struct dvb_adapter *dvb_adapter)
753 struct dvb_device *dvbdev;
755 dprintk("%s:registering DST-CA device\n", __FUNCTION__);
756 dvb_register_device(dvb_adapter, &dvbdev, &dvbdev_ca, dst, DVB_DEVICE_CA);
760 EXPORT_SYMBOL(dst_ca_attach);
762 MODULE_DESCRIPTION("DST DVB-S/T/C Combo CA driver");
763 MODULE_AUTHOR("Manu Abraham");
764 MODULE_LICENSE("GPL");