dst_ca.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. static unsigned int verbose = 1;
  26. module_param(verbose, int, 0644);
  27. MODULE_PARM_DESC(verbose, "verbose startup messages, default is 1 (yes)");
  28. static unsigned int debug = 1;
  29. module_param(debug, int, 0644);
  30. MODULE_PARM_DESC(dst_ca_debug, "debug messages, default is 0 (yes)");
  31. static unsigned int session;
  32. module_param(session, int, 0644);
  33. MODULE_PARM_DESC(session, "Support for hardware that has multiple sessions, default 0");
  34. static unsigned int new_ca;
  35. module_param(new_ca, int, 0644);
  36. MODULE_PARM_DESC(new_ca, "Support for the new CA interface firmware, default 0");
  37. #define dprintk if (debug) printk
  38. static int ca_set_slot_descr(void)
  39. {
  40. /* We could make this more graceful ? */
  41. return -EOPNOTSUPP;
  42. }
  43. static int ca_set_pid(void)
  44. {
  45. /* We could make this more graceful ? */
  46. return -EOPNOTSUPP;
  47. }
  48. static int put_checksum(u8 *check_string, int length)
  49. {
  50. u8 i = 0, checksum = 0;
  51. if (verbose > 3) {
  52. dprintk("%s: ========================= Checksum calculation ===========================\n", __FUNCTION__);
  53. dprintk("%s: String Length=[0x%02x]\n", __FUNCTION__, length);
  54. dprintk("%s: String=[", __FUNCTION__);
  55. }
  56. while (i < length) {
  57. if (verbose > 3)
  58. dprintk(" %02x", check_string[i]);
  59. checksum += check_string[i];
  60. i++;
  61. }
  62. if (verbose > 3) {
  63. dprintk(" ]\n");
  64. dprintk("%s: Sum=[%02x]\n", __FUNCTION__, checksum);
  65. }
  66. check_string[length] = ~checksum + 1;
  67. if (verbose > 3) {
  68. dprintk("%s: Checksum=[%02x]\n", __FUNCTION__, check_string[length]);
  69. dprintk("%s: ==========================================================================\n", __FUNCTION__);
  70. }
  71. return 0;
  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. dst_comm_init(state);
  77. msleep(65);
  78. if (write_dst(state, data, len)) {
  79. dprintk("%s: Write not successful, trying to recover\n", __FUNCTION__);
  80. dst_error_recovery(state);
  81. return -1;
  82. }
  83. if ((dst_pio_disable(state)) < 0) {
  84. dprintk("%s: DST PIO disable failed.\n", __FUNCTION__);
  85. return -1;
  86. }
  87. if (read_dst(state, &reply, GET_ACK) < 0) {
  88. dprintk("%s: Read not successful, trying to recover\n", __FUNCTION__);
  89. dst_error_recovery(state);
  90. return -1;
  91. }
  92. if (read) {
  93. if (! dst_wait_dst_ready(state, LONG_DELAY)) {
  94. dprintk("%s: 8820 not ready\n", __FUNCTION__);
  95. return -1;
  96. }
  97. if (read_dst(state, ca_string, 128) < 0) { /* Try to make this dynamic */
  98. dprintk("%s: Read not successful, trying to recover\n", __FUNCTION__);
  99. dst_error_recovery(state);
  100. return -1;
  101. }
  102. }
  103. return 0;
  104. }
  105. static int dst_put_ci(struct dst_state *state, u8 *data, int len, u8 *ca_string, int read)
  106. {
  107. u8 dst_ca_comm_err = 0;
  108. while (dst_ca_comm_err < RETRIES) {
  109. dst_comm_init(state);
  110. if (verbose > 2)
  111. dprintk("%s: Put Command\n", __FUNCTION__);
  112. if (dst_ci_command(state, data, ca_string, len, read)) { // If error
  113. dst_error_recovery(state);
  114. dst_ca_comm_err++; // work required here.
  115. }
  116. break;
  117. }
  118. return 0;
  119. }
  120. static int ca_get_app_info(struct dst_state *state)
  121. {
  122. static u8 command[8] = {0x07, 0x40, 0x01, 0x00, 0x01, 0x00, 0x00, 0xff};
  123. put_checksum(&command[0], command[0]);
  124. if ((dst_put_ci(state, command, sizeof(command), state->messages, GET_REPLY)) < 0) {
  125. dprintk("%s: -->dst_put_ci FAILED !\n", __FUNCTION__);
  126. return -1;
  127. }
  128. if (verbose > 1) {
  129. dprintk("%s: -->dst_put_ci SUCCESS !\n", __FUNCTION__);
  130. dprintk("%s: ================================ CI Module Application Info ======================================\n", __FUNCTION__);
  131. dprintk("%s: Application Type=[%d], Application Vendor=[%d], Vendor Code=[%d]\n%s: Application info=[%s]\n",
  132. __FUNCTION__, state->messages[7], (state->messages[8] << 8) | state->messages[9],
  133. (state->messages[10] << 8) | state->messages[11], __FUNCTION__, (char *)(&state->messages[11]));
  134. dprintk("%s: ==================================================================================================\n", __FUNCTION__);
  135. }
  136. return 0;
  137. }
  138. static int ca_get_slot_caps(struct dst_state *state, struct ca_caps *p_ca_caps, void *arg)
  139. {
  140. int i;
  141. u8 slot_cap[256];
  142. static u8 slot_command[8] = {0x07, 0x40, 0x02, 0x00, 0x02, 0x00, 0x00, 0xff};
  143. put_checksum(&slot_command[0], slot_command[0]);
  144. if ((dst_put_ci(state, slot_command, sizeof (slot_command), slot_cap, GET_REPLY)) < 0) {
  145. dprintk("%s: -->dst_put_ci FAILED !\n", __FUNCTION__);
  146. return -1;
  147. }
  148. if (verbose > 1)
  149. dprintk("%s: -->dst_put_ci SUCCESS !\n", __FUNCTION__);
  150. /* Will implement the rest soon */
  151. if (verbose > 1) {
  152. dprintk("%s: Slot cap = [%d]\n", __FUNCTION__, slot_cap[7]);
  153. dprintk("===================================\n");
  154. for (i = 0; i < 8; i++)
  155. dprintk(" %d", slot_cap[i]);
  156. dprintk("\n");
  157. }
  158. p_ca_caps->slot_num = 1;
  159. p_ca_caps->slot_type = 1;
  160. p_ca_caps->descr_num = slot_cap[7];
  161. p_ca_caps->descr_type = 1;
  162. if (copy_to_user((struct ca_caps *)arg, p_ca_caps, sizeof (struct ca_caps))) {
  163. return -EFAULT;
  164. }
  165. return 0;
  166. }
  167. static int ca_get_slot_descr(struct dst_state *state, struct ca_msg *p_ca_message, void *arg)
  168. {
  169. return -EOPNOTSUPP;
  170. }
  171. static int ca_get_slot_info(struct dst_state *state, struct ca_slot_info *p_ca_slot_info, void *arg)
  172. {
  173. int i;
  174. static u8 slot_command[8] = {0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff};
  175. u8 *slot_info = state->rxbuffer;
  176. put_checksum(&slot_command[0], 7);
  177. if ((dst_put_ci(state, slot_command, sizeof (slot_command), slot_info, GET_REPLY)) < 0) {
  178. dprintk("%s: -->dst_put_ci FAILED !\n", __FUNCTION__);
  179. return -1;
  180. }
  181. if (verbose > 1)
  182. dprintk("%s: -->dst_put_ci SUCCESS !\n", __FUNCTION__);
  183. /* Will implement the rest soon */
  184. if (verbose > 1) {
  185. dprintk("%s: Slot info = [%d]\n", __FUNCTION__, slot_info[3]);
  186. dprintk("===================================\n");
  187. for (i = 0; i < 8; i++)
  188. dprintk(" %d", slot_info[i]);
  189. dprintk("\n");
  190. }
  191. if (slot_info[4] & 0x80) {
  192. p_ca_slot_info->flags = CA_CI_MODULE_PRESENT;
  193. p_ca_slot_info->num = 1;
  194. p_ca_slot_info->type = CA_CI;
  195. }
  196. else if (slot_info[4] & 0x40) {
  197. p_ca_slot_info->flags = CA_CI_MODULE_READY;
  198. p_ca_slot_info->num = 1;
  199. p_ca_slot_info->type = CA_CI;
  200. }
  201. else {
  202. p_ca_slot_info->flags = 0;
  203. }
  204. if (copy_to_user((struct ca_slot_info *)arg, p_ca_slot_info, sizeof (struct ca_slot_info))) {
  205. return -EFAULT;
  206. }
  207. return 0;
  208. }
  209. static int ca_get_message(struct dst_state *state, struct ca_msg *p_ca_message, void *arg)
  210. {
  211. u8 i = 0;
  212. u32 command = 0;
  213. if (copy_from_user(p_ca_message, (void *)arg, sizeof (struct ca_msg)))
  214. return -EFAULT;
  215. if (p_ca_message->msg) {
  216. if (verbose > 3)
  217. dprintk("Message = [%02x %02x %02x]\n", p_ca_message->msg[0], p_ca_message->msg[1], p_ca_message->msg[2]);
  218. for (i = 0; i < 3; i++) {
  219. command = command | p_ca_message->msg[i];
  220. if (i < 2)
  221. command = command << 8;
  222. }
  223. if (verbose > 3)
  224. dprintk("%s:Command=[0x%x]\n", __FUNCTION__, command);
  225. switch (command) {
  226. case CA_APP_INFO:
  227. memcpy(p_ca_message->msg, state->messages, 128);
  228. if (copy_to_user((void *)arg, p_ca_message, sizeof (struct ca_msg)) )
  229. return -EFAULT;
  230. break;
  231. }
  232. }
  233. return 0;
  234. }
  235. static int handle_en50221_tag(struct ca_msg *p_ca_message, struct ca_msg *hw_buffer)
  236. {
  237. if (session) {
  238. hw_buffer->msg[2] = p_ca_message->msg[1]; /* MSB */
  239. hw_buffer->msg[3] = p_ca_message->msg[2]; /* LSB */
  240. }
  241. else {
  242. hw_buffer->msg[2] = 0x03;
  243. hw_buffer->msg[3] = 0x00;
  244. }
  245. return 0;
  246. }
  247. static int debug_8820_buffer(struct ca_msg *hw_buffer)
  248. {
  249. unsigned int i;
  250. dprintk("%s:Debug=[", __FUNCTION__);
  251. for (i = 0; i < (hw_buffer->msg[0] + 1); i++)
  252. dprintk(" %02x", hw_buffer->msg[i]);
  253. dprintk("]\n");
  254. return 0;
  255. }
  256. static int write_to_8820(struct dst_state *state, struct ca_msg *hw_buffer, u8 reply)
  257. {
  258. if ((dst_put_ci(state, hw_buffer->msg, (hw_buffer->length + 1), hw_buffer->msg, reply)) < 0) {
  259. dprintk("%s: DST-CI Command failed.\n", __FUNCTION__);
  260. dprintk("%s: Resetting DST.\n", __FUNCTION__);
  261. rdc_reset_state(state);
  262. return -1;
  263. }
  264. if (verbose > 2)
  265. dprintk("%s: DST-CI Command succes.\n", __FUNCTION__);
  266. return 0;
  267. }
  268. static int ca_set_pmt(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer, u8 reply, u8 query)
  269. {
  270. u32 hw_offset, buf_offset, i, k;
  271. u32 program_info_length = 0, es_info_length = 0, length = 0, words = 0;
  272. u8 found_prog_ca_desc = 0, found_stream_ca_desc = 0, error_condition = 0, hw_buffer_length = 0;
  273. if (verbose > 3)
  274. dprintk("%s, p_ca_message length %d (0x%x)\n", __FUNCTION__,p_ca_message->length,p_ca_message->length );
  275. handle_en50221_tag(p_ca_message, hw_buffer); /* EN50221 tag */
  276. /* Handle the length field (variable) */
  277. if (!(p_ca_message->msg[3] & 0x80)) { /* Length = 1 */
  278. length = p_ca_message->msg[3] & 0x7f;
  279. words = 0; /* domi's suggestion */
  280. }
  281. else { /* Length = words */
  282. words = p_ca_message->msg[3] & 0x7f;
  283. for (i = 0; i < words; i++) {
  284. length = length << 8;
  285. length = length | p_ca_message->msg[4 + i];
  286. }
  287. }
  288. if (verbose > 4) {
  289. dprintk("%s:Length=[%d (0x%x)], Words=[%d]\n", __FUNCTION__, length,length, words);
  290. /* Debug Input string */
  291. for (i = 0; i < length; i++)
  292. dprintk(" %02x", p_ca_message->msg[i]);
  293. dprintk("]\n");
  294. }
  295. hw_offset = 7;
  296. buf_offset = words + 4;
  297. /* Program Header */
  298. if (verbose > 4)
  299. dprintk("\n%s:Program Header=[", __FUNCTION__);
  300. for (i = 0; i < 6; i++) {
  301. hw_buffer->msg[hw_offset] = p_ca_message->msg[buf_offset];
  302. if (verbose > 4)
  303. dprintk(" %02x", p_ca_message->msg[buf_offset]);
  304. hw_offset++, buf_offset++, hw_buffer_length++;
  305. }
  306. if (verbose > 4)
  307. dprintk("]\n");
  308. program_info_length = 0;
  309. program_info_length = (((program_info_length | p_ca_message->msg[words + 8]) & 0x0f) << 8) | p_ca_message->msg[words + 9];
  310. if (verbose > 4)
  311. dprintk("%s:Program info Length=[%d][%02x], hw_offset=[%d], buf_offset=[%d] \n",
  312. __FUNCTION__, program_info_length, program_info_length, hw_offset, buf_offset);
  313. if (program_info_length && (program_info_length < 256)) { /* If program_info_length */
  314. hw_buffer->msg[11] = hw_buffer->msg[11] & 0x0f; /* req only 4 bits */
  315. hw_buffer->msg[12] = hw_buffer->msg[12] + 1; /* increment! ASIC bug! */
  316. if (p_ca_message->msg[buf_offset + 1] == 0x09) { /* Check CA descriptor */
  317. found_prog_ca_desc = 1;
  318. if (verbose > 4)
  319. dprintk("%s: Found CA descriptor @ Program level\n", __FUNCTION__);
  320. }
  321. if (found_prog_ca_desc) { /* Command only if CA descriptor */
  322. hw_buffer->msg[13] = p_ca_message->msg[buf_offset]; /* CA PMT command ID */
  323. hw_offset++, buf_offset++, hw_buffer_length++;
  324. }
  325. /* Program descriptors */
  326. if (verbose > 4) {
  327. dprintk("%s:**********>buf_offset=[%d], hw_offset=[%d]\n", __FUNCTION__, buf_offset, hw_offset);
  328. dprintk("%s:Program descriptors=[", __FUNCTION__);
  329. }
  330. while (program_info_length && !error_condition) { /* Copy prog descriptors */
  331. if (program_info_length > p_ca_message->length) { /* Error situation */
  332. dprintk ("%s:\"WARNING\" Length error, line=[%d], prog_info_length=[%d]\n",
  333. __FUNCTION__, __LINE__, program_info_length);
  334. dprintk("%s:\"WARNING\" Bailing out of possible loop\n", __FUNCTION__);
  335. error_condition = 1;
  336. break;
  337. }
  338. hw_buffer->msg[hw_offset] = p_ca_message->msg[buf_offset];
  339. dprintk(" %02x", p_ca_message->msg[buf_offset]);
  340. hw_offset++, buf_offset++, hw_buffer_length++, program_info_length--;
  341. }
  342. if (verbose > 4) {
  343. dprintk("]\n");
  344. dprintk("%s:**********>buf_offset=[%d], hw_offset=[%d]\n", __FUNCTION__, buf_offset, hw_offset);
  345. }
  346. if (found_prog_ca_desc) {
  347. if (!reply) {
  348. hw_buffer->msg[13] = 0x01; /* OK descrambling */
  349. if (verbose > 1)
  350. dprintk("CA PMT Command = OK Descrambling\n");
  351. }
  352. else {
  353. hw_buffer->msg[13] = 0x02; /* Ok MMI */
  354. if (verbose > 1)
  355. dprintk("CA PMT Command = Ok MMI\n");
  356. }
  357. if (query) {
  358. hw_buffer->msg[13] = 0x03; /* Query */
  359. if (verbose > 1)
  360. dprintk("CA PMT Command = CA PMT query\n");
  361. }
  362. }
  363. }
  364. else {
  365. hw_buffer->msg[11] = hw_buffer->msg[11] & 0xf0; /* Don't write to ASIC */
  366. hw_buffer->msg[12] = hw_buffer->msg[12] = 0x00;
  367. }
  368. if (verbose > 4)
  369. dprintk("%s:**********>p_ca_message->length=[%d], buf_offset=[%d], hw_offset=[%d]\n",
  370. __FUNCTION__, p_ca_message->length, buf_offset, hw_offset);
  371. while ((buf_offset < p_ca_message->length) && !error_condition) {
  372. /* Bail out in case of an indefinite loop */
  373. if ((es_info_length > p_ca_message->length) || (buf_offset > p_ca_message->length)) {
  374. dprintk("%s:\"WARNING\" Length error, line=[%d], prog_info_length=[%d], buf_offset=[%d]\n",
  375. __FUNCTION__, __LINE__, program_info_length, buf_offset);
  376. dprintk("%s:\"WARNING\" Bailing out of possible loop\n", __FUNCTION__);
  377. error_condition = 1;
  378. break;
  379. }
  380. /* Stream Header */
  381. for (k = 0; k < 5; k++) {
  382. hw_buffer->msg[hw_offset + k] = p_ca_message->msg[buf_offset + k];
  383. }
  384. es_info_length = 0;
  385. es_info_length = (es_info_length | (p_ca_message->msg[buf_offset + 3] & 0x0f)) << 8 | p_ca_message->msg[buf_offset + 4];
  386. if (verbose > 4) {
  387. dprintk("\n%s:----->Stream header=[%02x %02x %02x %02x %02x]\n", __FUNCTION__,
  388. p_ca_message->msg[buf_offset + 0], p_ca_message->msg[buf_offset + 1],
  389. p_ca_message->msg[buf_offset + 2], p_ca_message->msg[buf_offset + 3],
  390. p_ca_message->msg[buf_offset + 4]);
  391. dprintk("%s:----->Stream type=[%02x], es length=[%d (0x%x)], Chars=[%02x] [%02x], buf_offset=[%d]\n", __FUNCTION__,
  392. p_ca_message->msg[buf_offset + 0], es_info_length, es_info_length,
  393. p_ca_message->msg[buf_offset + 3], p_ca_message->msg[buf_offset + 4], buf_offset);
  394. }
  395. hw_buffer->msg[hw_offset + 3] &= 0x0f; /* req only 4 bits */
  396. if (found_prog_ca_desc) {
  397. hw_buffer->msg[hw_offset + 3] = 0x00;
  398. hw_buffer->msg[hw_offset + 4] = 0x00;
  399. }
  400. hw_offset += 5, buf_offset += 5, hw_buffer_length += 5;
  401. /* Check for CA descriptor */
  402. if (p_ca_message->msg[buf_offset + 1] == 0x09) {
  403. if (verbose > 4)
  404. dprintk("%s:Found CA descriptor @ Stream level\n", __FUNCTION__);
  405. found_stream_ca_desc = 1;
  406. }
  407. /* ES descriptors */
  408. if (es_info_length && !error_condition && !found_prog_ca_desc && found_stream_ca_desc) {
  409. // if (!ca_pmt_done) {
  410. hw_buffer->msg[hw_offset] = p_ca_message->msg[buf_offset]; /* CA PMT cmd(es) */
  411. if (verbose > 4)
  412. printk("%s:----->CA PMT Command ID=[%02x]\n", __FUNCTION__, p_ca_message->msg[buf_offset]);
  413. // hw_offset++, buf_offset++, hw_buffer_length++, es_info_length--, ca_pmt_done = 1;
  414. hw_offset++, buf_offset++, hw_buffer_length++, es_info_length--;
  415. // }
  416. if (verbose > 4)
  417. dprintk("%s:----->ES descriptors=[", __FUNCTION__);
  418. while (es_info_length && !error_condition) { /* ES descriptors */
  419. if ((es_info_length > p_ca_message->length) || (buf_offset > p_ca_message->length)) {
  420. if (verbose > 4) {
  421. dprintk("%s:\"WARNING\" ES Length error, line=[%d], es_info_length=[%d], buf_offset=[%d]\n",
  422. __FUNCTION__, __LINE__, es_info_length, buf_offset);
  423. dprintk("%s:\"WARNING\" Bailing out of possible loop\n", __FUNCTION__);
  424. }
  425. error_condition = 1;
  426. break;
  427. }
  428. hw_buffer->msg[hw_offset] = p_ca_message->msg[buf_offset];
  429. if (verbose > 3)
  430. dprintk("%02x ", hw_buffer->msg[hw_offset]);
  431. hw_offset++, buf_offset++, hw_buffer_length++, es_info_length--;
  432. }
  433. found_stream_ca_desc = 0; /* unset for new streams */
  434. dprintk("]\n");
  435. }
  436. }
  437. /* MCU Magic words */
  438. hw_buffer_length += 7;
  439. hw_buffer->msg[0] = hw_buffer_length;
  440. hw_buffer->msg[1] = 64;
  441. hw_buffer->msg[4] = 3;
  442. hw_buffer->msg[5] = hw_buffer->msg[0] - 7;
  443. hw_buffer->msg[6] = 0;
  444. /* Fix length */
  445. hw_buffer->length = hw_buffer->msg[0];
  446. put_checksum(&hw_buffer->msg[0], hw_buffer->msg[0]);
  447. /* Do the actual write */
  448. if (verbose > 4) {
  449. dprintk("%s:======================DEBUGGING================================\n", __FUNCTION__);
  450. dprintk("%s: Actual Length=[%d]\n", __FUNCTION__, hw_buffer_length);
  451. }
  452. /* Only for debugging! */
  453. if (verbose > 2)
  454. debug_8820_buffer(hw_buffer);
  455. if (verbose > 3)
  456. dprintk("%s: Reply = [%d]\n", __FUNCTION__, reply);
  457. write_to_8820(state, hw_buffer, reply);
  458. return 0;
  459. }
  460. /* Board supports CA PMT reply ? */
  461. static int dst_check_ca_pmt(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer)
  462. {
  463. int ca_pmt_reply_test = 0;
  464. /* Do test board */
  465. /* Not there yet but soon */
  466. /* CA PMT Reply capable */
  467. if (ca_pmt_reply_test) {
  468. if ((ca_set_pmt(state, p_ca_message, hw_buffer, 1, GET_REPLY)) < 0) {
  469. dprintk("%s: ca_set_pmt.. failed !\n", __FUNCTION__);
  470. return -1;
  471. }
  472. /* Process CA PMT Reply */
  473. /* will implement soon */
  474. dprintk("%s: Not there yet\n", __FUNCTION__);
  475. }
  476. /* CA PMT Reply not capable */
  477. if (!ca_pmt_reply_test) {
  478. if ((ca_set_pmt(state, p_ca_message, hw_buffer, 0, NO_REPLY)) < 0) {
  479. dprintk("%s: ca_set_pmt.. failed !\n", __FUNCTION__);
  480. return -1;
  481. }
  482. if (verbose > 3)
  483. dprintk("%s: ca_set_pmt.. success !\n", __FUNCTION__);
  484. /* put a dummy message */
  485. }
  486. return 0;
  487. }
  488. static int ca_send_message(struct dst_state *state, struct ca_msg *p_ca_message, void *arg)
  489. {
  490. int i = 0;
  491. unsigned int ca_message_header_len;
  492. u32 command = 0;
  493. struct ca_msg *hw_buffer;
  494. if ((hw_buffer = (struct ca_msg *) kmalloc(sizeof (struct ca_msg), GFP_KERNEL)) == NULL) {
  495. printk("%s: Memory allocation failure\n", __FUNCTION__);
  496. return -ENOMEM;
  497. }
  498. if (verbose > 3)
  499. dprintk("%s\n", __FUNCTION__);
  500. if (copy_from_user(p_ca_message, (void *)arg, sizeof (struct ca_msg)))
  501. return -EFAULT;
  502. if (p_ca_message->msg) {
  503. ca_message_header_len = p_ca_message->length; /* Restore it back when you are done */
  504. /* EN50221 tag */
  505. command = 0;
  506. for (i = 0; i < 3; i++) {
  507. command = command | p_ca_message->msg[i];
  508. if (i < 2)
  509. command = command << 8;
  510. }
  511. if (verbose > 3)
  512. dprintk("%s:Command=[0x%x]\n", __FUNCTION__, command);
  513. switch (command) {
  514. case CA_PMT:
  515. if (verbose > 3)
  516. dprintk("Command = SEND_CA_PMT\n");
  517. if ((ca_set_pmt(state, p_ca_message, hw_buffer, 0, 0)) < 0) {
  518. dprintk("%s: -->CA_PMT Failed !\n", __FUNCTION__);
  519. return -1;
  520. }
  521. if (verbose > 3)
  522. dprintk("%s: -->CA_PMT Success !\n", __FUNCTION__);
  523. // retval = dummy_set_pmt(state, p_ca_message, hw_buffer, 0, 0);
  524. break;
  525. case CA_PMT_REPLY:
  526. if (verbose > 3)
  527. dprintk("Command = CA_PMT_REPLY\n");
  528. /* Have to handle the 2 basic types of cards here */
  529. if ((dst_check_ca_pmt(state, p_ca_message, hw_buffer)) < 0) {
  530. dprintk("%s: -->CA_PMT_REPLY Failed !\n", __FUNCTION__);
  531. return -1;
  532. }
  533. if (verbose > 3)
  534. dprintk("%s: -->CA_PMT_REPLY Success !\n", __FUNCTION__);
  535. /* Certain boards do behave different ? */
  536. // retval = ca_set_pmt(state, p_ca_message, hw_buffer, 1, 1);
  537. case CA_APP_INFO_ENQUIRY: // only for debugging
  538. if (verbose > 3)
  539. dprintk("%s: Getting Cam Application information\n", __FUNCTION__);
  540. if ((ca_get_app_info(state)) < 0) {
  541. dprintk("%s: -->CA_APP_INFO_ENQUIRY Failed !\n", __FUNCTION__);
  542. return -1;
  543. }
  544. if (verbose > 3)
  545. printk("%s: -->CA_APP_INFO_ENQUIRY Success !\n", __FUNCTION__);
  546. break;
  547. }
  548. }
  549. return 0;
  550. }
  551. static int dst_ca_ioctl(struct inode *inode, struct file *file, unsigned int cmd, void *arg)
  552. {
  553. struct dvb_device* dvbdev = (struct dvb_device*) file->private_data;
  554. struct dst_state* state = (struct dst_state*) dvbdev->priv;
  555. struct ca_slot_info *p_ca_slot_info;
  556. struct ca_caps *p_ca_caps;
  557. struct ca_msg *p_ca_message;
  558. if ((p_ca_message = (struct ca_msg *) kmalloc(sizeof (struct ca_msg), GFP_KERNEL)) == NULL) {
  559. printk("%s: Memory allocation failure\n", __FUNCTION__);
  560. return -ENOMEM;
  561. }
  562. if ((p_ca_slot_info = (struct ca_slot_info *) kmalloc(sizeof (struct ca_slot_info), GFP_KERNEL)) == NULL) {
  563. printk("%s: Memory allocation failure\n", __FUNCTION__);
  564. return -ENOMEM;
  565. }
  566. if ((p_ca_caps = (struct ca_caps *) kmalloc(sizeof (struct ca_caps), GFP_KERNEL)) == NULL) {
  567. printk("%s: Memory allocation failure\n", __FUNCTION__);
  568. return -ENOMEM;
  569. }
  570. /* We have now only the standard ioctl's, the driver is upposed to handle internals. */
  571. switch (cmd) {
  572. case CA_SEND_MSG:
  573. if (verbose > 1)
  574. dprintk("%s: Sending message\n", __FUNCTION__);
  575. if ((ca_send_message(state, p_ca_message, arg)) < 0) {
  576. dprintk("%s: -->CA_SEND_MSG Failed !\n", __FUNCTION__);
  577. return -1;
  578. }
  579. break;
  580. case CA_GET_MSG:
  581. if (verbose > 1)
  582. dprintk("%s: Getting message\n", __FUNCTION__);
  583. if ((ca_get_message(state, p_ca_message, arg)) < 0) {
  584. dprintk("%s: -->CA_GET_MSG Failed !\n", __FUNCTION__);
  585. return -1;
  586. }
  587. if (verbose > 1)
  588. dprintk("%s: -->CA_GET_MSG Success !\n", __FUNCTION__);
  589. break;
  590. case CA_RESET:
  591. if (verbose > 1)
  592. dprintk("%s: Resetting DST\n", __FUNCTION__);
  593. dst_error_bailout(state);
  594. msleep(4000);
  595. break;
  596. case CA_GET_SLOT_INFO:
  597. if (verbose > 1)
  598. dprintk("%s: Getting Slot info\n", __FUNCTION__);
  599. if ((ca_get_slot_info(state, p_ca_slot_info, arg)) < 0) {
  600. dprintk("%s: -->CA_GET_SLOT_INFO Failed !\n", __FUNCTION__);
  601. return -1;
  602. }
  603. if (verbose > 1)
  604. dprintk("%s: -->CA_GET_SLOT_INFO Success !\n", __FUNCTION__);
  605. break;
  606. case CA_GET_CAP:
  607. if (verbose > 1)
  608. dprintk("%s: Getting Slot capabilities\n", __FUNCTION__);
  609. if ((ca_get_slot_caps(state, p_ca_caps, arg)) < 0) {
  610. dprintk("%s: -->CA_GET_CAP Failed !\n", __FUNCTION__);
  611. return -1;
  612. }
  613. if (verbose > 1)
  614. dprintk("%s: -->CA_GET_CAP Success !\n", __FUNCTION__);
  615. break;
  616. case CA_GET_DESCR_INFO:
  617. if (verbose > 1)
  618. dprintk("%s: Getting descrambler description\n", __FUNCTION__);
  619. if ((ca_get_slot_descr(state, p_ca_message, arg)) < 0) {
  620. dprintk("%s: -->CA_GET_DESCR_INFO Failed !\n", __FUNCTION__);
  621. return -1;
  622. }
  623. if (verbose > 1)
  624. dprintk("%s: -->CA_GET_DESCR_INFO Success !\n", __FUNCTION__);
  625. break;
  626. case CA_SET_DESCR:
  627. if (verbose > 1)
  628. dprintk("%s: Setting descrambler\n", __FUNCTION__);
  629. if ((ca_set_slot_descr()) < 0) {
  630. dprintk("%s: -->CA_SET_DESCR Failed !\n", __FUNCTION__);
  631. return -1;
  632. }
  633. if (verbose > 1)
  634. dprintk("%s: -->CA_SET_DESCR Success !\n", __FUNCTION__);
  635. break;
  636. case CA_SET_PID:
  637. if (verbose > 1)
  638. dprintk("%s: Setting PID\n", __FUNCTION__);
  639. if ((ca_set_pid()) < 0) {
  640. dprintk("%s: -->CA_SET_PID Failed !\n", __FUNCTION__);
  641. return -1;
  642. }
  643. if (verbose > 1)
  644. dprintk("%s: -->CA_SET_PID Success !\n", __FUNCTION__);
  645. default:
  646. return -EOPNOTSUPP;
  647. };
  648. return 0;
  649. }
  650. static int dst_ca_open(struct inode *inode, struct file *file)
  651. {
  652. if (verbose > 4)
  653. dprintk("%s:Device opened [%p]\n", __FUNCTION__, file);
  654. try_module_get(THIS_MODULE);
  655. return 0;
  656. }
  657. static int dst_ca_release(struct inode *inode, struct file *file)
  658. {
  659. if (verbose > 4)
  660. dprintk("%s:Device closed.\n", __FUNCTION__);
  661. module_put(THIS_MODULE);
  662. return 0;
  663. }
  664. static int dst_ca_read(struct file *file, char __user * buffer, size_t length, loff_t * offset)
  665. {
  666. int bytes_read = 0;
  667. if (verbose > 4)
  668. dprintk("%s:Device read.\n", __FUNCTION__);
  669. return bytes_read;
  670. }
  671. static int dst_ca_write(struct file *file, const char __user * buffer, size_t length, loff_t * offset)
  672. {
  673. if (verbose > 4)
  674. dprintk("%s:Device write.\n", __FUNCTION__);
  675. return 0;
  676. }
  677. static struct file_operations dst_ca_fops = {
  678. .owner = THIS_MODULE,
  679. .ioctl = (void *)dst_ca_ioctl,
  680. .open = dst_ca_open,
  681. .release = dst_ca_release,
  682. .read = dst_ca_read,
  683. .write = dst_ca_write
  684. };
  685. static struct dvb_device dvbdev_ca = {
  686. .priv = NULL,
  687. .users = 1,
  688. .readers = 1,
  689. .writers = 1,
  690. .fops = &dst_ca_fops
  691. };
  692. int dst_ca_attach(struct dst_state *dst, struct dvb_adapter *dvb_adapter)
  693. {
  694. struct dvb_device *dvbdev;
  695. if (verbose > 4)
  696. dprintk("%s:registering DST-CA device\n", __FUNCTION__);
  697. dvb_register_device(dvb_adapter, &dvbdev, &dvbdev_ca, dst, DVB_DEVICE_CA);
  698. return 0;
  699. }
  700. EXPORT_SYMBOL(dst_ca_attach);
  701. MODULE_DESCRIPTION("DST DVB-S/T/C Combo CA driver");
  702. MODULE_AUTHOR("Manu Abraham");
  703. MODULE_LICENSE("GPL");