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