dst_ca.c 21 KB

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