blob: 0a7623c0fc8ef2f55f62b8857b9669a350610542 [file] [log] [blame]
Johannes Stezenbach50b215a2005-05-16 21:54:41 -07001/*
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
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070021#include <linux/kernel.h>
22#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070024#include <linux/init.h>
Arnd Bergmannadfedd22010-09-11 19:53:25 +020025#include <linux/mutex.h>
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070026#include <linux/string.h>
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070027#include <linux/dvb/ca.h>
Mauro Carvalho Chehabfada1932017-12-28 13:03:51 -050028#include <media/dvbdev.h>
29#include <media/dvb_frontend.h>
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070030#include "dst_ca.h"
31#include "dst_common.h"
32
Manu Abrahama427de62005-09-09 13:03:00 -070033#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)) \
Harvey Harrisonb2e62e72008-04-08 23:20:00 -030041 printk(KERN_ERR "%s: " format "\n", __func__ , ##arg); \
Manu Abrahama427de62005-09-09 13:03:00 -070042 else if ((x > DST_CA_NOTICE) && (x > y)) \
Harvey Harrisonb2e62e72008-04-08 23:20:00 -030043 printk(KERN_NOTICE "%s: " format "\n", __func__ , ##arg); \
Manu Abrahama427de62005-09-09 13:03:00 -070044 else if ((x > DST_CA_INFO) && (x > y)) \
Harvey Harrisonb2e62e72008-04-08 23:20:00 -030045 printk(KERN_INFO "%s: " format "\n", __func__ , ##arg); \
Manu Abrahama427de62005-09-09 13:03:00 -070046 else if ((x > DST_CA_DEBUG) && (x > y)) \
Harvey Harrisonb2e62e72008-04-08 23:20:00 -030047 printk(KERN_DEBUG "%s: " format "\n", __func__ , ##arg); \
Manu Abrahama427de62005-09-09 13:03:00 -070048 } else { \
49 if (x > y) \
50 printk(format, ## arg); \
51 } \
52} while(0)
53
54
Arnd Bergmannadfedd22010-09-11 19:53:25 +020055static DEFINE_MUTEX(dst_ca_mutex);
Manu Abraham7d534212005-07-07 17:57:50 -070056static unsigned int verbose = 5;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070057module_param(verbose, int, 0644);
58MODULE_PARM_DESC(verbose, "verbose startup messages, default is 1 (yes)");
59
Henrik Sjoberg1f194562006-06-21 16:33:21 -030060static 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}
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070067
Perceval Anichini4fbbc7e2005-11-08 21:35:23 -080068static void put_checksum(u8 *check_string, int length)
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070069{
Perceval Anichini4fbbc7e2005-11-08 21:35:23 -080070 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]);
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070074}
75
76static int dst_ci_command(struct dst_state* state, u8 * data, u8 *ca_string, u8 len, int read)
77{
78 u8 reply;
79
Ingo Molnar3593cab2006-02-07 06:49:14 -020080 mutex_lock(&state->dst_mutex);
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070081 dst_comm_init(state);
82 msleep(65);
83
84 if (write_dst(state, data, len)) {
Manu Abrahama427de62005-09-09 13:03:00 -070085 dprintk(verbose, DST_CA_INFO, 1, " Write not successful, trying to recover");
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070086 dst_error_recovery(state);
Manu Abrahamd28d5762005-11-08 21:35:36 -080087 goto error;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070088 }
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070089 if ((dst_pio_disable(state)) < 0) {
Manu Abrahama427de62005-09-09 13:03:00 -070090 dprintk(verbose, DST_CA_ERROR, 1, " DST PIO disable failed.");
Manu Abrahamd28d5762005-11-08 21:35:36 -080091 goto error;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070092 }
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070093 if (read_dst(state, &reply, GET_ACK) < 0) {
Manu Abrahama427de62005-09-09 13:03:00 -070094 dprintk(verbose, DST_CA_INFO, 1, " Read not successful, trying to recover");
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070095 dst_error_recovery(state);
Manu Abrahamd28d5762005-11-08 21:35:36 -080096 goto error;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070097 }
Johannes Stezenbach50b215a2005-05-16 21:54:41 -070098 if (read) {
99 if (! dst_wait_dst_ready(state, LONG_DELAY)) {
Manu Abrahama427de62005-09-09 13:03:00 -0700100 dprintk(verbose, DST_CA_NOTICE, 1, " 8820 not ready");
Manu Abrahamd28d5762005-11-08 21:35:36 -0800101 goto error;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700102 }
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700103 if (read_dst(state, ca_string, 128) < 0) { /* Try to make this dynamic */
Manu Abrahama427de62005-09-09 13:03:00 -0700104 dprintk(verbose, DST_CA_INFO, 1, " Read not successful, trying to recover");
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700105 dst_error_recovery(state);
Manu Abrahamd28d5762005-11-08 21:35:36 -0800106 goto error;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700107 }
108 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200109 mutex_unlock(&state->dst_mutex);
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700110 return 0;
Manu Abrahamd28d5762005-11-08 21:35:36 -0800111
112error:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200113 mutex_unlock(&state->dst_mutex);
Manu Abrahamd28d5762005-11-08 21:35:36 -0800114 return -EIO;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700115}
116
117
118static 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) {
Manu Abrahama427de62005-09-09 13:03:00 -0700123 dprintk(verbose, DST_CA_NOTICE, 1, " Put Command");
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700124 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.
Henrik Sjoberg1f194562006-06-21 16:33:21 -0300127 } else {
128 break;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700129 }
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700130 }
131
Henrik Sjoberg1f194562006-06-21 16:33:21 -0300132 if(dst_ca_comm_err == RETRIES)
Mauro Carvalho Chehab39a7c8a2017-09-01 08:21:04 -0400133 return -EIO;
Henrik Sjoberg1f194562006-06-21 16:33:21 -0300134
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700135 return 0;
136}
137
138
139
140static int ca_get_app_info(struct dst_state *state)
141{
Henrik Sjoberg1f194562006-06-21 16:33:21 -0300142 int length, str_length;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700143 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) {
Manu Abrahama427de62005-09-09 13:03:00 -0700147 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
Mauro Carvalho Chehab39a7c8a2017-09-01 08:21:04 -0400148 return -EIO;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700149 }
Manu Abrahama427de62005-09-09 13:03:00 -0700150 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],
Harvey Harrisonb2e62e72008-04-08 23:20:00 -0300154 (state->messages[10] << 8) | state->messages[11], __func__, (char *)(&state->messages[12]));
Manu Abrahama427de62005-09-09 13:03:00 -0700155 dprintk(verbose, DST_CA_INFO, 1, " ==================================================================================================");
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700156
Henrik Sjoberg1f194562006-06-21 16:33:21 -0300157 // 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
Nickolai Zeldovichbb71b142013-01-07 22:28:05 -0300169 memmove(&state->messages[4], &state->messages[7], 5);
Henrik Sjoberg1f194562006-06-21 16:33:21 -0300170
171 // Set string length and copy string
172 state->messages[9] = str_length;
Nickolai Zeldovichbb71b142013-01-07 22:28:05 -0300173 memmove(&state->messages[10], &state->messages[12], str_length);
Henrik Sjoberg1f194562006-06-21 16:33:21 -0300174
175 return 0;
176}
177
178static 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 !");
Mauro Carvalho Chehab39a7c8a2017-09-01 08:21:04 -0400187 return -EIO;
Henrik Sjoberg1f194562006-06-21 16:33:21 -0300188 }
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
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700219 return 0;
220}
221
Peter Hagervall174f80d2005-11-08 21:35:18 -0800222static int ca_get_slot_caps(struct dst_state *state, struct ca_caps *p_ca_caps, void __user *arg)
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700223{
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) {
Manu Abrahama427de62005-09-09 13:03:00 -0700230 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
Mauro Carvalho Chehab39a7c8a2017-09-01 08:21:04 -0400231 return -EIO;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700232 }
Manu Abrahama427de62005-09-09 13:03:00 -0700233 dprintk(verbose, DST_CA_NOTICE, 1, " -->dst_put_ci SUCCESS !");
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700234
235 /* Will implement the rest soon */
236
Manu Abrahama427de62005-09-09 13:03:00 -0700237 dprintk(verbose, DST_CA_INFO, 1, " Slot cap = [%d]", slot_cap[7]);
238 dprintk(verbose, DST_CA_INFO, 0, "===================================\n");
Henrik Sjoberg1f194562006-06-21 16:33:21 -0300239 for (i = 0; i < slot_cap[0] + 1; i++)
Manu Abrahama427de62005-09-09 13:03:00 -0700240 dprintk(verbose, DST_CA_INFO, 0, " %d", slot_cap[i]);
241 dprintk(verbose, DST_CA_INFO, 0, "\n");
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700242
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
Peter Hagervall174f80d2005-11-08 21:35:18 -0800248 if (copy_to_user(arg, p_ca_caps, sizeof (struct ca_caps)))
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700249 return -EFAULT;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700250
251 return 0;
252}
253
Johannes Stezenbach4a2cc122005-05-16 21:54:45 -0700254/* Need some more work */
Peter Hagervall174f80d2005-11-08 21:35:18 -0800255static int ca_get_slot_descr(struct dst_state *state, struct ca_msg *p_ca_message, void __user *arg)
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700256{
257 return -EOPNOTSUPP;
258}
259
260
Peter Hagervall174f80d2005-11-08 21:35:18 -0800261static int ca_get_slot_info(struct dst_state *state, struct ca_slot_info *p_ca_slot_info, void __user *arg)
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700262{
263 int i;
264 static u8 slot_command[8] = {0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff};
265
Manu Abraham5c15c0b2005-11-08 21:35:24 -0800266 u8 *slot_info = state->messages;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700267
268 put_checksum(&slot_command[0], 7);
269 if ((dst_put_ci(state, slot_command, sizeof (slot_command), slot_info, GET_REPLY)) < 0) {
Manu Abrahama427de62005-09-09 13:03:00 -0700270 dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
Mauro Carvalho Chehab39a7c8a2017-09-01 08:21:04 -0400271 return -EIO;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700272 }
Manu Abrahama427de62005-09-09 13:03:00 -0700273 dprintk(verbose, DST_CA_INFO, 1, " -->dst_put_ci SUCCESS !");
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700274
275 /* Will implement the rest soon */
276
Manu Abrahama427de62005-09-09 13:03:00 -0700277 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");
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700282
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;
Manu Abrahama427de62005-09-09 13:03:00 -0700287 } else if (slot_info[4] & 0x40) {
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700288 p_ca_slot_info->flags = CA_CI_MODULE_READY;
289 p_ca_slot_info->num = 1;
290 p_ca_slot_info->type = CA_CI;
Manu Abrahama427de62005-09-09 13:03:00 -0700291 } else
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700292 p_ca_slot_info->flags = 0;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700293
Peter Hagervall174f80d2005-11-08 21:35:18 -0800294 if (copy_to_user(arg, p_ca_slot_info, sizeof (struct ca_slot_info)))
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700295 return -EFAULT;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700296
297 return 0;
298}
299
300
Peter Hagervall174f80d2005-11-08 21:35:18 -0800301static int ca_get_message(struct dst_state *state, struct ca_msg *p_ca_message, void __user *arg)
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700302{
303 u8 i = 0;
304 u32 command = 0;
305
Peter Hagervall174f80d2005-11-08 21:35:18 -0800306 if (copy_from_user(p_ca_message, arg, sizeof (struct ca_msg)))
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700307 return -EFAULT;
308
Mauro Carvalho Chehabc65171c2015-06-05 08:35:09 -0300309 dprintk(verbose, DST_CA_NOTICE, 1, " Message = [%*ph]",
310 3, p_ca_message->msg);
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700311
Mauro Carvalho Chehabc65171c2015-06-05 08:35:09 -0300312 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);
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700318
Mauro Carvalho Chehabc65171c2015-06-05 08:35:09 -0300319 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;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700330 }
331
332 return 0;
333}
334
Manu Abraham7d534212005-07-07 17:57:50 -0700335static int handle_dst_tag(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer, u32 length)
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700336{
Johannes Stezenbach4a2cc122005-05-16 21:54:45 -0700337 if (state->dst_hw_cap & DST_TYPE_HAS_SESSION) {
Manu Abraham94b74102005-09-09 13:02:59 -0700338 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) {
Manu Abrahama427de62005-09-09 13:03:00 -0700342 dprintk(verbose, DST_CA_ERROR, 1, " Message too long ! *** Bailing Out *** !");
Mauro Carvalho Chehab39a7c8a2017-09-01 08:21:04 -0400343 return -EIO;
Manu Abraham94b74102005-09-09 13:02:59 -0700344 }
Manu Abraham7d534212005-07-07 17:57:50 -0700345 hw_buffer->msg[0] = (length & 0xff) + 7;
346 hw_buffer->msg[1] = 0x40;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700347 hw_buffer->msg[2] = 0x03;
348 hw_buffer->msg[3] = 0x00;
Manu Abraham7d534212005-07-07 17:57:50 -0700349 hw_buffer->msg[4] = 0x03;
350 hw_buffer->msg[5] = length & 0xff;
351 hw_buffer->msg[6] = 0x00;
Michael Krufky50c25ff2006-01-09 15:25:34 -0200352
Manu Abraham94b74102005-09-09 13:02:59 -0700353 /*
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);
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700358 }
Michael Krufky50c25ff2006-01-09 15:25:34 -0200359
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700360 return 0;
361}
362
Manu Abraham7d534212005-07-07 17:57:50 -0700363static int write_to_8820(struct dst_state *state, struct ca_msg *hw_buffer, u8 length, u8 reply)
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700364{
Manu Abraham7d534212005-07-07 17:57:50 -0700365 if ((dst_put_ci(state, hw_buffer->msg, length, hw_buffer->msg, reply)) < 0) {
Manu Abrahama427de62005-09-09 13:03:00 -0700366 dprintk(verbose, DST_CA_ERROR, 1, " DST-CI Command failed.");
367 dprintk(verbose, DST_CA_NOTICE, 1, " Resetting DST.");
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700368 rdc_reset_state(state);
Mauro Carvalho Chehab39a7c8a2017-09-01 08:21:04 -0400369 return -EIO;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700370 }
Henrik Sjoberg1f194562006-06-21 16:33:21 -0300371 dprintk(verbose, DST_CA_NOTICE, 1, " DST-CI Command success.");
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700372
373 return 0;
374}
375
Peter Hagervall174f80d2005-11-08 21:35:18 -0800376static u32 asn_1_decode(u8 *asn_1_array)
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700377{
Manu Abraham7d534212005-07-07 17:57:50 -0700378 u8 length_field = 0, word_count = 0, count = 0;
379 u32 length = 0;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700380
Manu Abraham7d534212005-07-07 17:57:50 -0700381 length_field = asn_1_array[0];
Manu Abrahama427de62005-09-09 13:03:00 -0700382 dprintk(verbose, DST_CA_DEBUG, 1, " Length field=[%02x]", length_field);
Manu Abraham7d534212005-07-07 17:57:50 -0700383 if (length_field < 0x80) {
384 length = length_field & 0x7f;
Manu Abrahama427de62005-09-09 13:03:00 -0700385 dprintk(verbose, DST_CA_DEBUG, 1, " Length=[%02x]\n", length);
Manu Abraham7d534212005-07-07 17:57:50 -0700386 } else {
387 word_count = length_field & 0x7f;
388 for (count = 0; count < word_count; count++) {
Raymond Mantchala93a14f12005-11-08 21:35:17 -0800389 length = length << 8;
390 length += asn_1_array[count + 1];
Manu Abrahama427de62005-09-09 13:03:00 -0700391 dprintk(verbose, DST_CA_DEBUG, 1, " Length=[%04x]", length);
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700392 }
393 }
Manu Abraham7d534212005-07-07 17:57:50 -0700394 return length;
395}
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700396
Manu Abraham7d534212005-07-07 17:57:50 -0700397static int debug_string(u8 *msg, u32 length, u32 offset)
398{
399 u32 i;
400
Manu Abrahama427de62005-09-09 13:03:00 -0700401 dprintk(verbose, DST_CA_DEBUG, 0, " String=[ ");
Manu Abraham7d534212005-07-07 17:57:50 -0700402 for (i = offset; i < length; i++)
Manu Abrahama427de62005-09-09 13:03:00 -0700403 dprintk(verbose, DST_CA_DEBUG, 0, "%02x ", msg[i]);
404 dprintk(verbose, DST_CA_DEBUG, 0, "]\n");
Manu Abraham7d534212005-07-07 17:57:50 -0700405
406 return 0;
407}
408
Manu Abraham3e357fd2006-06-21 15:06:49 -0300409
Manu Abraham7d534212005-07-07 17:57:50 -0700410static int ca_set_pmt(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer, u8 reply, u8 query)
411{
Manu Abraham94b74102005-09-09 13:02:59 -0700412 u32 length = 0;
413 u8 tag_length = 8;
Manu Abraham7d534212005-07-07 17:57:50 -0700414
415 length = asn_1_decode(&p_ca_message->msg[3]);
Manu Abrahama427de62005-09-09 13:03:00 -0700416 dprintk(verbose, DST_CA_DEBUG, 1, " CA Message length=[%d]", length);
Manu Abraham94b74102005-09-09 13:02:59 -0700417 debug_string(&p_ca_message->msg[4], length, 0); /* length is excluding tag & length */
Manu Abraham7d534212005-07-07 17:57:50 -0700418
Manu Abraham94b74102005-09-09 13:02:59 -0700419 memset(hw_buffer->msg, '\0', length);
Manu Abraham7d534212005-07-07 17:57:50 -0700420 handle_dst_tag(state, p_ca_message, hw_buffer, length);
Manu Abraham94b74102005-09-09 13:02:59 -0700421 put_checksum(hw_buffer->msg, hw_buffer->msg[0]);
Manu Abraham7d534212005-07-07 17:57:50 -0700422
Manu Abraham94b74102005-09-09 13:02:59 -0700423 debug_string(hw_buffer->msg, (length + tag_length), 0); /* tags too */
424 write_to_8820(state, hw_buffer, (length + tag_length), reply);
Manu Abraham7d534212005-07-07 17:57:50 -0700425
426 return 0;
427}
428
429
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700430/* Board supports CA PMT reply ? */
431static 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
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700438 /* 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) {
Manu Abrahama427de62005-09-09 13:03:00 -0700441 dprintk(verbose, DST_CA_ERROR, 1, " ca_set_pmt.. failed !");
Mauro Carvalho Chehab39a7c8a2017-09-01 08:21:04 -0400442 return -EIO;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700443 }
444
445 /* Process CA PMT Reply */
446 /* will implement soon */
Manu Abrahama427de62005-09-09 13:03:00 -0700447 dprintk(verbose, DST_CA_ERROR, 1, " Not there yet");
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700448 }
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) {
Manu Abrahama427de62005-09-09 13:03:00 -0700452 dprintk(verbose, DST_CA_ERROR, 1, " ca_set_pmt.. failed !");
Mauro Carvalho Chehab39a7c8a2017-09-01 08:21:04 -0400453 return -EIO;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700454 }
Manu Abrahama427de62005-09-09 13:03:00 -0700455 dprintk(verbose, DST_CA_NOTICE, 1, " ca_set_pmt.. success !");
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700456 /* put a dummy message */
457
458 }
459 return 0;
460}
461
Peter Hagervall174f80d2005-11-08 21:35:18 -0800462static int ca_send_message(struct dst_state *state, struct ca_msg *p_ca_message, void __user *arg)
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700463{
Markus Elfring87266502016-12-10 17:56:04 -0200464 int i;
465 u32 command;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700466 struct ca_msg *hw_buffer;
Perceval Anichinif6305582005-11-08 21:35:19 -0800467 int result = 0;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700468
Markus Elfring6e531f12016-12-10 17:50:58 -0200469 hw_buffer = kmalloc(sizeof(*hw_buffer), GFP_KERNEL);
470 if (!hw_buffer)
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700471 return -ENOMEM;
Manu Abrahama427de62005-09-09 13:03:00 -0700472 dprintk(verbose, DST_CA_DEBUG, 1, " ");
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700473
Al Viro94299172005-12-15 09:18:45 +0000474 if (copy_from_user(p_ca_message, arg, sizeof (struct ca_msg))) {
Perceval Anichinif6305582005-11-08 21:35:19 -0800475 result = -EFAULT;
476 goto free_mem_and_exit;
477 }
478
Mauro Carvalho Chehabc65171c2015-06-05 08:35:09 -0300479 /* EN50221 tag */
480 command = 0;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700481
Mauro Carvalho Chehabc65171c2015-06-05 08:35:09 -0300482 for (i = 0; i < 3; i++) {
483 command = command | p_ca_message->msg[i];
484 if (i < 2)
485 command = command << 8;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700486 }
Mauro Carvalho Chehabc65171c2015-06-05 08:35:09 -0300487 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
Perceval Anichinif6305582005-11-08 21:35:19 -0800531free_mem_and_exit:
532 kfree (hw_buffer);
533
534 return result;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700535}
536
Stoyan Gaydarov5ed2b642009-03-24 18:12:47 -0300537static long dst_ca_ioctl(struct file *file, unsigned int cmd, unsigned long ioctl_arg)
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700538{
Hans Verkuilce18c482009-03-28 09:35:40 -0300539 struct dvb_device *dvbdev;
540 struct dst_state *state;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700541 struct ca_slot_info *p_ca_slot_info;
542 struct ca_caps *p_ca_caps;
543 struct ca_msg *p_ca_message;
Peter Hagervall174f80d2005-11-08 21:35:18 -0800544 void __user *arg = (void __user *)ioctl_arg;
Perceval Anichinif6305582005-11-08 21:35:19 -0800545 int result = 0;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700546
Arnd Bergmannadfedd22010-09-11 19:53:25 +0200547 mutex_lock(&dst_ca_mutex);
Joe Perchesabf84382010-07-12 17:50:03 -0300548 dvbdev = file->private_data;
Hans Verkuilce18c482009-03-28 09:35:40 -0300549 state = (struct dst_state *)dvbdev->priv;
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300550 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) {
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300554 result = -ENOMEM;
555 goto free_mem_and_exit;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700556 }
Mauro Carvalho Chehabb57e5572006-06-23 16:13:56 -0300557
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700558 /* We have now only the standard ioctl's, the driver is upposed to handle internals. */
559 switch (cmd) {
Manu Abrahama427de62005-09-09 13:03:00 -0700560 case CA_SEND_MSG:
561 dprintk(verbose, DST_CA_INFO, 1, " Sending message");
Mauro Carvalho Chehab39a7c8a2017-09-01 08:21:04 -0400562 result = ca_send_message(state, p_ca_message, arg);
563
564 if (result < 0) {
Manu Abrahama427de62005-09-09 13:03:00 -0700565 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_SEND_MSG Failed !");
Perceval Anichinif6305582005-11-08 21:35:19 -0800566 goto free_mem_and_exit;
Manu Abrahama427de62005-09-09 13:03:00 -0700567 }
568 break;
569 case CA_GET_MSG:
570 dprintk(verbose, DST_CA_INFO, 1, " Getting message");
Mauro Carvalho Chehab39a7c8a2017-09-01 08:21:04 -0400571 result = ca_get_message(state, p_ca_message, arg);
572 if (result < 0) {
Manu Abrahama427de62005-09-09 13:03:00 -0700573 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_MSG Failed !");
Perceval Anichinif6305582005-11-08 21:35:19 -0800574 goto free_mem_and_exit;
Manu Abrahama427de62005-09-09 13:03:00 -0700575 }
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");
Mauro Carvalho Chehab39a7c8a2017-09-01 08:21:04 -0400585 result = ca_get_slot_info(state, p_ca_slot_info, arg);
586 if (result < 0) {
Manu Abrahama427de62005-09-09 13:03:00 -0700587 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_SLOT_INFO Failed !");
Perceval Anichinif6305582005-11-08 21:35:19 -0800588 result = -1;
589 goto free_mem_and_exit;
Manu Abrahama427de62005-09-09 13:03:00 -0700590 }
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");
Mauro Carvalho Chehab39a7c8a2017-09-01 08:21:04 -0400595 result = ca_get_slot_caps(state, p_ca_caps, arg);
596 if (result < 0) {
Manu Abrahama427de62005-09-09 13:03:00 -0700597 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_CAP Failed !");
Perceval Anichinif6305582005-11-08 21:35:19 -0800598 goto free_mem_and_exit;
Manu Abrahama427de62005-09-09 13:03:00 -0700599 }
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");
Mauro Carvalho Chehab39a7c8a2017-09-01 08:21:04 -0400604 result = ca_get_slot_descr(state, p_ca_message, arg);
605 if (result < 0) {
Manu Abrahama427de62005-09-09 13:03:00 -0700606 dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_DESCR_INFO Failed !");
Perceval Anichinif6305582005-11-08 21:35:19 -0800607 goto free_mem_and_exit;
Manu Abrahama427de62005-09-09 13:03:00 -0700608 }
609 dprintk(verbose, DST_CA_INFO, 1, " -->CA_GET_DESCR_INFO Success !");
610 break;
Manu Abrahama427de62005-09-09 13:03:00 -0700611 default:
Perceval Anichinif6305582005-11-08 21:35:19 -0800612 result = -EOPNOTSUPP;
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300613 }
Perceval Anichinif6305582005-11-08 21:35:19 -0800614 free_mem_and_exit:
615 kfree (p_ca_message);
616 kfree (p_ca_slot_info);
617 kfree (p_ca_caps);
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700618
Arnd Bergmannadfedd22010-09-11 19:53:25 +0200619 mutex_unlock(&dst_ca_mutex);
Perceval Anichinif6305582005-11-08 21:35:19 -0800620 return result;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700621}
622
623static int dst_ca_open(struct inode *inode, struct file *file)
624{
Manu Abrahama427de62005-09-09 13:03:00 -0700625 dprintk(verbose, DST_CA_DEBUG, 1, " Device opened [%p] ", file);
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700626
627 return 0;
628}
629
630static int dst_ca_release(struct inode *inode, struct file *file)
631{
Manu Abrahama427de62005-09-09 13:03:00 -0700632 dprintk(verbose, DST_CA_DEBUG, 1, " Device closed.");
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700633
634 return 0;
635}
636
Al Viro94299172005-12-15 09:18:45 +0000637static ssize_t dst_ca_read(struct file *file, char __user *buffer, size_t length, loff_t *offset)
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700638{
Manu Abrahama427de62005-09-09 13:03:00 -0700639 dprintk(verbose, DST_CA_DEBUG, 1, " Device read.");
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700640
Mauro Carvalho Chehaba896dc72014-09-03 15:30:41 -0300641 return 0;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700642}
643
Al Viro94299172005-12-15 09:18:45 +0000644static ssize_t dst_ca_write(struct file *file, const char __user *buffer, size_t length, loff_t *offset)
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700645{
Manu Abrahama427de62005-09-09 13:03:00 -0700646 dprintk(verbose, DST_CA_DEBUG, 1, " Device write.");
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700647
648 return 0;
649}
650
Jan Engelhardt784e29d2009-01-11 06:12:43 -0300651static const struct file_operations dst_ca_fops = {
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700652 .owner = THIS_MODULE,
Stoyan Gaydarov5ed2b642009-03-24 18:12:47 -0300653 .unlocked_ioctl = dst_ca_ioctl,
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700654 .open = dst_ca_open,
655 .release = dst_ca_release,
656 .read = dst_ca_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200657 .write = dst_ca_write,
658 .llseek = noop_llseek,
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700659};
660
661static struct dvb_device dvbdev_ca = {
662 .priv = NULL,
663 .users = 1,
664 .readers = 1,
665 .writers = 1,
666 .fops = &dst_ca_fops
667};
668
Manu Abrahambbdd11f2006-08-08 15:48:08 -0300669struct dvb_device *dst_ca_attach(struct dst_state *dst, struct dvb_adapter *dvb_adapter)
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700670{
671 struct dvb_device *dvbdev;
Manu Abrahambbdd11f2006-08-08 15:48:08 -0300672
Manu Abrahama427de62005-09-09 13:03:00 -0700673 dprintk(verbose, DST_CA_ERROR, 1, "registering DST-CA device");
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300674 if (dvb_register_device(dvb_adapter, &dvbdev, &dvbdev_ca, dst,
675 DVB_DEVICE_CA, 0) == 0) {
Manu Abrahambbdd11f2006-08-08 15:48:08 -0300676 dst->dst_ca = dvbdev;
677 return dst->dst_ca;
678 }
679
680 return NULL;
Johannes Stezenbach50b215a2005-05-16 21:54:41 -0700681}
682
683EXPORT_SYMBOL(dst_ca_attach);
684
685MODULE_DESCRIPTION("DST DVB-S/T/C Combo CA driver");
686MODULE_AUTHOR("Manu Abraham");
687MODULE_LICENSE("GPL");