V4L/DVB (4182): Comment out MMI functions for now
[pandora-kernel.git] / drivers / media / dvb / bt8xx / dst_ca.c
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 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/string.h>
25 #include <linux/dvb/ca.h>
26 #include "dvbdev.h"
27 #include "dvb_frontend.h"
28 #include "dst_ca.h"
29 #include "dst_common.h"
30
31 #define DST_CA_ERROR            0
32 #define DST_CA_NOTICE           1
33 #define DST_CA_INFO             2
34 #define DST_CA_DEBUG            3
35
36 #define dprintk(x, y, z, format, arg...) do {                                           \
37         if (z) {                                                                        \
38                 if      ((x > DST_CA_ERROR) && (x > y))                                 \
39                         printk(KERN_ERR "%s: " format "\n", __FUNCTION__ , ##arg);      \
40                 else if ((x > DST_CA_NOTICE) && (x > y))                                \
41                         printk(KERN_NOTICE "%s: " format "\n", __FUNCTION__ , ##arg);   \
42                 else if ((x > DST_CA_INFO) && (x > y))                                  \
43                         printk(KERN_INFO "%s: " format "\n", __FUNCTION__ , ##arg);     \
44                 else if ((x > DST_CA_DEBUG) && (x > y))                                 \
45                         printk(KERN_DEBUG "%s: " format "\n", __FUNCTION__ , ##arg);    \
46         } else {                                                                        \
47                 if (x > y)                                                              \
48                         printk(format, ## arg);                                         \
49         }                                                                               \
50 } while(0)
51
52
53 static unsigned int verbose = 5;
54 module_param(verbose, int, 0644);
55 MODULE_PARM_DESC(verbose, "verbose startup messages, default is 1 (yes)");
56
57 /*      Need some more work     */
58 static int ca_set_slot_descr(void)
59 {
60         /*      We could make this more graceful ?      */
61         return -EOPNOTSUPP;
62 }
63
64 /*      Need some more work     */
65 static int ca_set_pid(void)
66 {
67         /*      We could make this more graceful ?      */
68         return -EOPNOTSUPP;
69 }
70
71 static void put_command_and_length(u8 *data, int command, int length)
72 {
73         data[0] = (command >> 16) & 0xff;
74         data[1] = (command >> 8) & 0xff;
75         data[2] = command & 0xff;
76         data[3] = length;
77 }
78
79 static void put_checksum(u8 *check_string, int length)
80 {
81         dprintk(verbose, DST_CA_DEBUG, 1, " Computing string checksum.");
82         dprintk(verbose, DST_CA_DEBUG, 1, "  -> string length : 0x%02x", length);
83         check_string[length] = dst_check_sum (check_string, length);
84         dprintk(verbose, DST_CA_DEBUG, 1, "  -> checksum      : 0x%02x", check_string[length]);
85 }
86
87 static int dst_ci_command(struct dst_state* state, u8 * data, u8 *ca_string, u8 len, int read)
88 {
89         u8 reply;
90
91         mutex_lock(&state->dst_mutex);
92         dst_comm_init(state);
93         msleep(65);
94
95         if (write_dst(state, data, len)) {
96                 dprintk(verbose, DST_CA_INFO, 1, " Write not successful, trying to recover");
97                 dst_error_recovery(state);
98                 goto error;
99         }
100         if ((dst_pio_disable(state)) < 0) {
101                 dprintk(verbose, DST_CA_ERROR, 1, " DST PIO disable failed.");
102                 goto error;
103         }
104         if (read_dst(state, &reply, GET_ACK) < 0) {
105                 dprintk(verbose, DST_CA_INFO, 1, " Read not successful, trying to recover");
106                 dst_error_recovery(state);
107                 goto error;
108         }
109         if (read) {
110                 if (! dst_wait_dst_ready(state, LONG_DELAY)) {
111                         dprintk(verbose, DST_CA_NOTICE, 1, " 8820 not ready");
112                         goto error;
113                 }
114                 if (read_dst(state, ca_string, 128) < 0) {      /*      Try to make this dynamic        */
115                         dprintk(verbose, DST_CA_INFO, 1, " Read not successful, trying to recover");
116                         dst_error_recovery(state);
117                         goto error;
118                 }
119         }
120         mutex_unlock(&state->dst_mutex);
121         return 0;
122
123 error:
124         mutex_unlock(&state->dst_mutex);
125         return -EIO;
126 }
127
128
129 static int dst_put_ci(struct dst_state *state, u8 *data, int len, u8 *ca_string, int read)
130 {
131         u8 dst_ca_comm_err = 0;
132
133         while (dst_ca_comm_err < RETRIES) {
134                 dprintk(verbose, DST_CA_NOTICE, 1, " Put Command");
135                 if (dst_ci_command(state, data, ca_string, len, read)) {        // If error
136                         dst_error_recovery(state);
137                         dst_ca_comm_err++; // work required here.
138                 } else {
139                         break;
140                 }
141         }
142
143         if(dst_ca_comm_err == RETRIES)
144                 return -1;
145
146         return 0;
147 }
148
149
150
151 static int ca_get_app_info(struct dst_state *state)
152 {
153         int length, str_length;
154         static u8 command[8] = {0x07, 0x40, 0x01, 0x00, 0x01, 0x00, 0x00, 0xff};
155
156         put_checksum(&command[0], command[0]);
157         if ((dst_put_ci(state, command, sizeof(command), state->messages, GET_REPLY)) < 0) {
158                 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
159                 return -1;
160         }
161         dprintk(verbose, DST_CA_INFO, 1, " -->dst_put_ci SUCCESS !");
162         dprintk(verbose, DST_CA_INFO, 1, " ================================ CI Module Application Info ======================================");
163         dprintk(verbose, DST_CA_INFO, 1, " Application Type=[%d], Application Vendor=[%d], Vendor Code=[%d]\n%s: Application info=[%s]",
164                 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(verbose, DST_CA_INFO, 1, " ==================================================================================================");
167
168         // Transform dst message to correct application_info message
169         length = state->messages[5];
170         str_length = length - 6;
171         if (str_length < 0) {
172                 str_length = 0;
173                 dprintk(verbose, DST_CA_ERROR, 1, "Invalid string length returned in ca_get_app_info(). Recovering.");
174         }
175
176         // First, the command and length fields
177         put_command_and_length(&state->messages[0], CA_APP_INFO, length);
178
179         // Copy application_type, application_manufacturer and manufacturer_code
180         memcpy(&state->messages[4], &state->messages[7], 5);
181
182         // Set string length and copy string
183         state->messages[9] = str_length;
184         memcpy(&state->messages[10], &state->messages[12], str_length);
185
186         return 0;
187 }
188
189 static int ca_get_ca_info(struct dst_state *state)
190 {
191         int srcPtr, dstPtr, i, num_ids;
192         static u8 slot_command[8] = {0x07, 0x40, 0x00, 0x00, 0x02, 0x00, 0x00, 0xff};
193         const int in_system_id_pos = 8, out_system_id_pos = 4, in_num_ids_pos = 7;
194
195         put_checksum(&slot_command[0], slot_command[0]);
196         if ((dst_put_ci(state, slot_command, sizeof (slot_command), state->messages, GET_REPLY)) < 0) {
197                 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
198                 return -1;
199         }
200         dprintk(verbose, DST_CA_INFO, 1, " -->dst_put_ci SUCCESS !");
201
202         // Print raw data
203         dprintk(verbose, DST_CA_INFO, 0, " DST data = [");
204         for (i = 0; i < state->messages[0] + 1; i++) {
205                 dprintk(verbose, DST_CA_INFO, 0, " 0x%02x", state->messages[i]);
206         }
207         dprintk(verbose, DST_CA_INFO, 0, "]\n");
208
209         // Set the command and length of the output
210         num_ids = state->messages[in_num_ids_pos];
211         if (num_ids >= 100) {
212                 num_ids = 100;
213                 dprintk(verbose, DST_CA_ERROR, 1, "Invalid number of ids (>100). Recovering.");
214         }
215         put_command_and_length(&state->messages[0], CA_INFO, num_ids * 2);
216
217         dprintk(verbose, DST_CA_INFO, 0, " CA_INFO = [");
218         srcPtr = in_system_id_pos;
219         dstPtr = out_system_id_pos;
220         for(i = 0; i < num_ids; i++) {
221                 dprintk(verbose, DST_CA_INFO, 0, " 0x%02x%02x", state->messages[srcPtr + 0], state->messages[srcPtr + 1]);
222                 // Append to output
223                 state->messages[dstPtr + 0] = state->messages[srcPtr + 0];
224                 state->messages[dstPtr + 1] = state->messages[srcPtr + 1];
225                 srcPtr += 2;
226                 dstPtr += 2;
227         }
228         dprintk(verbose, DST_CA_INFO, 0, "]\n");
229
230         return 0;
231 }
232
233 static int ca_get_slot_caps(struct dst_state *state, struct ca_caps *p_ca_caps, void __user *arg)
234 {
235         int i;
236         u8 slot_cap[256];
237         static u8 slot_command[8] = {0x07, 0x40, 0x02, 0x00, 0x02, 0x00, 0x00, 0xff};
238
239         put_checksum(&slot_command[0], slot_command[0]);
240         if ((dst_put_ci(state, slot_command, sizeof (slot_command), slot_cap, GET_REPLY)) < 0) {
241                 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
242                 return -1;
243         }
244         dprintk(verbose, DST_CA_NOTICE, 1, " -->dst_put_ci SUCCESS !");
245
246         /*      Will implement the rest soon            */
247
248         dprintk(verbose, DST_CA_INFO, 1, " Slot cap = [%d]", slot_cap[7]);
249         dprintk(verbose, DST_CA_INFO, 0, "===================================\n");
250         for (i = 0; i < slot_cap[0] + 1; i++)
251                 dprintk(verbose, DST_CA_INFO, 0, " %d", slot_cap[i]);
252         dprintk(verbose, DST_CA_INFO, 0, "\n");
253
254         p_ca_caps->slot_num = 1;
255         p_ca_caps->slot_type = 1;
256         p_ca_caps->descr_num = slot_cap[7];
257         p_ca_caps->descr_type = 1;
258
259         if (copy_to_user(arg, p_ca_caps, sizeof (struct ca_caps)))
260                 return -EFAULT;
261
262         return 0;
263 }
264
265 /*      Need some more work     */
266 static int ca_get_slot_descr(struct dst_state *state, struct ca_msg *p_ca_message, void __user *arg)
267 {
268         return -EOPNOTSUPP;
269 }
270
271
272 static int ca_get_slot_info(struct dst_state *state, struct ca_slot_info *p_ca_slot_info, void __user *arg)
273 {
274         int i;
275         static u8 slot_command[8] = {0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff};
276
277         u8 *slot_info = state->messages;
278
279         put_checksum(&slot_command[0], 7);
280         if ((dst_put_ci(state, slot_command, sizeof (slot_command), slot_info, GET_REPLY)) < 0) {
281                 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
282                 return -1;
283         }
284         dprintk(verbose, DST_CA_INFO, 1, " -->dst_put_ci SUCCESS !");
285
286         /*      Will implement the rest soon            */
287
288         dprintk(verbose, DST_CA_INFO, 1, " Slot info = [%d]", slot_info[3]);
289         dprintk(verbose, DST_CA_INFO, 0, "===================================\n");
290         for (i = 0; i < 8; i++)
291                 dprintk(verbose, DST_CA_INFO, 0, " %d", slot_info[i]);
292         dprintk(verbose, DST_CA_INFO, 0, "\n");
293
294         if (slot_info[4] & 0x80) {
295                 p_ca_slot_info->flags = CA_CI_MODULE_PRESENT;
296                 p_ca_slot_info->num = 1;
297                 p_ca_slot_info->type = CA_CI;
298         } else if (slot_info[4] & 0x40) {
299                 p_ca_slot_info->flags = CA_CI_MODULE_READY;
300                 p_ca_slot_info->num = 1;
301                 p_ca_slot_info->type = CA_CI;
302         } else
303                 p_ca_slot_info->flags = 0;
304
305         if (copy_to_user(arg, p_ca_slot_info, sizeof (struct ca_slot_info)))
306                 return -EFAULT;
307
308         return 0;
309 }
310
311 /*      MMI     */
312 static int ca_get_mmi(struct dst_state *state, struct ca_msg *hw_msg, struct ca_msg *mmi_msg)
313 {
314         static u8 get_mmi[] = { 0x07, 0x40, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x0f };
315
316         put_checksum(&get_mmi[0], 7);
317         if ((dst_put_ci(state, get_mmi, sizeof (get_mmi), hw_msg->msg, GET_REPLY)) < 0) {
318                 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
319                 return -1;
320         }
321         dprintk(verbose, DST_CA_NOTICE, 1, " -->dst_put_ci SUCCESS !");
322         memcpy(mmi_msg->msg, hw_msg->msg, hw_msg->msg[4]);
323
324         return 0;
325 }
326
327 /**
328  *      Get Menu should be the first MMI function (like open !)
329  */
330 static int ca_get_menu(struct dst_state *state)
331 {
332         static u8 get_menu[] = { 0x07, 0x40, 0x00, 0x00, 0x09, 0x00, 0x00, 0xff };
333
334         put_checksum(&get_menu[0], 7);
335         if ((dst_put_ci(state, get_menu, sizeof (get_menu), get_menu, NO_REPLY)) < 0) {
336                 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
337                 return -1;
338         }
339         dprintk(verbose, DST_CA_NOTICE, 1, " -->dst_put_ci SUCCESS !");
340
341         return 0;
342 }
343
344 /**
345  *      MMI Enq (Enquire the application to allow user input)
346  */
347 static int ca_answer_menu(struct dst_state *state, struct ca_msg *hw_msg, struct ca_msg *menu_answ)
348 {
349         u8 choice = 0;
350
351         static u8 answer_menu[] = { 0x08, 0x40, 0x00, 0x00, 0x0b, 0x01, 0x00, 0x01, 0xff };
352
353         /*      derive answer from menu (This comes from the user)      */
354         answer_menu[7] = choice;
355         put_checksum(&answer_menu[0], 7);
356         if ((dst_put_ci(state, answer_menu, sizeof (answer_menu), hw_msg->msg, NO_REPLY)) < 0) {
357                 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
358                 return -1;
359         }
360         dprintk(verbose, DST_CA_NOTICE, 1, " -->dst_put_ci SUCCESS !");
361
362         return 0;
363 }
364
365 static int ca_answer_mmi(struct dst_state *state, struct ca_msg *hw_msg, struct ca_msg *answ_msg)
366 {
367         u8 answer =0, length = 0;
368
369         static u8 answer_mmi[] = { 0x08, 0x40, 0x00, 0x00, 0x08, 0x01, 0x00, 0x01, 0xff };
370
371         /*      derive answer from answ_msg (This comes from the user)  */
372         if (answer == 0)        /*      0x00 == Cancel  */
373                 answer_mmi[7] = 0x00;
374         else {                  /*      0x01 == Answer  */
375                 length = strlen(answ_msg->msg);
376                 memcpy(&answer_mmi[8], answ_msg->msg, length);
377                 answer_mmi[0] += length;
378                 answer_mmi[5] += length;
379         }
380         put_checksum(&answer_mmi[0], (8 + length));
381         if ((dst_put_ci(state, answer_mmi, sizeof (answer_mmi), hw_msg->msg, GET_REPLY)) < 0) {
382                 dprintk(verbose, DST_CA_NOTICE, 1, " -->dst_put_ci FAILED !");
383
384                 return -1;
385         }
386         dprintk(verbose, DST_CA_NOTICE, 1, " -->dst_put_ci SUCCESS !");
387
388         return 0;
389 }
390
391 static int ca_close_mmi(struct dst_state *state, struct ca_msg *hw_msg)
392 {
393         static u8 close_mmi[] = { 0x07, 0x40, 0x00, 0x00, 0x0e, 0x00, 0x00, 0xff };
394
395         put_checksum(&close_mmi[0], 7);
396         if ((dst_put_ci(state, close_mmi, sizeof (close_mmi), hw_msg->msg, NO_REPLY)) < 0) {
397                 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
398
399                 return -1;
400         }
401         dprintk(verbose, DST_CA_NOTICE, 1, " -->dst_put_ci SUCCESS !");
402
403         return 0;
404 }
405
406 static int ca_get_message(struct dst_state *state, struct ca_msg *p_ca_message, void __user *arg)
407 {
408         u8 i = 0;
409         u32 command = 0;
410
411         if (copy_from_user(p_ca_message, arg, sizeof (struct ca_msg)))
412                 return -EFAULT;
413
414         if (p_ca_message->msg) {
415                 dprintk(verbose, DST_CA_NOTICE, 1, " Message = [%02x %02x %02x]", p_ca_message->msg[0], p_ca_message->msg[1], p_ca_message->msg[2]);
416
417                 for (i = 0; i < 3; i++) {
418                         command = command | p_ca_message->msg[i];
419                         if (i < 2)
420                                 command = command << 8;
421                 }
422                 dprintk(verbose, DST_CA_NOTICE, 1, " Command=[0x%x]", command);
423
424                 switch (command) {
425                 case CA_APP_INFO:
426                         memcpy(p_ca_message->msg, state->messages, 128);
427                         if (copy_to_user(arg, p_ca_message, sizeof (struct ca_msg)) )
428                                 return -EFAULT;
429                         break;
430                 case CA_INFO:
431                         memcpy(p_ca_message->msg, state->messages, 128);
432                         if (copy_to_user(arg, p_ca_message, sizeof (struct ca_msg)) )
433                                 return -EFAULT;
434                         break;
435                 }
436         }
437
438         return 0;
439 }
440
441 static int handle_dst_tag(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer, u32 length)
442 {
443         if (state->dst_hw_cap & DST_TYPE_HAS_SESSION) {
444                 hw_buffer->msg[2] = p_ca_message->msg[1];       /*      MSB     */
445                 hw_buffer->msg[3] = p_ca_message->msg[2];       /*      LSB     */
446         } else {
447                 if (length > 247) {
448                         dprintk(verbose, DST_CA_ERROR, 1, " Message too long ! *** Bailing Out *** !");
449                         return -1;
450                 }
451                 hw_buffer->msg[0] = (length & 0xff) + 7;
452                 hw_buffer->msg[1] = 0x40;
453                 hw_buffer->msg[2] = 0x03;
454                 hw_buffer->msg[3] = 0x00;
455                 hw_buffer->msg[4] = 0x03;
456                 hw_buffer->msg[5] = length & 0xff;
457                 hw_buffer->msg[6] = 0x00;
458
459                 /*
460                  *      Need to compute length for EN50221 section 8.3.2, for the time being
461                  *      assuming 8.3.2 is not applicable
462                  */
463                 memcpy(&hw_buffer->msg[7], &p_ca_message->msg[4], length);
464         }
465
466         return 0;
467 }
468
469 static int write_to_8820(struct dst_state *state, struct ca_msg *hw_buffer, u8 length, u8 reply)
470 {
471         if ((dst_put_ci(state, hw_buffer->msg, length, hw_buffer->msg, reply)) < 0) {
472                 dprintk(verbose, DST_CA_ERROR, 1, " DST-CI Command failed.");
473                 dprintk(verbose, DST_CA_NOTICE, 1, " Resetting DST.");
474                 rdc_reset_state(state);
475                 return -1;
476         }
477         dprintk(verbose, DST_CA_NOTICE, 1, " DST-CI Command success.");
478
479         return 0;
480 }
481
482 static u32 asn_1_decode(u8 *asn_1_array)
483 {
484         u8 length_field = 0, word_count = 0, count = 0;
485         u32 length = 0;
486
487         length_field = asn_1_array[0];
488         dprintk(verbose, DST_CA_DEBUG, 1, " Length field=[%02x]", length_field);
489         if (length_field < 0x80) {
490                 length = length_field & 0x7f;
491                 dprintk(verbose, DST_CA_DEBUG, 1, " Length=[%02x]\n", length);
492         } else {
493                 word_count = length_field & 0x7f;
494                 for (count = 0; count < word_count; count++) {
495                         length = length  << 8;
496                         length += asn_1_array[count + 1];
497                         dprintk(verbose, DST_CA_DEBUG, 1, " Length=[%04x]", length);
498                 }
499         }
500         return length;
501 }
502
503 static int debug_string(u8 *msg, u32 length, u32 offset)
504 {
505         u32 i;
506
507         dprintk(verbose, DST_CA_DEBUG, 0, " String=[ ");
508         for (i = offset; i < length; i++)
509                 dprintk(verbose, DST_CA_DEBUG, 0, "%02x ", msg[i]);
510         dprintk(verbose, DST_CA_DEBUG, 0, "]\n");
511
512         return 0;
513 }
514
515 /*      MMI     */
516 static int ca_get_mmi(struct dst_state *state, struct ca_msg *hw_msg, struct ca_msg *mmi_msg)
517 {
518         static u8 get_mmi[] = { 0x07, 0x40, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x0f };
519
520         put_checksum(&get_mmi[0], 7);
521         if ((dst_put_ci(state, get_mmi, sizeof (get_mmi), hw_msg->msg, GET_REPLY)) < 0) {
522                 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
523                 return -1;
524         }
525         dprintk(verbose, DST_CA_NOTICE, 1, " -->dst_put_ci SUCCESS !");
526         memcpy(mmi_msg->msg, hw_msg->msg, hw_msg->msg[4]);
527
528         return 0;
529 }
530
531 /**
532  *      Get Menu should be the first MMI function (like open !)
533  */
534 static int ca_get_menu(struct dst_state *state)
535 {
536         static u8 get_menu[] = { 0x07, 0x40, 0x00, 0x00, 0x09, 0x00, 0x00, 0xff };
537
538         put_checksum(&get_menu[0], 7);
539         if ((dst_put_ci(state, get_menu, sizeof (get_menu), get_menu, NO_REPLY)) < 0) {
540                 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
541                 return -1;
542         }
543         dprintk(verbose, DST_CA_NOTICE, 1, " -->dst_put_ci SUCCESS !");
544
545         return 0;
546 }
547
548 /**
549  *      MMI Enq (Enquire the application to allow user input)
550  */
551 static int ca_answer_menu(struct dst_state *state, struct ca_msg *hw_msg, struct ca_msg *menu_answ)
552 {
553         u8 choice = 0;
554
555         static u8 answer_menu[] = { 0x08, 0x40, 0x00, 0x00, 0x0b, 0x01, 0x00, 0x01, 0xff };
556
557         /*      derive answer from menu (This comes from the user)      */
558         answer_menu[7] = choice;
559         put_checksum(&answer_menu[0], 7);
560         if ((dst_put_ci(state, answer_menu, sizeof (answer_menu), hw_msg->msg, NO_REPLY)) < 0) {
561                 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
562                 return -1;
563         }
564         dprintk(verbose, DST_CA_NOTICE, 1, " -->dst_put_ci SUCCESS !");
565
566         return 0;
567 }
568
569 static int ca_answer_mmi(struct dst_state *state, struct ca_msg *hw_msg, struct ca_msg *answ_msg)
570 {
571         u8 answer =0, length = 0;
572
573         static u8 answer_mmi[] = { 0x08, 0x40, 0x00, 0x00, 0x08, 0x01, 0x00, 0x01, 0xff };
574
575         /*      derive answer from answ_msg (This comes from the user)  */
576         if (answer == 0)        /*      0x00 == Cancel  */
577                 answer_mmi[7] = 0x00;
578         else {                  /*      0x01 == Answer  */
579                 length = strlen(answ_msg->msg);
580                 memcpy(&answer_mmi[8], answ_msg->msg, length);
581                 answer_mmi[0] += length;
582                 answer_mmi[5] += length;
583         }
584         put_checksum(&answer_mmi[0], (8 + length));
585         if ((dst_put_ci(state, answer_mmi, sizeof (answer_mmi), hw_msg->msg, GET_REPLY)) < 0) {
586                 dprintk(verbose, DST_CA_NOTICE, 1, " -->dst_put_ci FAILED !");
587
588                 return -1;
589         }
590         dprintk(verbose, DST_CA_NOTICE, 1, " -->dst_put_ci SUCCESS !");
591
592         return 0;
593 }
594
595 static int ca_close_mmi(struct dst_state *state, struct ca_msg *hw_msg)
596 {
597         static u8 close_mmi[] = { 0x07, 0x40, 0x00, 0x00, 0x0e, 0x00, 0x00, 0xff };
598
599         put_checksum(&close_mmi[0], 7);
600         if ((dst_put_ci(state, close_mmi, sizeof (close_mmi), hw_msg->msg, NO_REPLY)) < 0) {
601                 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
602
603                 return -1;
604         }
605         dprintk(verbose, DST_CA_NOTICE, 1, " -->dst_put_ci SUCCESS !");
606
607         return 0;
608 }
609
610 static int ca_set_pmt(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer, u8 reply, u8 query)
611 {
612         u32 length = 0;
613         u8 tag_length = 8;
614
615         length = asn_1_decode(&p_ca_message->msg[3]);
616         dprintk(verbose, DST_CA_DEBUG, 1, " CA Message length=[%d]", length);
617         debug_string(&p_ca_message->msg[4], length, 0); /*      length is excluding tag & length        */
618
619         memset(hw_buffer->msg, '\0', length);
620         handle_dst_tag(state, p_ca_message, hw_buffer, length);
621         put_checksum(hw_buffer->msg, hw_buffer->msg[0]);
622
623         debug_string(hw_buffer->msg, (length + tag_length), 0); /*      tags too        */
624         write_to_8820(state, hw_buffer, (length + tag_length), reply);
625
626         return 0;
627 }
628
629
630 /*      Board supports CA PMT reply ?           */
631 static int dst_check_ca_pmt(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer)
632 {
633         int ca_pmt_reply_test = 0;
634
635         /*      Do test board                   */
636         /*      Not there yet but soon          */
637
638         /*      CA PMT Reply capable            */
639         if (ca_pmt_reply_test) {
640                 if ((ca_set_pmt(state, p_ca_message, hw_buffer, 1, GET_REPLY)) < 0) {
641                         dprintk(verbose, DST_CA_ERROR, 1, " ca_set_pmt.. failed !");
642                         return -1;
643                 }
644
645         /*      Process CA PMT Reply            */
646         /*      will implement soon             */
647                 dprintk(verbose, DST_CA_ERROR, 1, " Not there yet");
648         }
649         /*      CA PMT Reply not capable        */
650         if (!ca_pmt_reply_test) {
651                 if ((ca_set_pmt(state, p_ca_message, hw_buffer, 0, NO_REPLY)) < 0) {
652                         dprintk(verbose, DST_CA_ERROR, 1, " ca_set_pmt.. failed !");
653                         return -1;
654                 }
655                 dprintk(verbose, DST_CA_NOTICE, 1, " ca_set_pmt.. success !");
656         /*      put a dummy message             */
657
658         }
659         return 0;
660 }
661
662 static int ca_send_message(struct dst_state *state, struct ca_msg *p_ca_message, void __user *arg)
663 {
664         int i = 0;
665         unsigned int ca_message_header_len;
666
667         u32 command = 0;
668         struct ca_msg *hw_buffer;
669         int result = 0;
670
671         if ((hw_buffer = (struct ca_msg *) kmalloc(sizeof (struct ca_msg), GFP_KERNEL)) == NULL) {
672                 dprintk(verbose, DST_CA_ERROR, 1, " Memory allocation failure");
673                 return -ENOMEM;
674         }
675         dprintk(verbose, DST_CA_DEBUG, 1, " ");
676
677         if (copy_from_user(p_ca_message, arg, sizeof (struct ca_msg))) {
678                 result = -EFAULT;
679                 goto free_mem_and_exit;
680         }
681
682
683         if (p_ca_message->msg) {
684                 ca_message_header_len = p_ca_message->length;   /*      Restore it back when you are done       */
685                 /*      EN50221 tag     */
686                 command = 0;
687
688                 for (i = 0; i < 3; i++) {
689                         command = command | p_ca_message->msg[i];
690                         if (i < 2)
691                                 command = command << 8;
692                 }
693                 dprintk(verbose, DST_CA_DEBUG, 1, " Command=[0x%x]\n", command);
694
695                 switch (command) {
696                 case CA_PMT:
697                         dprintk(verbose, DST_CA_DEBUG, 1, "Command = SEND_CA_PMT");
698                         if ((ca_set_pmt(state, p_ca_message, hw_buffer, 0, 0)) < 0) {   // code simplification started
699                                 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_PMT Failed !");
700                                 result = -1;
701                                 goto free_mem_and_exit;
702                         }
703                         dprintk(verbose, DST_CA_INFO, 1, " -->CA_PMT Success !");
704                         break;
705                 case CA_PMT_REPLY:
706                         dprintk(verbose, DST_CA_INFO, 1, "Command = CA_PMT_REPLY");
707                         /*      Have to handle the 2 basic types of cards here  */
708                         if ((dst_check_ca_pmt(state, p_ca_message, hw_buffer)) < 0) {
709                                 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_PMT_REPLY Failed !");
710                                 result = -1;
711                                 goto free_mem_and_exit;
712                         }
713                         dprintk(verbose, DST_CA_INFO, 1, " -->CA_PMT_REPLY Success !");
714                         break;
715                 case CA_APP_INFO_ENQUIRY:               // only for debugging
716                         dprintk(verbose, DST_CA_INFO, 1, " Getting Cam Application information");
717
718                         if ((ca_get_app_info(state)) < 0) {
719                                 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_APP_INFO_ENQUIRY Failed !");
720                                 result = -1;
721                                 goto free_mem_and_exit;
722                         }
723                         dprintk(verbose, DST_CA_INFO, 1, " -->CA_APP_INFO_ENQUIRY Success !");
724                         break;
725                 case CA_INFO_ENQUIRY:
726                         dprintk(verbose, DST_CA_INFO, 1, " Getting CA Information");
727
728                         if ((ca_get_ca_info(state)) < 0) {
729                                 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_INFO_ENQUIRY Failed !");
730                                 result = -1;
731                                 goto free_mem_and_exit;
732                         }
733                         dprintk(verbose, DST_CA_INFO, 1, " -->CA_INFO_ENQUIRY Success !");
734                         break;
735                 }
736         }
737 free_mem_and_exit:
738         kfree (hw_buffer);
739
740         return result;
741 }
742
743 static int dst_ca_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long ioctl_arg)
744 {
745         struct dvb_device* dvbdev = (struct dvb_device*) file->private_data;
746         struct dst_state* state = (struct dst_state*) dvbdev->priv;
747         struct ca_slot_info *p_ca_slot_info;
748         struct ca_caps *p_ca_caps;
749         struct ca_msg *p_ca_message;
750         void __user *arg = (void __user *)ioctl_arg;
751         int result = 0;
752
753         if ((p_ca_message = (struct ca_msg *) kmalloc(sizeof (struct ca_msg), GFP_KERNEL)) == NULL) {
754                 dprintk(verbose, DST_CA_ERROR, 1, " Memory allocation failure");
755                 return -ENOMEM;
756         }
757         if ((p_ca_slot_info = (struct ca_slot_info *) kmalloc(sizeof (struct ca_slot_info), GFP_KERNEL)) == NULL) {
758                 dprintk(verbose, DST_CA_ERROR, 1, " Memory allocation failure");
759                 return -ENOMEM;
760         }
761         if ((p_ca_caps = (struct ca_caps *) kmalloc(sizeof (struct ca_caps), GFP_KERNEL)) == NULL) {
762                 dprintk(verbose, DST_CA_ERROR, 1, " Memory allocation failure");
763                 return -ENOMEM;
764         }
765         /*      We have now only the standard ioctl's, the driver is upposed to handle internals.       */
766         switch (cmd) {
767         case CA_SEND_MSG:
768                 dprintk(verbose, DST_CA_INFO, 1, " Sending message");
769                 if ((ca_send_message(state, p_ca_message, arg)) < 0) {
770                         dprintk(verbose, DST_CA_ERROR, 1, " -->CA_SEND_MSG Failed !");
771                         result = -1;
772                         goto free_mem_and_exit;
773                 }
774                 break;
775         case CA_GET_MSG:
776                 dprintk(verbose, DST_CA_INFO, 1, " Getting message");
777                 if ((ca_get_message(state, p_ca_message, arg)) < 0) {
778                         dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_MSG Failed !");
779                         result = -1;
780                         goto free_mem_and_exit;
781                 }
782                 dprintk(verbose, DST_CA_INFO, 1, " -->CA_GET_MSG Success !");
783                 break;
784         case CA_RESET:
785                 dprintk(verbose, DST_CA_ERROR, 1, " Resetting DST");
786                 dst_error_bailout(state);
787                 msleep(4000);
788                 break;
789         case CA_GET_SLOT_INFO:
790                 dprintk(verbose, DST_CA_INFO, 1, " Getting Slot info");
791                 if ((ca_get_slot_info(state, p_ca_slot_info, arg)) < 0) {
792                         dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_SLOT_INFO Failed !");
793                         result = -1;
794                         goto free_mem_and_exit;
795                 }
796                 dprintk(verbose, DST_CA_INFO, 1, " -->CA_GET_SLOT_INFO Success !");
797                 break;
798         case CA_GET_CAP:
799                 dprintk(verbose, DST_CA_INFO, 1, " Getting Slot capabilities");
800                 if ((ca_get_slot_caps(state, p_ca_caps, arg)) < 0) {
801                         dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_CAP Failed !");
802                         result = -1;
803                         goto free_mem_and_exit;
804                 }
805                 dprintk(verbose, DST_CA_INFO, 1, " -->CA_GET_CAP Success !");
806                 break;
807         case CA_GET_DESCR_INFO:
808                 dprintk(verbose, DST_CA_INFO, 1, " Getting descrambler description");
809                 if ((ca_get_slot_descr(state, p_ca_message, arg)) < 0) {
810                         dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_DESCR_INFO Failed !");
811                         result = -1;
812                         goto free_mem_and_exit;
813                 }
814                 dprintk(verbose, DST_CA_INFO, 1, " -->CA_GET_DESCR_INFO Success !");
815                 break;
816         case CA_SET_DESCR:
817                 dprintk(verbose, DST_CA_INFO, 1, " Setting descrambler");
818                 if ((ca_set_slot_descr()) < 0) {
819                         dprintk(verbose, DST_CA_ERROR, 1, " -->CA_SET_DESCR Failed !");
820                         result = -1;
821                         goto free_mem_and_exit;
822                 }
823                 dprintk(verbose, DST_CA_INFO, 1, " -->CA_SET_DESCR Success !");
824                 break;
825         case CA_SET_PID:
826                 dprintk(verbose, DST_CA_INFO, 1, " Setting PID");
827                 if ((ca_set_pid()) < 0) {
828                         dprintk(verbose, DST_CA_ERROR, 1, " -->CA_SET_PID Failed !");
829                         result = -1;
830                         goto free_mem_and_exit;
831                 }
832                 dprintk(verbose, DST_CA_INFO, 1, " -->CA_SET_PID Success !");
833         default:
834                 result = -EOPNOTSUPP;
835         };
836  free_mem_and_exit:
837         kfree (p_ca_message);
838         kfree (p_ca_slot_info);
839         kfree (p_ca_caps);
840
841         return result;
842 }
843
844 static int dst_ca_open(struct inode *inode, struct file *file)
845 {
846         dprintk(verbose, DST_CA_DEBUG, 1, " Device opened [%p] ", file);
847         try_module_get(THIS_MODULE);
848
849         return 0;
850 }
851
852 static int dst_ca_release(struct inode *inode, struct file *file)
853 {
854         dprintk(verbose, DST_CA_DEBUG, 1, " Device closed.");
855         module_put(THIS_MODULE);
856
857         return 0;
858 }
859
860 static ssize_t dst_ca_read(struct file *file, char __user *buffer, size_t length, loff_t *offset)
861 {
862         ssize_t bytes_read = 0;
863
864         dprintk(verbose, DST_CA_DEBUG, 1, " Device read.");
865
866         return bytes_read;
867 }
868
869 static ssize_t dst_ca_write(struct file *file, const char __user *buffer, size_t length, loff_t *offset)
870 {
871         dprintk(verbose, DST_CA_DEBUG, 1, " Device write.");
872
873         return 0;
874 }
875
876 static struct file_operations dst_ca_fops = {
877         .owner = THIS_MODULE,
878         .ioctl = dst_ca_ioctl,
879         .open = dst_ca_open,
880         .release = dst_ca_release,
881         .read = dst_ca_read,
882         .write = dst_ca_write
883 };
884
885 static struct dvb_device dvbdev_ca = {
886         .priv = NULL,
887         .users = 1,
888         .readers = 1,
889         .writers = 1,
890         .fops = &dst_ca_fops
891 };
892
893 int dst_ca_attach(struct dst_state *dst, struct dvb_adapter *dvb_adapter)
894 {
895         struct dvb_device *dvbdev;
896         dprintk(verbose, DST_CA_ERROR, 1, "registering DST-CA device");
897         dvb_register_device(dvb_adapter, &dvbdev, &dvbdev_ca, dst, DVB_DEVICE_CA);
898         return 0;
899 }
900
901 EXPORT_SYMBOL(dst_ca_attach);
902
903 MODULE_DESCRIPTION("DST DVB-S/T/C Combo CA driver");
904 MODULE_AUTHOR("Manu Abraham");
905 MODULE_LICENSE("GPL");