xref: /linux/drivers/media/pci/bt8xx/dst_ca.c (revision bfd5bb6f90af092aa345b15cd78143956a13c2a8)
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/slab.h>
24 #include <linux/init.h>
25 #include <linux/mutex.h>
26 #include <linux/string.h>
27 #include <linux/dvb/ca.h>
28 #include <media/dvbdev.h>
29 #include <media/dvb_frontend.h>
30 #include "dst_ca.h"
31 #include "dst_common.h"
32 
33 #define DST_CA_ERROR		0
34 #define DST_CA_NOTICE		1
35 #define DST_CA_INFO		2
36 #define DST_CA_DEBUG		3
37 
38 #define dprintk(x, y, z, format, arg...) do {						\
39 	if (z) {									\
40 		if	((x > DST_CA_ERROR) && (x > y))					\
41 			printk(KERN_ERR "%s: " format "\n", __func__ , ##arg);	\
42 		else if	((x > DST_CA_NOTICE) && (x > y))				\
43 			printk(KERN_NOTICE "%s: " format "\n", __func__ , ##arg);	\
44 		else if ((x > DST_CA_INFO) && (x > y))					\
45 			printk(KERN_INFO "%s: " format "\n", __func__ , ##arg);	\
46 		else if ((x > DST_CA_DEBUG) && (x > y))					\
47 			printk(KERN_DEBUG "%s: " format "\n", __func__ , ##arg);	\
48 	} else {									\
49 		if (x > y)								\
50 			printk(format, ## arg);						\
51 	}										\
52 } while(0)
53 
54 
55 static DEFINE_MUTEX(dst_ca_mutex);
56 static unsigned int verbose = 5;
57 module_param(verbose, int, 0644);
58 MODULE_PARM_DESC(verbose, "verbose startup messages, default is 1 (yes)");
59 
60 static void put_command_and_length(u8 *data, int command, int length)
61 {
62 	data[0] = (command >> 16) & 0xff;
63 	data[1] = (command >> 8) & 0xff;
64 	data[2] = command & 0xff;
65 	data[3] = length;
66 }
67 
68 static void put_checksum(u8 *check_string, int length)
69 {
70 	dprintk(verbose, DST_CA_DEBUG, 1, " Computing string checksum.");
71 	dprintk(verbose, DST_CA_DEBUG, 1, "  -> string length : 0x%02x", length);
72 	check_string[length] = dst_check_sum (check_string, length);
73 	dprintk(verbose, DST_CA_DEBUG, 1, "  -> checksum      : 0x%02x", check_string[length]);
74 }
75 
76 static int dst_ci_command(struct dst_state* state, u8 * data, u8 *ca_string, u8 len, int read)
77 {
78 	u8 reply;
79 
80 	mutex_lock(&state->dst_mutex);
81 	dst_comm_init(state);
82 	msleep(65);
83 
84 	if (write_dst(state, data, len)) {
85 		dprintk(verbose, DST_CA_INFO, 1, " Write not successful, trying to recover");
86 		dst_error_recovery(state);
87 		goto error;
88 	}
89 	if ((dst_pio_disable(state)) < 0) {
90 		dprintk(verbose, DST_CA_ERROR, 1, " DST PIO disable failed.");
91 		goto error;
92 	}
93 	if (read_dst(state, &reply, GET_ACK) < 0) {
94 		dprintk(verbose, DST_CA_INFO, 1, " Read not successful, trying to recover");
95 		dst_error_recovery(state);
96 		goto error;
97 	}
98 	if (read) {
99 		if (! dst_wait_dst_ready(state, LONG_DELAY)) {
100 			dprintk(verbose, DST_CA_NOTICE, 1, " 8820 not ready");
101 			goto error;
102 		}
103 		if (read_dst(state, ca_string, 128) < 0) {	/*	Try to make this dynamic	*/
104 			dprintk(verbose, DST_CA_INFO, 1, " Read not successful, trying to recover");
105 			dst_error_recovery(state);
106 			goto error;
107 		}
108 	}
109 	mutex_unlock(&state->dst_mutex);
110 	return 0;
111 
112 error:
113 	mutex_unlock(&state->dst_mutex);
114 	return -EIO;
115 }
116 
117 
118 static int dst_put_ci(struct dst_state *state, u8 *data, int len, u8 *ca_string, int read)
119 {
120 	u8 dst_ca_comm_err = 0;
121 
122 	while (dst_ca_comm_err < RETRIES) {
123 		dprintk(verbose, DST_CA_NOTICE, 1, " Put Command");
124 		if (dst_ci_command(state, data, ca_string, len, read)) {	// If error
125 			dst_error_recovery(state);
126 			dst_ca_comm_err++; // work required here.
127 		} else {
128 			break;
129 		}
130 	}
131 
132 	if(dst_ca_comm_err == RETRIES)
133 		return -EIO;
134 
135 	return 0;
136 }
137 
138 
139 
140 static int ca_get_app_info(struct dst_state *state)
141 {
142 	int length, str_length;
143 	static u8 command[8] = {0x07, 0x40, 0x01, 0x00, 0x01, 0x00, 0x00, 0xff};
144 
145 	put_checksum(&command[0], command[0]);
146 	if ((dst_put_ci(state, command, sizeof(command), state->messages, GET_REPLY)) < 0) {
147 		dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
148 		return -EIO;
149 	}
150 	dprintk(verbose, DST_CA_INFO, 1, " -->dst_put_ci SUCCESS !");
151 	dprintk(verbose, DST_CA_INFO, 1, " ================================ CI Module Application Info ======================================");
152 	dprintk(verbose, DST_CA_INFO, 1, " Application Type=[%d], Application Vendor=[%d], Vendor Code=[%d]\n%s: Application info=[%s]",
153 		state->messages[7], (state->messages[8] << 8) | state->messages[9],
154 		(state->messages[10] << 8) | state->messages[11], __func__, (char *)(&state->messages[12]));
155 	dprintk(verbose, DST_CA_INFO, 1, " ==================================================================================================");
156 
157 	// Transform dst message to correct application_info message
158 	length = state->messages[5];
159 	str_length = length - 6;
160 	if (str_length < 0) {
161 		str_length = 0;
162 		dprintk(verbose, DST_CA_ERROR, 1, "Invalid string length returned in ca_get_app_info(). Recovering.");
163 	}
164 
165 	// First, the command and length fields
166 	put_command_and_length(&state->messages[0], CA_APP_INFO, length);
167 
168 	// Copy application_type, application_manufacturer and manufacturer_code
169 	memmove(&state->messages[4], &state->messages[7], 5);
170 
171 	// Set string length and copy string
172 	state->messages[9] = str_length;
173 	memmove(&state->messages[10], &state->messages[12], str_length);
174 
175 	return 0;
176 }
177 
178 static int ca_get_ca_info(struct dst_state *state)
179 {
180 	int srcPtr, dstPtr, i, num_ids;
181 	static u8 slot_command[8] = {0x07, 0x40, 0x00, 0x00, 0x02, 0x00, 0x00, 0xff};
182 	const int in_system_id_pos = 8, out_system_id_pos = 4, in_num_ids_pos = 7;
183 
184 	put_checksum(&slot_command[0], slot_command[0]);
185 	if ((dst_put_ci(state, slot_command, sizeof (slot_command), state->messages, GET_REPLY)) < 0) {
186 		dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
187 		return -EIO;
188 	}
189 	dprintk(verbose, DST_CA_INFO, 1, " -->dst_put_ci SUCCESS !");
190 
191 	// Print raw data
192 	dprintk(verbose, DST_CA_INFO, 0, " DST data = [");
193 	for (i = 0; i < state->messages[0] + 1; i++) {
194 		dprintk(verbose, DST_CA_INFO, 0, " 0x%02x", state->messages[i]);
195 	}
196 	dprintk(verbose, DST_CA_INFO, 0, "]\n");
197 
198 	// Set the command and length of the output
199 	num_ids = state->messages[in_num_ids_pos];
200 	if (num_ids >= 100) {
201 		num_ids = 100;
202 		dprintk(verbose, DST_CA_ERROR, 1, "Invalid number of ids (>100). Recovering.");
203 	}
204 	put_command_and_length(&state->messages[0], CA_INFO, num_ids * 2);
205 
206 	dprintk(verbose, DST_CA_INFO, 0, " CA_INFO = [");
207 	srcPtr = in_system_id_pos;
208 	dstPtr = out_system_id_pos;
209 	for(i = 0; i < num_ids; i++) {
210 		dprintk(verbose, DST_CA_INFO, 0, " 0x%02x%02x", state->messages[srcPtr + 0], state->messages[srcPtr + 1]);
211 		// Append to output
212 		state->messages[dstPtr + 0] = state->messages[srcPtr + 0];
213 		state->messages[dstPtr + 1] = state->messages[srcPtr + 1];
214 		srcPtr += 2;
215 		dstPtr += 2;
216 	}
217 	dprintk(verbose, DST_CA_INFO, 0, "]\n");
218 
219 	return 0;
220 }
221 
222 static int ca_get_slot_caps(struct dst_state *state, struct ca_caps *p_ca_caps, void __user *arg)
223 {
224 	int i;
225 	u8 slot_cap[256];
226 	static u8 slot_command[8] = {0x07, 0x40, 0x02, 0x00, 0x02, 0x00, 0x00, 0xff};
227 
228 	put_checksum(&slot_command[0], slot_command[0]);
229 	if ((dst_put_ci(state, slot_command, sizeof (slot_command), slot_cap, GET_REPLY)) < 0) {
230 		dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
231 		return -EIO;
232 	}
233 	dprintk(verbose, DST_CA_NOTICE, 1, " -->dst_put_ci SUCCESS !");
234 
235 	/*	Will implement the rest soon		*/
236 
237 	dprintk(verbose, DST_CA_INFO, 1, " Slot cap = [%d]", slot_cap[7]);
238 	dprintk(verbose, DST_CA_INFO, 0, "===================================\n");
239 	for (i = 0; i < slot_cap[0] + 1; i++)
240 		dprintk(verbose, DST_CA_INFO, 0, " %d", slot_cap[i]);
241 	dprintk(verbose, DST_CA_INFO, 0, "\n");
242 
243 	p_ca_caps->slot_num = 1;
244 	p_ca_caps->slot_type = 1;
245 	p_ca_caps->descr_num = slot_cap[7];
246 	p_ca_caps->descr_type = 1;
247 
248 	if (copy_to_user(arg, p_ca_caps, sizeof (struct ca_caps)))
249 		return -EFAULT;
250 
251 	return 0;
252 }
253 
254 /*	Need some more work	*/
255 static int ca_get_slot_descr(struct dst_state *state, struct ca_msg *p_ca_message, void __user *arg)
256 {
257 	return -EOPNOTSUPP;
258 }
259 
260 
261 static int ca_get_slot_info(struct dst_state *state, struct ca_slot_info *p_ca_slot_info, void __user *arg)
262 {
263 	int i;
264 	static u8 slot_command[8] = {0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff};
265 
266 	u8 *slot_info = state->messages;
267 
268 	put_checksum(&slot_command[0], 7);
269 	if ((dst_put_ci(state, slot_command, sizeof (slot_command), slot_info, GET_REPLY)) < 0) {
270 		dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
271 		return -EIO;
272 	}
273 	dprintk(verbose, DST_CA_INFO, 1, " -->dst_put_ci SUCCESS !");
274 
275 	/*	Will implement the rest soon		*/
276 
277 	dprintk(verbose, DST_CA_INFO, 1, " Slot info = [%d]", slot_info[3]);
278 	dprintk(verbose, DST_CA_INFO, 0, "===================================\n");
279 	for (i = 0; i < 8; i++)
280 		dprintk(verbose, DST_CA_INFO, 0, " %d", slot_info[i]);
281 	dprintk(verbose, DST_CA_INFO, 0, "\n");
282 
283 	if (slot_info[4] & 0x80) {
284 		p_ca_slot_info->flags = CA_CI_MODULE_PRESENT;
285 		p_ca_slot_info->num = 1;
286 		p_ca_slot_info->type = CA_CI;
287 	} else if (slot_info[4] & 0x40) {
288 		p_ca_slot_info->flags = CA_CI_MODULE_READY;
289 		p_ca_slot_info->num = 1;
290 		p_ca_slot_info->type = CA_CI;
291 	} else
292 		p_ca_slot_info->flags = 0;
293 
294 	if (copy_to_user(arg, p_ca_slot_info, sizeof (struct ca_slot_info)))
295 		return -EFAULT;
296 
297 	return 0;
298 }
299 
300 
301 static int ca_get_message(struct dst_state *state, struct ca_msg *p_ca_message, void __user *arg)
302 {
303 	u8 i = 0;
304 	u32 command = 0;
305 
306 	if (copy_from_user(p_ca_message, arg, sizeof (struct ca_msg)))
307 		return -EFAULT;
308 
309 	dprintk(verbose, DST_CA_NOTICE, 1, " Message = [%*ph]",
310 		3, p_ca_message->msg);
311 
312 	for (i = 0; i < 3; i++) {
313 		command = command | p_ca_message->msg[i];
314 		if (i < 2)
315 			command = command << 8;
316 	}
317 	dprintk(verbose, DST_CA_NOTICE, 1, " Command=[0x%x]", command);
318 
319 	switch (command) {
320 	case CA_APP_INFO:
321 		memcpy(p_ca_message->msg, state->messages, 128);
322 		if (copy_to_user(arg, p_ca_message, sizeof (struct ca_msg)) )
323 			return -EFAULT;
324 		break;
325 	case CA_INFO:
326 		memcpy(p_ca_message->msg, state->messages, 128);
327 		if (copy_to_user(arg, p_ca_message, sizeof (struct ca_msg)) )
328 			return -EFAULT;
329 		break;
330 	}
331 
332 	return 0;
333 }
334 
335 static int handle_dst_tag(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer, u32 length)
336 {
337 	if (state->dst_hw_cap & DST_TYPE_HAS_SESSION) {
338 		hw_buffer->msg[2] = p_ca_message->msg[1];	/*	MSB	*/
339 		hw_buffer->msg[3] = p_ca_message->msg[2];	/*	LSB	*/
340 	} else {
341 		if (length > 247) {
342 			dprintk(verbose, DST_CA_ERROR, 1, " Message too long ! *** Bailing Out *** !");
343 			return -EIO;
344 		}
345 		hw_buffer->msg[0] = (length & 0xff) + 7;
346 		hw_buffer->msg[1] = 0x40;
347 		hw_buffer->msg[2] = 0x03;
348 		hw_buffer->msg[3] = 0x00;
349 		hw_buffer->msg[4] = 0x03;
350 		hw_buffer->msg[5] = length & 0xff;
351 		hw_buffer->msg[6] = 0x00;
352 
353 		/*
354 		 *	Need to compute length for EN50221 section 8.3.2, for the time being
355 		 *	assuming 8.3.2 is not applicable
356 		 */
357 		memcpy(&hw_buffer->msg[7], &p_ca_message->msg[4], length);
358 	}
359 
360 	return 0;
361 }
362 
363 static int write_to_8820(struct dst_state *state, struct ca_msg *hw_buffer, u8 length, u8 reply)
364 {
365 	if ((dst_put_ci(state, hw_buffer->msg, length, hw_buffer->msg, reply)) < 0) {
366 		dprintk(verbose, DST_CA_ERROR, 1, " DST-CI Command failed.");
367 		dprintk(verbose, DST_CA_NOTICE, 1, " Resetting DST.");
368 		rdc_reset_state(state);
369 		return -EIO;
370 	}
371 	dprintk(verbose, DST_CA_NOTICE, 1, " DST-CI Command success.");
372 
373 	return 0;
374 }
375 
376 static u32 asn_1_decode(u8 *asn_1_array)
377 {
378 	u8 length_field = 0, word_count = 0, count = 0;
379 	u32 length = 0;
380 
381 	length_field = asn_1_array[0];
382 	dprintk(verbose, DST_CA_DEBUG, 1, " Length field=[%02x]", length_field);
383 	if (length_field < 0x80) {
384 		length = length_field & 0x7f;
385 		dprintk(verbose, DST_CA_DEBUG, 1, " Length=[%02x]\n", length);
386 	} else {
387 		word_count = length_field & 0x7f;
388 		for (count = 0; count < word_count; count++) {
389 			length = length  << 8;
390 			length += asn_1_array[count + 1];
391 			dprintk(verbose, DST_CA_DEBUG, 1, " Length=[%04x]", length);
392 		}
393 	}
394 	return length;
395 }
396 
397 static int debug_string(u8 *msg, u32 length, u32 offset)
398 {
399 	u32 i;
400 
401 	dprintk(verbose, DST_CA_DEBUG, 0, " String=[ ");
402 	for (i = offset; i < length; i++)
403 		dprintk(verbose, DST_CA_DEBUG, 0, "%02x ", msg[i]);
404 	dprintk(verbose, DST_CA_DEBUG, 0, "]\n");
405 
406 	return 0;
407 }
408 
409 
410 static int ca_set_pmt(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer, u8 reply, u8 query)
411 {
412 	u32 length = 0;
413 	u8 tag_length = 8;
414 
415 	length = asn_1_decode(&p_ca_message->msg[3]);
416 	dprintk(verbose, DST_CA_DEBUG, 1, " CA Message length=[%d]", length);
417 	debug_string(&p_ca_message->msg[4], length, 0); /*	length is excluding tag & length	*/
418 
419 	memset(hw_buffer->msg, '\0', length);
420 	handle_dst_tag(state, p_ca_message, hw_buffer, length);
421 	put_checksum(hw_buffer->msg, hw_buffer->msg[0]);
422 
423 	debug_string(hw_buffer->msg, (length + tag_length), 0); /*	tags too	*/
424 	write_to_8820(state, hw_buffer, (length + tag_length), reply);
425 
426 	return 0;
427 }
428 
429 
430 /*	Board supports CA PMT reply ?		*/
431 static int dst_check_ca_pmt(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer)
432 {
433 	int ca_pmt_reply_test = 0;
434 
435 	/*	Do test board			*/
436 	/*	Not there yet but soon		*/
437 
438 	/*	CA PMT Reply capable		*/
439 	if (ca_pmt_reply_test) {
440 		if ((ca_set_pmt(state, p_ca_message, hw_buffer, 1, GET_REPLY)) < 0) {
441 			dprintk(verbose, DST_CA_ERROR, 1, " ca_set_pmt.. failed !");
442 			return -EIO;
443 		}
444 
445 	/*	Process CA PMT Reply		*/
446 	/*	will implement soon		*/
447 		dprintk(verbose, DST_CA_ERROR, 1, " Not there yet");
448 	}
449 	/*	CA PMT Reply not capable	*/
450 	if (!ca_pmt_reply_test) {
451 		if ((ca_set_pmt(state, p_ca_message, hw_buffer, 0, NO_REPLY)) < 0) {
452 			dprintk(verbose, DST_CA_ERROR, 1, " ca_set_pmt.. failed !");
453 			return -EIO;
454 		}
455 		dprintk(verbose, DST_CA_NOTICE, 1, " ca_set_pmt.. success !");
456 	/*	put a dummy message		*/
457 
458 	}
459 	return 0;
460 }
461 
462 static int ca_send_message(struct dst_state *state, struct ca_msg *p_ca_message, void __user *arg)
463 {
464 	int i;
465 	u32 command;
466 	struct ca_msg *hw_buffer;
467 	int result = 0;
468 
469 	hw_buffer = kmalloc(sizeof(*hw_buffer), GFP_KERNEL);
470 	if (!hw_buffer)
471 		return -ENOMEM;
472 	dprintk(verbose, DST_CA_DEBUG, 1, " ");
473 
474 	if (copy_from_user(p_ca_message, arg, sizeof (struct ca_msg))) {
475 		result = -EFAULT;
476 		goto free_mem_and_exit;
477 	}
478 
479 	/*	EN50221 tag	*/
480 	command = 0;
481 
482 	for (i = 0; i < 3; i++) {
483 		command = command | p_ca_message->msg[i];
484 		if (i < 2)
485 			command = command << 8;
486 	}
487 	dprintk(verbose, DST_CA_DEBUG, 1, " Command=[0x%x]\n", command);
488 
489 	switch (command) {
490 	case CA_PMT:
491 		dprintk(verbose, DST_CA_DEBUG, 1, "Command = SEND_CA_PMT");
492 		if ((ca_set_pmt(state, p_ca_message, hw_buffer, 0, 0)) < 0) {	// code simplification started
493 			dprintk(verbose, DST_CA_ERROR, 1, " -->CA_PMT Failed !");
494 			result = -1;
495 			goto free_mem_and_exit;
496 		}
497 		dprintk(verbose, DST_CA_INFO, 1, " -->CA_PMT Success !");
498 		break;
499 	case CA_PMT_REPLY:
500 		dprintk(verbose, DST_CA_INFO, 1, "Command = CA_PMT_REPLY");
501 		/*      Have to handle the 2 basic types of cards here  */
502 		if ((dst_check_ca_pmt(state, p_ca_message, hw_buffer)) < 0) {
503 			dprintk(verbose, DST_CA_ERROR, 1, " -->CA_PMT_REPLY Failed !");
504 			result = -1;
505 			goto free_mem_and_exit;
506 		}
507 		dprintk(verbose, DST_CA_INFO, 1, " -->CA_PMT_REPLY Success !");
508 		break;
509 	case CA_APP_INFO_ENQUIRY:		// only for debugging
510 		dprintk(verbose, DST_CA_INFO, 1, " Getting Cam Application information");
511 
512 		if ((ca_get_app_info(state)) < 0) {
513 			dprintk(verbose, DST_CA_ERROR, 1, " -->CA_APP_INFO_ENQUIRY Failed !");
514 			result = -1;
515 			goto free_mem_and_exit;
516 		}
517 		dprintk(verbose, DST_CA_INFO, 1, " -->CA_APP_INFO_ENQUIRY Success !");
518 		break;
519 	case CA_INFO_ENQUIRY:
520 		dprintk(verbose, DST_CA_INFO, 1, " Getting CA Information");
521 
522 		if ((ca_get_ca_info(state)) < 0) {
523 			dprintk(verbose, DST_CA_ERROR, 1, " -->CA_INFO_ENQUIRY Failed !");
524 			result = -1;
525 			goto free_mem_and_exit;
526 		}
527 		dprintk(verbose, DST_CA_INFO, 1, " -->CA_INFO_ENQUIRY Success !");
528 		break;
529 	}
530 
531 free_mem_and_exit:
532 	kfree (hw_buffer);
533 
534 	return result;
535 }
536 
537 static long dst_ca_ioctl(struct file *file, unsigned int cmd, unsigned long ioctl_arg)
538 {
539 	struct dvb_device *dvbdev;
540 	struct dst_state *state;
541 	struct ca_slot_info *p_ca_slot_info;
542 	struct ca_caps *p_ca_caps;
543 	struct ca_msg *p_ca_message;
544 	void __user *arg = (void __user *)ioctl_arg;
545 	int result = 0;
546 
547 	mutex_lock(&dst_ca_mutex);
548 	dvbdev = file->private_data;
549 	state = (struct dst_state *)dvbdev->priv;
550 	p_ca_message = kmalloc(sizeof (struct ca_msg), GFP_KERNEL);
551 	p_ca_slot_info = kmalloc(sizeof (struct ca_slot_info), GFP_KERNEL);
552 	p_ca_caps = kmalloc(sizeof (struct ca_caps), GFP_KERNEL);
553 	if (!p_ca_message || !p_ca_slot_info || !p_ca_caps) {
554 		result = -ENOMEM;
555 		goto free_mem_and_exit;
556 	}
557 
558 	/*	We have now only the standard ioctl's, the driver is upposed to handle internals.	*/
559 	switch (cmd) {
560 	case CA_SEND_MSG:
561 		dprintk(verbose, DST_CA_INFO, 1, " Sending message");
562 		result = ca_send_message(state, p_ca_message, arg);
563 
564 		if (result < 0) {
565 			dprintk(verbose, DST_CA_ERROR, 1, " -->CA_SEND_MSG Failed !");
566 			goto free_mem_and_exit;
567 		}
568 		break;
569 	case CA_GET_MSG:
570 		dprintk(verbose, DST_CA_INFO, 1, " Getting message");
571 		result = ca_get_message(state, p_ca_message, arg);
572 		if (result < 0) {
573 			dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_MSG Failed !");
574 			goto free_mem_and_exit;
575 		}
576 		dprintk(verbose, DST_CA_INFO, 1, " -->CA_GET_MSG Success !");
577 		break;
578 	case CA_RESET:
579 		dprintk(verbose, DST_CA_ERROR, 1, " Resetting DST");
580 		dst_error_bailout(state);
581 		msleep(4000);
582 		break;
583 	case CA_GET_SLOT_INFO:
584 		dprintk(verbose, DST_CA_INFO, 1, " Getting Slot info");
585 		result = ca_get_slot_info(state, p_ca_slot_info, arg);
586 		if (result < 0) {
587 			dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_SLOT_INFO Failed !");
588 			result = -1;
589 			goto free_mem_and_exit;
590 		}
591 		dprintk(verbose, DST_CA_INFO, 1, " -->CA_GET_SLOT_INFO Success !");
592 		break;
593 	case CA_GET_CAP:
594 		dprintk(verbose, DST_CA_INFO, 1, " Getting Slot capabilities");
595 		result = ca_get_slot_caps(state, p_ca_caps, arg);
596 		if (result < 0) {
597 			dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_CAP Failed !");
598 			goto free_mem_and_exit;
599 		}
600 		dprintk(verbose, DST_CA_INFO, 1, " -->CA_GET_CAP Success !");
601 		break;
602 	case CA_GET_DESCR_INFO:
603 		dprintk(verbose, DST_CA_INFO, 1, " Getting descrambler description");
604 		result = ca_get_slot_descr(state, p_ca_message, arg);
605 		if (result < 0) {
606 			dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_DESCR_INFO Failed !");
607 			goto free_mem_and_exit;
608 		}
609 		dprintk(verbose, DST_CA_INFO, 1, " -->CA_GET_DESCR_INFO Success !");
610 		break;
611 	default:
612 		result = -EOPNOTSUPP;
613 	}
614  free_mem_and_exit:
615 	kfree (p_ca_message);
616 	kfree (p_ca_slot_info);
617 	kfree (p_ca_caps);
618 
619 	mutex_unlock(&dst_ca_mutex);
620 	return result;
621 }
622 
623 static int dst_ca_open(struct inode *inode, struct file *file)
624 {
625 	dprintk(verbose, DST_CA_DEBUG, 1, " Device opened [%p] ", file);
626 
627 	return 0;
628 }
629 
630 static int dst_ca_release(struct inode *inode, struct file *file)
631 {
632 	dprintk(verbose, DST_CA_DEBUG, 1, " Device closed.");
633 
634 	return 0;
635 }
636 
637 static ssize_t dst_ca_read(struct file *file, char __user *buffer, size_t length, loff_t *offset)
638 {
639 	dprintk(verbose, DST_CA_DEBUG, 1, " Device read.");
640 
641 	return 0;
642 }
643 
644 static ssize_t dst_ca_write(struct file *file, const char __user *buffer, size_t length, loff_t *offset)
645 {
646 	dprintk(verbose, DST_CA_DEBUG, 1, " Device write.");
647 
648 	return 0;
649 }
650 
651 static const struct file_operations dst_ca_fops = {
652 	.owner = THIS_MODULE,
653 	.unlocked_ioctl = dst_ca_ioctl,
654 	.open = dst_ca_open,
655 	.release = dst_ca_release,
656 	.read = dst_ca_read,
657 	.write = dst_ca_write,
658 	.llseek = noop_llseek,
659 };
660 
661 static struct dvb_device dvbdev_ca = {
662 	.priv = NULL,
663 	.users = 1,
664 	.readers = 1,
665 	.writers = 1,
666 	.fops = &dst_ca_fops
667 };
668 
669 struct dvb_device *dst_ca_attach(struct dst_state *dst, struct dvb_adapter *dvb_adapter)
670 {
671 	struct dvb_device *dvbdev;
672 
673 	dprintk(verbose, DST_CA_ERROR, 1, "registering DST-CA device");
674 	if (dvb_register_device(dvb_adapter, &dvbdev, &dvbdev_ca, dst,
675 				DVB_DEVICE_CA, 0) == 0) {
676 		dst->dst_ca = dvbdev;
677 		return dst->dst_ca;
678 	}
679 
680 	return NULL;
681 }
682 
683 EXPORT_SYMBOL(dst_ca_attach);
684 
685 MODULE_DESCRIPTION("DST DVB-S/T/C Combo CA driver");
686 MODULE_AUTHOR("Manu Abraham");
687 MODULE_LICENSE("GPL");
688