1 // SPDX-License-Identifier: GPL-2.0-or-later
3 CA-driver for TwinHan DST Frontend/Card
5 Copyright (C) 2004, 2005 Manu Abraham (manu@kromtek.com)
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/mutex.h>
14 #include <linux/string.h>
15 #include <linux/dvb/ca.h>
16 #include <media/dvbdev.h>
17 #include <media/dvb_frontend.h>
19 #include "dst_common.h"
21 #define DST_CA_ERROR 0
22 #define DST_CA_NOTICE 1
24 #define DST_CA_DEBUG 3
26 #define dprintk(x, y, z, format, arg...) do { \
28 if ((x > DST_CA_ERROR) && (x > y)) \
29 printk(KERN_ERR "%s: " format "\n", __func__ , ##arg); \
30 else if ((x > DST_CA_NOTICE) && (x > y)) \
31 printk(KERN_NOTICE "%s: " format "\n", __func__ , ##arg); \
32 else if ((x > DST_CA_INFO) && (x > y)) \
33 printk(KERN_INFO "%s: " format "\n", __func__ , ##arg); \
34 else if ((x > DST_CA_DEBUG) && (x > y)) \
35 printk(KERN_DEBUG "%s: " format "\n", __func__ , ##arg); \
38 printk(format, ## arg); \
43 static DEFINE_MUTEX(dst_ca_mutex);
44 static unsigned int verbose = 5;
45 module_param(verbose, int, 0644);
46 MODULE_PARM_DESC(verbose, "verbose startup messages, default is 1 (yes)");
48 static void put_command_and_length(u8 *data, int command, int length)
50 data[0] = (command >> 16) & 0xff;
51 data[1] = (command >> 8) & 0xff;
52 data[2] = command & 0xff;
56 static void put_checksum(u8 *check_string, int length)
58 dprintk(verbose, DST_CA_DEBUG, 1, " Computing string checksum.");
59 dprintk(verbose, DST_CA_DEBUG, 1, " -> string length : 0x%02x", length);
60 check_string[length] = dst_check_sum (check_string, length);
61 dprintk(verbose, DST_CA_DEBUG, 1, " -> checksum : 0x%02x", check_string[length]);
64 static int dst_ci_command(struct dst_state* state, u8 * data, u8 *ca_string, u8 len, int read)
68 mutex_lock(&state->dst_mutex);
72 if (write_dst(state, data, len)) {
73 dprintk(verbose, DST_CA_INFO, 1, " Write not successful, trying to recover");
74 dst_error_recovery(state);
77 if ((dst_pio_disable(state)) < 0) {
78 dprintk(verbose, DST_CA_ERROR, 1, " DST PIO disable failed.");
81 if (read_dst(state, &reply, GET_ACK) < 0) {
82 dprintk(verbose, DST_CA_INFO, 1, " Read not successful, trying to recover");
83 dst_error_recovery(state);
87 if (! dst_wait_dst_ready(state, LONG_DELAY)) {
88 dprintk(verbose, DST_CA_NOTICE, 1, " 8820 not ready");
91 if (read_dst(state, ca_string, 128) < 0) { /* Try to make this dynamic */
92 dprintk(verbose, DST_CA_INFO, 1, " Read not successful, trying to recover");
93 dst_error_recovery(state);
97 mutex_unlock(&state->dst_mutex);
101 mutex_unlock(&state->dst_mutex);
106 static int dst_put_ci(struct dst_state *state, u8 *data, int len, u8 *ca_string, int read)
108 u8 dst_ca_comm_err = 0;
110 while (dst_ca_comm_err < RETRIES) {
111 dprintk(verbose, DST_CA_NOTICE, 1, " Put Command");
112 if (dst_ci_command(state, data, ca_string, len, read)) { // If error
113 dst_error_recovery(state);
114 dst_ca_comm_err++; // work required here.
120 if(dst_ca_comm_err == RETRIES)
128 static int ca_get_app_info(struct dst_state *state)
130 int length, str_length;
131 static u8 command[8] = {0x07, 0x40, 0x01, 0x00, 0x01, 0x00, 0x00, 0xff};
133 put_checksum(&command[0], command[0]);
134 if ((dst_put_ci(state, command, sizeof(command), state->messages, GET_REPLY)) < 0) {
135 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
138 dprintk(verbose, DST_CA_INFO, 1, " -->dst_put_ci SUCCESS !");
139 dprintk(verbose, DST_CA_INFO, 1, " ================================ CI Module Application Info ======================================");
140 dprintk(verbose, DST_CA_INFO, 1, " Application Type=[%d], Application Vendor=[%d], Vendor Code=[%d]\n%s: Application info=[%s]",
141 state->messages[7], (state->messages[8] << 8) | state->messages[9],
142 (state->messages[10] << 8) | state->messages[11], __func__, (char *)(&state->messages[12]));
143 dprintk(verbose, DST_CA_INFO, 1, " ==================================================================================================");
145 // Transform dst message to correct application_info message
146 length = state->messages[5];
147 str_length = length - 6;
148 if (str_length < 0) {
150 dprintk(verbose, DST_CA_ERROR, 1, "Invalid string length returned in ca_get_app_info(). Recovering.");
153 // First, the command and length fields
154 put_command_and_length(&state->messages[0], CA_APP_INFO, length);
156 // Copy application_type, application_manufacturer and manufacturer_code
157 memmove(&state->messages[4], &state->messages[7], 5);
159 // Set string length and copy string
160 state->messages[9] = str_length;
161 memmove(&state->messages[10], &state->messages[12], str_length);
166 static int ca_get_ca_info(struct dst_state *state)
168 int srcPtr, dstPtr, i, num_ids;
169 static u8 slot_command[8] = {0x07, 0x40, 0x00, 0x00, 0x02, 0x00, 0x00, 0xff};
170 const int in_system_id_pos = 8, out_system_id_pos = 4, in_num_ids_pos = 7;
172 put_checksum(&slot_command[0], slot_command[0]);
173 if ((dst_put_ci(state, slot_command, sizeof (slot_command), state->messages, GET_REPLY)) < 0) {
174 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
177 dprintk(verbose, DST_CA_INFO, 1, " -->dst_put_ci SUCCESS !");
180 dprintk(verbose, DST_CA_INFO, 0, " DST data = [");
181 for (i = 0; i < state->messages[0] + 1; i++) {
182 dprintk(verbose, DST_CA_INFO, 0, " 0x%02x", state->messages[i]);
184 dprintk(verbose, DST_CA_INFO, 0, "]\n");
186 // Set the command and length of the output
187 num_ids = state->messages[in_num_ids_pos];
188 if (num_ids >= 100) {
190 dprintk(verbose, DST_CA_ERROR, 1, "Invalid number of ids (>100). Recovering.");
192 put_command_and_length(&state->messages[0], CA_INFO, num_ids * 2);
194 dprintk(verbose, DST_CA_INFO, 0, " CA_INFO = [");
195 srcPtr = in_system_id_pos;
196 dstPtr = out_system_id_pos;
197 for(i = 0; i < num_ids; i++) {
198 dprintk(verbose, DST_CA_INFO, 0, " 0x%02x%02x", state->messages[srcPtr + 0], state->messages[srcPtr + 1]);
200 state->messages[dstPtr + 0] = state->messages[srcPtr + 0];
201 state->messages[dstPtr + 1] = state->messages[srcPtr + 1];
205 dprintk(verbose, DST_CA_INFO, 0, "]\n");
210 static int ca_get_slot_caps(struct dst_state *state, struct ca_caps *p_ca_caps, void __user *arg)
214 static u8 slot_command[8] = {0x07, 0x40, 0x02, 0x00, 0x02, 0x00, 0x00, 0xff};
216 put_checksum(&slot_command[0], slot_command[0]);
217 if ((dst_put_ci(state, slot_command, sizeof (slot_command), slot_cap, GET_REPLY)) < 0) {
218 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
221 dprintk(verbose, DST_CA_NOTICE, 1, " -->dst_put_ci SUCCESS !");
223 /* Will implement the rest soon */
225 dprintk(verbose, DST_CA_INFO, 1, " Slot cap = [%d]", slot_cap[7]);
226 dprintk(verbose, DST_CA_INFO, 0, "===================================\n");
227 for (i = 0; i < slot_cap[0] + 1; i++)
228 dprintk(verbose, DST_CA_INFO, 0, " %d", slot_cap[i]);
229 dprintk(verbose, DST_CA_INFO, 0, "\n");
231 p_ca_caps->slot_num = 1;
232 p_ca_caps->slot_type = 1;
233 p_ca_caps->descr_num = slot_cap[7];
234 p_ca_caps->descr_type = 1;
236 if (copy_to_user(arg, p_ca_caps, sizeof (struct ca_caps)))
242 /* Need some more work */
243 static int ca_get_slot_descr(struct dst_state *state, struct ca_msg *p_ca_message, void __user *arg)
249 static int ca_get_slot_info(struct dst_state *state, struct ca_slot_info *p_ca_slot_info, void __user *arg)
252 static u8 slot_command[8] = {0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff};
254 u8 *slot_info = state->messages;
256 put_checksum(&slot_command[0], 7);
257 if ((dst_put_ci(state, slot_command, sizeof (slot_command), slot_info, GET_REPLY)) < 0) {
258 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
261 dprintk(verbose, DST_CA_INFO, 1, " -->dst_put_ci SUCCESS !");
263 /* Will implement the rest soon */
265 dprintk(verbose, DST_CA_INFO, 1, " Slot info = [%d]", slot_info[3]);
266 dprintk(verbose, DST_CA_INFO, 0, "===================================\n");
267 for (i = 0; i < 8; i++)
268 dprintk(verbose, DST_CA_INFO, 0, " %d", slot_info[i]);
269 dprintk(verbose, DST_CA_INFO, 0, "\n");
271 if (slot_info[4] & 0x80) {
272 p_ca_slot_info->flags = CA_CI_MODULE_PRESENT;
273 p_ca_slot_info->num = 1;
274 p_ca_slot_info->type = CA_CI;
275 } else if (slot_info[4] & 0x40) {
276 p_ca_slot_info->flags = CA_CI_MODULE_READY;
277 p_ca_slot_info->num = 1;
278 p_ca_slot_info->type = CA_CI;
280 p_ca_slot_info->flags = 0;
282 if (copy_to_user(arg, p_ca_slot_info, sizeof (struct ca_slot_info)))
289 static int ca_get_message(struct dst_state *state, struct ca_msg *p_ca_message, void __user *arg)
294 if (copy_from_user(p_ca_message, arg, sizeof (struct ca_msg)))
297 dprintk(verbose, DST_CA_NOTICE, 1, " Message = [%*ph]",
298 3, p_ca_message->msg);
300 for (i = 0; i < 3; i++) {
301 command = command | p_ca_message->msg[i];
303 command = command << 8;
305 dprintk(verbose, DST_CA_NOTICE, 1, " Command=[0x%x]", command);
309 memcpy(p_ca_message->msg, state->messages, 128);
310 if (copy_to_user(arg, p_ca_message, sizeof (struct ca_msg)) )
314 memcpy(p_ca_message->msg, state->messages, 128);
315 if (copy_to_user(arg, p_ca_message, sizeof (struct ca_msg)) )
323 static int handle_dst_tag(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer, u32 length)
325 if (state->dst_hw_cap & DST_TYPE_HAS_SESSION) {
326 hw_buffer->msg[2] = p_ca_message->msg[1]; /* MSB */
327 hw_buffer->msg[3] = p_ca_message->msg[2]; /* LSB */
330 dprintk(verbose, DST_CA_ERROR, 1, " Message too long ! *** Bailing Out *** !");
333 hw_buffer->msg[0] = (length & 0xff) + 7;
334 hw_buffer->msg[1] = 0x40;
335 hw_buffer->msg[2] = 0x03;
336 hw_buffer->msg[3] = 0x00;
337 hw_buffer->msg[4] = 0x03;
338 hw_buffer->msg[5] = length & 0xff;
339 hw_buffer->msg[6] = 0x00;
342 * Need to compute length for EN50221 section 8.3.2, for the time being
343 * assuming 8.3.2 is not applicable
345 memcpy(&hw_buffer->msg[7], &p_ca_message->msg[4], length);
351 static int write_to_8820(struct dst_state *state, struct ca_msg *hw_buffer, u8 length, u8 reply)
353 if ((dst_put_ci(state, hw_buffer->msg, length, hw_buffer->msg, reply)) < 0) {
354 dprintk(verbose, DST_CA_ERROR, 1, " DST-CI Command failed.");
355 dprintk(verbose, DST_CA_NOTICE, 1, " Resetting DST.");
356 rdc_reset_state(state);
359 dprintk(verbose, DST_CA_NOTICE, 1, " DST-CI Command success.");
364 static u32 asn_1_decode(u8 *asn_1_array)
366 u8 length_field = 0, word_count = 0, count = 0;
369 length_field = asn_1_array[0];
370 dprintk(verbose, DST_CA_DEBUG, 1, " Length field=[%02x]", length_field);
371 if (length_field < 0x80) {
372 length = length_field & 0x7f;
373 dprintk(verbose, DST_CA_DEBUG, 1, " Length=[%02x]\n", length);
375 word_count = length_field & 0x7f;
376 for (count = 0; count < word_count; count++) {
377 length = length << 8;
378 length += asn_1_array[count + 1];
379 dprintk(verbose, DST_CA_DEBUG, 1, " Length=[%04x]", length);
385 static int debug_string(u8 *msg, u32 length, u32 offset)
389 dprintk(verbose, DST_CA_DEBUG, 0, " String=[ ");
390 for (i = offset; i < length; i++)
391 dprintk(verbose, DST_CA_DEBUG, 0, "%02x ", msg[i]);
392 dprintk(verbose, DST_CA_DEBUG, 0, "]\n");
398 static int ca_set_pmt(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer, u8 reply, u8 query)
403 length = asn_1_decode(&p_ca_message->msg[3]);
404 dprintk(verbose, DST_CA_DEBUG, 1, " CA Message length=[%d]", length);
405 debug_string(&p_ca_message->msg[4], length, 0); /* length is excluding tag & length */
407 memset(hw_buffer->msg, '\0', length);
408 handle_dst_tag(state, p_ca_message, hw_buffer, length);
409 put_checksum(hw_buffer->msg, hw_buffer->msg[0]);
411 debug_string(hw_buffer->msg, (length + tag_length), 0); /* tags too */
412 write_to_8820(state, hw_buffer, (length + tag_length), reply);
418 /* Board supports CA PMT reply ? */
419 static int dst_check_ca_pmt(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer)
421 int ca_pmt_reply_test = 0;
424 /* Not there yet but soon */
426 /* CA PMT Reply capable */
427 if (ca_pmt_reply_test) {
428 if ((ca_set_pmt(state, p_ca_message, hw_buffer, 1, GET_REPLY)) < 0) {
429 dprintk(verbose, DST_CA_ERROR, 1, " ca_set_pmt.. failed !");
433 /* Process CA PMT Reply */
434 /* will implement soon */
435 dprintk(verbose, DST_CA_ERROR, 1, " Not there yet");
437 /* CA PMT Reply not capable */
438 if (!ca_pmt_reply_test) {
439 if ((ca_set_pmt(state, p_ca_message, hw_buffer, 0, NO_REPLY)) < 0) {
440 dprintk(verbose, DST_CA_ERROR, 1, " ca_set_pmt.. failed !");
443 dprintk(verbose, DST_CA_NOTICE, 1, " ca_set_pmt.. success !");
444 /* put a dummy message */
450 static int ca_send_message(struct dst_state *state, struct ca_msg *p_ca_message, void __user *arg)
454 struct ca_msg *hw_buffer;
457 hw_buffer = kmalloc(sizeof(*hw_buffer), GFP_KERNEL);
460 dprintk(verbose, DST_CA_DEBUG, 1, " ");
462 if (copy_from_user(p_ca_message, arg, sizeof (struct ca_msg))) {
464 goto free_mem_and_exit;
470 for (i = 0; i < 3; i++) {
471 command = command | p_ca_message->msg[i];
473 command = command << 8;
475 dprintk(verbose, DST_CA_DEBUG, 1, " Command=[0x%x]\n", command);
479 dprintk(verbose, DST_CA_DEBUG, 1, "Command = SEND_CA_PMT");
480 if ((ca_set_pmt(state, p_ca_message, hw_buffer, 0, 0)) < 0) { // code simplification started
481 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_PMT Failed !");
483 goto free_mem_and_exit;
485 dprintk(verbose, DST_CA_INFO, 1, " -->CA_PMT Success !");
488 dprintk(verbose, DST_CA_INFO, 1, "Command = CA_PMT_REPLY");
489 /* Have to handle the 2 basic types of cards here */
490 if ((dst_check_ca_pmt(state, p_ca_message, hw_buffer)) < 0) {
491 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_PMT_REPLY Failed !");
493 goto free_mem_and_exit;
495 dprintk(verbose, DST_CA_INFO, 1, " -->CA_PMT_REPLY Success !");
497 case CA_APP_INFO_ENQUIRY: // only for debugging
498 dprintk(verbose, DST_CA_INFO, 1, " Getting Cam Application information");
500 if ((ca_get_app_info(state)) < 0) {
501 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_APP_INFO_ENQUIRY Failed !");
503 goto free_mem_and_exit;
505 dprintk(verbose, DST_CA_INFO, 1, " -->CA_APP_INFO_ENQUIRY Success !");
507 case CA_INFO_ENQUIRY:
508 dprintk(verbose, DST_CA_INFO, 1, " Getting CA Information");
510 if ((ca_get_ca_info(state)) < 0) {
511 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_INFO_ENQUIRY Failed !");
513 goto free_mem_and_exit;
515 dprintk(verbose, DST_CA_INFO, 1, " -->CA_INFO_ENQUIRY Success !");
525 static long dst_ca_ioctl(struct file *file, unsigned int cmd, unsigned long ioctl_arg)
527 struct dvb_device *dvbdev;
528 struct dst_state *state;
529 struct ca_slot_info *p_ca_slot_info;
530 struct ca_caps *p_ca_caps;
531 struct ca_msg *p_ca_message;
532 void __user *arg = (void __user *)ioctl_arg;
535 mutex_lock(&dst_ca_mutex);
536 dvbdev = file->private_data;
537 state = (struct dst_state *)dvbdev->priv;
538 p_ca_message = kmalloc(sizeof (struct ca_msg), GFP_KERNEL);
539 p_ca_slot_info = kmalloc(sizeof (struct ca_slot_info), GFP_KERNEL);
540 p_ca_caps = kmalloc(sizeof (struct ca_caps), GFP_KERNEL);
541 if (!p_ca_message || !p_ca_slot_info || !p_ca_caps) {
543 goto free_mem_and_exit;
546 /* We have now only the standard ioctl's, the driver is upposed to handle internals. */
549 dprintk(verbose, DST_CA_INFO, 1, " Sending message");
550 result = ca_send_message(state, p_ca_message, arg);
553 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_SEND_MSG Failed !");
554 goto free_mem_and_exit;
558 dprintk(verbose, DST_CA_INFO, 1, " Getting message");
559 result = ca_get_message(state, p_ca_message, arg);
561 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_MSG Failed !");
562 goto free_mem_and_exit;
564 dprintk(verbose, DST_CA_INFO, 1, " -->CA_GET_MSG Success !");
567 dprintk(verbose, DST_CA_ERROR, 1, " Resetting DST");
568 dst_error_bailout(state);
571 case CA_GET_SLOT_INFO:
572 dprintk(verbose, DST_CA_INFO, 1, " Getting Slot info");
573 result = ca_get_slot_info(state, p_ca_slot_info, arg);
575 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_SLOT_INFO Failed !");
577 goto free_mem_and_exit;
579 dprintk(verbose, DST_CA_INFO, 1, " -->CA_GET_SLOT_INFO Success !");
582 dprintk(verbose, DST_CA_INFO, 1, " Getting Slot capabilities");
583 result = ca_get_slot_caps(state, p_ca_caps, arg);
585 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_CAP Failed !");
586 goto free_mem_and_exit;
588 dprintk(verbose, DST_CA_INFO, 1, " -->CA_GET_CAP Success !");
590 case CA_GET_DESCR_INFO:
591 dprintk(verbose, DST_CA_INFO, 1, " Getting descrambler description");
592 result = ca_get_slot_descr(state, p_ca_message, arg);
594 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_DESCR_INFO Failed !");
595 goto free_mem_and_exit;
597 dprintk(verbose, DST_CA_INFO, 1, " -->CA_GET_DESCR_INFO Success !");
600 result = -EOPNOTSUPP;
603 kfree (p_ca_message);
604 kfree (p_ca_slot_info);
607 mutex_unlock(&dst_ca_mutex);
611 static int dst_ca_open(struct inode *inode, struct file *file)
613 dprintk(verbose, DST_CA_DEBUG, 1, " Device opened [%p] ", file);
618 static int dst_ca_release(struct inode *inode, struct file *file)
620 dprintk(verbose, DST_CA_DEBUG, 1, " Device closed.");
625 static ssize_t dst_ca_read(struct file *file, char __user *buffer, size_t length, loff_t *offset)
627 dprintk(verbose, DST_CA_DEBUG, 1, " Device read.");
632 static ssize_t dst_ca_write(struct file *file, const char __user *buffer, size_t length, loff_t *offset)
634 dprintk(verbose, DST_CA_DEBUG, 1, " Device write.");
639 static const struct file_operations dst_ca_fops = {
640 .owner = THIS_MODULE,
641 .unlocked_ioctl = dst_ca_ioctl,
643 .release = dst_ca_release,
645 .write = dst_ca_write,
646 .llseek = noop_llseek,
649 static struct dvb_device dvbdev_ca = {
657 struct dvb_device *dst_ca_attach(struct dst_state *dst, struct dvb_adapter *dvb_adapter)
659 struct dvb_device *dvbdev;
661 dprintk(verbose, DST_CA_ERROR, 1, "registering DST-CA device");
662 if (dvb_register_device(dvb_adapter, &dvbdev, &dvbdev_ca, dst,
663 DVB_DEVICE_CA, 0) == 0) {
664 dst->dst_ca = dvbdev;
671 EXPORT_SYMBOL(dst_ca_attach);
673 MODULE_DESCRIPTION("DST DVB-S/T/C Combo CA driver");
674 MODULE_AUTHOR("Manu Abraham");
675 MODULE_LICENSE("GPL");