dst_ca.c 27 KB

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