dst_ca.c 20 KB

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