dst_ca.c 21 KB

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