dst_ca.c 21 KB

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