altera-ci.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * altera-ci.c
  3. *
  4. * CI driver in conjunction with NetUp Dual DVB-T/C RF CI card
  5. *
  6. * Copyright (C) 2010,2011 NetUP Inc.
  7. * Copyright (C) 2010,2011 Igor M. Liplianin <liplianin@netup.ru>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. *
  18. * GNU General Public License for more details.
  19. */
  20. /*
  21. * currently cx23885 GPIO's used.
  22. * GPIO-0 ~INT in
  23. * GPIO-1 TMS out
  24. * GPIO-2 ~reset chips out
  25. * GPIO-3 to GPIO-10 data/addr for CA in/out
  26. * GPIO-11 ~CS out
  27. * GPIO-12 AD_RG out
  28. * GPIO-13 ~WR out
  29. * GPIO-14 ~RD out
  30. * GPIO-15 ~RDY in
  31. * GPIO-16 TCK out
  32. * GPIO-17 TDO in
  33. * GPIO-18 TDI out
  34. */
  35. /*
  36. * Bit definitions for MC417_RWD and MC417_OEN registers
  37. * bits 31-16
  38. * +-----------+
  39. * | Reserved |
  40. * +-----------+
  41. * bit 15 bit 14 bit 13 bit 12 bit 11 bit 10 bit 9 bit 8
  42. * +-------+-------+-------+-------+-------+-------+-------+-------+
  43. * | TDI | TDO | TCK | RDY# | #RD | #WR | AD_RG | #CS |
  44. * +-------+-------+-------+-------+-------+-------+-------+-------+
  45. * bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
  46. * +-------+-------+-------+-------+-------+-------+-------+-------+
  47. * | DATA7| DATA6| DATA5| DATA4| DATA3| DATA2| DATA1| DATA0|
  48. * +-------+-------+-------+-------+-------+-------+-------+-------+
  49. */
  50. #include <dvb_demux.h>
  51. #include <dvb_frontend.h>
  52. #include "altera-ci.h"
  53. #include "dvb_ca_en50221.h"
  54. /* FPGA regs */
  55. #define NETUP_CI_INT_CTRL 0x00
  56. #define NETUP_CI_BUSCTRL2 0x01
  57. #define NETUP_CI_ADDR0 0x04
  58. #define NETUP_CI_ADDR1 0x05
  59. #define NETUP_CI_DATA 0x06
  60. #define NETUP_CI_BUSCTRL 0x07
  61. #define NETUP_CI_PID_ADDR0 0x08
  62. #define NETUP_CI_PID_ADDR1 0x09
  63. #define NETUP_CI_PID_DATA 0x0a
  64. #define NETUP_CI_TSA_DIV 0x0c
  65. #define NETUP_CI_TSB_DIV 0x0d
  66. #define NETUP_CI_REVISION 0x0f
  67. /* const for ci op */
  68. #define NETUP_CI_FLG_CTL 1
  69. #define NETUP_CI_FLG_RD 1
  70. #define NETUP_CI_FLG_AD 1
  71. static unsigned int ci_dbg;
  72. module_param(ci_dbg, int, 0644);
  73. MODULE_PARM_DESC(ci_dbg, "Enable CI debugging");
  74. static unsigned int pid_dbg;
  75. module_param(pid_dbg, int, 0644);
  76. MODULE_PARM_DESC(pid_dbg, "Enable PID filtering debugging");
  77. MODULE_DESCRIPTION("altera FPGA CI module");
  78. MODULE_AUTHOR("Igor M. Liplianin <liplianin@netup.ru>");
  79. MODULE_LICENSE("GPL");
  80. #define ci_dbg_print(args...) \
  81. do { \
  82. if (ci_dbg) \
  83. printk(KERN_DEBUG args); \
  84. } while (0)
  85. #define pid_dbg_print(args...) \
  86. do { \
  87. if (pid_dbg) \
  88. printk(KERN_DEBUG args); \
  89. } while (0)
  90. struct altera_ci_state;
  91. struct netup_hw_pid_filter;
  92. struct fpga_internal {
  93. void *dev;
  94. struct mutex fpga_mutex;/* two CI's on the same fpga */
  95. struct netup_hw_pid_filter *pid_filt[2];
  96. struct altera_ci_state *state[2];
  97. struct work_struct work;
  98. int (*fpga_rw) (void *dev, int flag, int data, int rw);
  99. int cis_used;
  100. int filts_used;
  101. int strt_wrk;
  102. };
  103. /* stores all private variables for communication with CI */
  104. struct altera_ci_state {
  105. struct fpga_internal *internal;
  106. struct dvb_ca_en50221 ca;
  107. int status;
  108. int nr;
  109. };
  110. /* stores all private variables for hardware pid filtering */
  111. struct netup_hw_pid_filter {
  112. struct fpga_internal *internal;
  113. struct dvb_demux *demux;
  114. /* save old functions */
  115. int (*start_feed)(struct dvb_demux_feed *feed);
  116. int (*stop_feed)(struct dvb_demux_feed *feed);
  117. int status;
  118. int nr;
  119. };
  120. /* internal params node */
  121. struct fpga_inode {
  122. /* pointer for internal params, one for each pair of CI's */
  123. struct fpga_internal *internal;
  124. struct fpga_inode *next_inode;
  125. };
  126. /* first internal params */
  127. static struct fpga_inode *fpga_first_inode;
  128. /* find chip by dev */
  129. static struct fpga_inode *find_inode(void *dev)
  130. {
  131. struct fpga_inode *temp_chip = fpga_first_inode;
  132. if (temp_chip == NULL)
  133. return temp_chip;
  134. /*
  135. Search for the last fpga CI chip or
  136. find it by dev */
  137. while ((temp_chip != NULL) &&
  138. (temp_chip->internal->dev != dev))
  139. temp_chip = temp_chip->next_inode;
  140. return temp_chip;
  141. }
  142. /* check demux */
  143. static struct fpga_internal *check_filter(struct fpga_internal *temp_int,
  144. void *demux_dev, int filt_nr)
  145. {
  146. if (temp_int == NULL)
  147. return NULL;
  148. if ((temp_int->pid_filt[filt_nr]) == NULL)
  149. return NULL;
  150. if (temp_int->pid_filt[filt_nr]->demux == demux_dev)
  151. return temp_int;
  152. return NULL;
  153. }
  154. /* find chip by demux */
  155. static struct fpga_inode *find_dinode(void *demux_dev)
  156. {
  157. struct fpga_inode *temp_chip = fpga_first_inode;
  158. struct fpga_internal *temp_int;
  159. /*
  160. * Search of the last fpga CI chip or
  161. * find it by demux
  162. */
  163. while (temp_chip != NULL) {
  164. if (temp_chip->internal != NULL) {
  165. temp_int = temp_chip->internal;
  166. if (check_filter(temp_int, demux_dev, 0))
  167. break;
  168. if (check_filter(temp_int, demux_dev, 1))
  169. break;
  170. }
  171. temp_chip = temp_chip->next_inode;
  172. }
  173. return temp_chip;
  174. }
  175. /* deallocating chip */
  176. static void remove_inode(struct fpga_internal *internal)
  177. {
  178. struct fpga_inode *prev_node = fpga_first_inode;
  179. struct fpga_inode *del_node = find_inode(internal->dev);
  180. if (del_node != NULL) {
  181. if (del_node == fpga_first_inode) {
  182. fpga_first_inode = del_node->next_inode;
  183. } else {
  184. while (prev_node->next_inode != del_node)
  185. prev_node = prev_node->next_inode;
  186. if (del_node->next_inode == NULL)
  187. prev_node->next_inode = NULL;
  188. else
  189. prev_node->next_inode =
  190. prev_node->next_inode->next_inode;
  191. }
  192. kfree(del_node);
  193. }
  194. }
  195. /* allocating new chip */
  196. static struct fpga_inode *append_internal(struct fpga_internal *internal)
  197. {
  198. struct fpga_inode *new_node = fpga_first_inode;
  199. if (new_node == NULL) {
  200. new_node = kmalloc(sizeof(struct fpga_inode), GFP_KERNEL);
  201. fpga_first_inode = new_node;
  202. } else {
  203. while (new_node->next_inode != NULL)
  204. new_node = new_node->next_inode;
  205. new_node->next_inode =
  206. kmalloc(sizeof(struct fpga_inode), GFP_KERNEL);
  207. if (new_node->next_inode != NULL)
  208. new_node = new_node->next_inode;
  209. else
  210. new_node = NULL;
  211. }
  212. if (new_node != NULL) {
  213. new_node->internal = internal;
  214. new_node->next_inode = NULL;
  215. }
  216. return new_node;
  217. }
  218. static int netup_fpga_op_rw(struct fpga_internal *inter, int addr,
  219. u8 val, u8 read)
  220. {
  221. inter->fpga_rw(inter->dev, NETUP_CI_FLG_AD, addr, 0);
  222. return inter->fpga_rw(inter->dev, 0, val, read);
  223. }
  224. /* flag - mem/io, read - read/write */
  225. static int altera_ci_op_cam(struct dvb_ca_en50221 *en50221, int slot,
  226. u8 flag, u8 read, int addr, u8 val)
  227. {
  228. struct altera_ci_state *state = en50221->data;
  229. struct fpga_internal *inter = state->internal;
  230. u8 store;
  231. int mem = 0;
  232. if (0 != slot)
  233. return -EINVAL;
  234. mutex_lock(&inter->fpga_mutex);
  235. netup_fpga_op_rw(inter, NETUP_CI_ADDR0, ((addr << 1) & 0xfe), 0);
  236. netup_fpga_op_rw(inter, NETUP_CI_ADDR1, ((addr >> 7) & 0x7f), 0);
  237. store = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, 0, NETUP_CI_FLG_RD);
  238. store &= 0x0f;
  239. store |= ((state->nr << 7) | (flag << 6));
  240. netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, store, 0);
  241. mem = netup_fpga_op_rw(inter, NETUP_CI_DATA, val, read);
  242. mutex_unlock(&inter->fpga_mutex);
  243. ci_dbg_print("%s: %s: addr=[0x%02x], %s=%x\n", __func__,
  244. (read) ? "read" : "write", addr,
  245. (flag == NETUP_CI_FLG_CTL) ? "ctl" : "mem",
  246. (read) ? mem : val);
  247. return mem;
  248. }
  249. static int altera_ci_read_attribute_mem(struct dvb_ca_en50221 *en50221,
  250. int slot, int addr)
  251. {
  252. return altera_ci_op_cam(en50221, slot, 0, NETUP_CI_FLG_RD, addr, 0);
  253. }
  254. static int altera_ci_write_attribute_mem(struct dvb_ca_en50221 *en50221,
  255. int slot, int addr, u8 data)
  256. {
  257. return altera_ci_op_cam(en50221, slot, 0, 0, addr, data);
  258. }
  259. static int altera_ci_read_cam_ctl(struct dvb_ca_en50221 *en50221,
  260. int slot, u8 addr)
  261. {
  262. return altera_ci_op_cam(en50221, slot, NETUP_CI_FLG_CTL,
  263. NETUP_CI_FLG_RD, addr, 0);
  264. }
  265. static int altera_ci_write_cam_ctl(struct dvb_ca_en50221 *en50221, int slot,
  266. u8 addr, u8 data)
  267. {
  268. return altera_ci_op_cam(en50221, slot, NETUP_CI_FLG_CTL, 0, addr, data);
  269. }
  270. static int altera_ci_slot_reset(struct dvb_ca_en50221 *en50221, int slot)
  271. {
  272. struct altera_ci_state *state = en50221->data;
  273. struct fpga_internal *inter = state->internal;
  274. /* reasonable timeout for CI reset is 10 seconds */
  275. unsigned long t_out = jiffies + msecs_to_jiffies(9999);
  276. int ret;
  277. ci_dbg_print("%s\n", __func__);
  278. if (0 != slot)
  279. return -EINVAL;
  280. mutex_lock(&inter->fpga_mutex);
  281. ret = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, 0, NETUP_CI_FLG_RD);
  282. netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL,
  283. (ret & 0xcf) | (1 << (5 - state->nr)), 0);
  284. mutex_unlock(&inter->fpga_mutex);
  285. for (;;) {
  286. mdelay(50);
  287. mutex_lock(&inter->fpga_mutex);
  288. ret = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL,
  289. 0, NETUP_CI_FLG_RD);
  290. mutex_unlock(&inter->fpga_mutex);
  291. if ((ret & (1 << (5 - state->nr))) == 0)
  292. break;
  293. if (time_after(jiffies, t_out))
  294. break;
  295. }
  296. ci_dbg_print("%s: %d msecs\n", __func__,
  297. jiffies_to_msecs(jiffies + msecs_to_jiffies(9999) - t_out));
  298. return 0;
  299. }
  300. static int altera_ci_slot_shutdown(struct dvb_ca_en50221 *en50221, int slot)
  301. {
  302. /* not implemented */
  303. return 0;
  304. }
  305. static int altera_ci_slot_ts_ctl(struct dvb_ca_en50221 *en50221, int slot)
  306. {
  307. struct altera_ci_state *state = en50221->data;
  308. struct fpga_internal *inter = state->internal;
  309. int ret;
  310. ci_dbg_print("%s\n", __func__);
  311. if (0 != slot)
  312. return -EINVAL;
  313. mutex_lock(&inter->fpga_mutex);
  314. ret = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, 0, NETUP_CI_FLG_RD);
  315. netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL,
  316. (ret & 0x0f) | (1 << (3 - state->nr)), 0);
  317. mutex_unlock(&inter->fpga_mutex);
  318. return 0;
  319. }
  320. /* work handler */
  321. static void netup_read_ci_status(struct work_struct *work)
  322. {
  323. struct fpga_internal *inter =
  324. container_of(work, struct fpga_internal, work);
  325. int ret;
  326. ci_dbg_print("%s\n", __func__);
  327. mutex_lock(&inter->fpga_mutex);
  328. /* ack' irq */
  329. ret = netup_fpga_op_rw(inter, NETUP_CI_INT_CTRL, 0, NETUP_CI_FLG_RD);
  330. ret = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, 0, NETUP_CI_FLG_RD);
  331. mutex_unlock(&inter->fpga_mutex);
  332. if (inter->state[1] != NULL) {
  333. inter->state[1]->status =
  334. ((ret & 1) == 0 ?
  335. DVB_CA_EN50221_POLL_CAM_PRESENT |
  336. DVB_CA_EN50221_POLL_CAM_READY : 0);
  337. ci_dbg_print("%s: setting CI[1] status = 0x%x\n",
  338. __func__, inter->state[1]->status);
  339. }
  340. if (inter->state[0] != NULL) {
  341. inter->state[0]->status =
  342. ((ret & 2) == 0 ?
  343. DVB_CA_EN50221_POLL_CAM_PRESENT |
  344. DVB_CA_EN50221_POLL_CAM_READY : 0);
  345. ci_dbg_print("%s: setting CI[0] status = 0x%x\n",
  346. __func__, inter->state[0]->status);
  347. }
  348. }
  349. /* CI irq handler */
  350. int altera_ci_irq(void *dev)
  351. {
  352. struct fpga_inode *temp_int = NULL;
  353. struct fpga_internal *inter = NULL;
  354. ci_dbg_print("%s\n", __func__);
  355. if (dev != NULL) {
  356. temp_int = find_inode(dev);
  357. if (temp_int != NULL) {
  358. inter = temp_int->internal;
  359. schedule_work(&inter->work);
  360. }
  361. }
  362. return 1;
  363. }
  364. EXPORT_SYMBOL(altera_ci_irq);
  365. static int altera_poll_ci_slot_status(struct dvb_ca_en50221 *en50221,
  366. int slot, int open)
  367. {
  368. struct altera_ci_state *state = en50221->data;
  369. if (0 != slot)
  370. return -EINVAL;
  371. return state->status;
  372. }
  373. static void altera_hw_filt_release(void *main_dev, int filt_nr)
  374. {
  375. struct fpga_inode *temp_int = find_inode(main_dev);
  376. struct netup_hw_pid_filter *pid_filt = NULL;
  377. ci_dbg_print("%s\n", __func__);
  378. if (temp_int != NULL) {
  379. pid_filt = temp_int->internal->pid_filt[filt_nr - 1];
  380. /* stored old feed controls */
  381. pid_filt->demux->start_feed = pid_filt->start_feed;
  382. pid_filt->demux->stop_feed = pid_filt->stop_feed;
  383. if (((--(temp_int->internal->filts_used)) <= 0) &&
  384. ((temp_int->internal->cis_used) <= 0)) {
  385. ci_dbg_print("%s: Actually removing\n", __func__);
  386. remove_inode(temp_int->internal);
  387. kfree(pid_filt->internal);
  388. }
  389. kfree(pid_filt);
  390. }
  391. }
  392. void altera_ci_release(void *dev, int ci_nr)
  393. {
  394. struct fpga_inode *temp_int = find_inode(dev);
  395. struct altera_ci_state *state = NULL;
  396. ci_dbg_print("%s\n", __func__);
  397. if (temp_int != NULL) {
  398. state = temp_int->internal->state[ci_nr - 1];
  399. altera_hw_filt_release(dev, ci_nr);
  400. if (((temp_int->internal->filts_used) <= 0) &&
  401. ((--(temp_int->internal->cis_used)) <= 0)) {
  402. ci_dbg_print("%s: Actually removing\n", __func__);
  403. remove_inode(temp_int->internal);
  404. kfree(state->internal);
  405. }
  406. if (state != NULL) {
  407. if (state->ca.data != NULL)
  408. dvb_ca_en50221_release(&state->ca);
  409. kfree(state);
  410. }
  411. }
  412. }
  413. EXPORT_SYMBOL(altera_ci_release);
  414. static void altera_pid_control(struct netup_hw_pid_filter *pid_filt,
  415. u16 pid, int onoff)
  416. {
  417. struct fpga_internal *inter = pid_filt->internal;
  418. u8 store = 0;
  419. /* pid 0-0x1f always enabled, don't touch them */
  420. if ((pid == 0x2000) || (pid < 0x20))
  421. return;
  422. mutex_lock(&inter->fpga_mutex);
  423. netup_fpga_op_rw(inter, NETUP_CI_PID_ADDR0, (pid >> 3) & 0xff, 0);
  424. netup_fpga_op_rw(inter, NETUP_CI_PID_ADDR1,
  425. ((pid >> 11) & 0x03) | (pid_filt->nr << 2), 0);
  426. store = netup_fpga_op_rw(inter, NETUP_CI_PID_DATA, 0, NETUP_CI_FLG_RD);
  427. if (onoff)/* 0 - on, 1 - off */
  428. store |= (1 << (pid & 7));
  429. else
  430. store &= ~(1 << (pid & 7));
  431. netup_fpga_op_rw(inter, NETUP_CI_PID_DATA, store, 0);
  432. mutex_unlock(&inter->fpga_mutex);
  433. pid_dbg_print("%s: (%d) set pid: %5d 0x%04x '%s'\n", __func__,
  434. pid_filt->nr, pid, pid, onoff ? "off" : "on");
  435. }
  436. static void altera_toggle_fullts_streaming(struct netup_hw_pid_filter *pid_filt,
  437. int filt_nr, int onoff)
  438. {
  439. struct fpga_internal *inter = pid_filt->internal;
  440. u8 store = 0;
  441. int i;
  442. pid_dbg_print("%s: pid_filt->nr[%d] now %s\n", __func__, pid_filt->nr,
  443. onoff ? "off" : "on");
  444. if (onoff)/* 0 - on, 1 - off */
  445. store = 0xff;/* ignore pid */
  446. else
  447. store = 0;/* enable pid */
  448. mutex_lock(&inter->fpga_mutex);
  449. for (i = 0; i < 1024; i++) {
  450. netup_fpga_op_rw(inter, NETUP_CI_PID_ADDR0, i & 0xff, 0);
  451. netup_fpga_op_rw(inter, NETUP_CI_PID_ADDR1,
  452. ((i >> 8) & 0x03) | (pid_filt->nr << 2), 0);
  453. /* pid 0-0x1f always enabled */
  454. netup_fpga_op_rw(inter, NETUP_CI_PID_DATA,
  455. (i > 3 ? store : 0), 0);
  456. }
  457. mutex_unlock(&inter->fpga_mutex);
  458. }
  459. static int altera_pid_feed_control(void *demux_dev, int filt_nr,
  460. struct dvb_demux_feed *feed, int onoff)
  461. {
  462. struct fpga_inode *temp_int = find_dinode(demux_dev);
  463. struct fpga_internal *inter = temp_int->internal;
  464. struct netup_hw_pid_filter *pid_filt = inter->pid_filt[filt_nr - 1];
  465. altera_pid_control(pid_filt, feed->pid, onoff ? 0 : 1);
  466. /* call old feed proc's */
  467. if (onoff)
  468. pid_filt->start_feed(feed);
  469. else
  470. pid_filt->stop_feed(feed);
  471. if (feed->pid == 0x2000)
  472. altera_toggle_fullts_streaming(pid_filt, filt_nr,
  473. onoff ? 0 : 1);
  474. return 0;
  475. }
  476. static int altera_ci_start_feed(struct dvb_demux_feed *feed, int num)
  477. {
  478. altera_pid_feed_control(feed->demux, num, feed, 1);
  479. return 0;
  480. }
  481. static int altera_ci_stop_feed(struct dvb_demux_feed *feed, int num)
  482. {
  483. altera_pid_feed_control(feed->demux, num, feed, 0);
  484. return 0;
  485. }
  486. static int altera_ci_start_feed_1(struct dvb_demux_feed *feed)
  487. {
  488. return altera_ci_start_feed(feed, 1);
  489. }
  490. static int altera_ci_stop_feed_1(struct dvb_demux_feed *feed)
  491. {
  492. return altera_ci_stop_feed(feed, 1);
  493. }
  494. static int altera_ci_start_feed_2(struct dvb_demux_feed *feed)
  495. {
  496. return altera_ci_start_feed(feed, 2);
  497. }
  498. static int altera_ci_stop_feed_2(struct dvb_demux_feed *feed)
  499. {
  500. return altera_ci_stop_feed(feed, 2);
  501. }
  502. static int altera_hw_filt_init(struct altera_ci_config *config, int hw_filt_nr)
  503. {
  504. struct netup_hw_pid_filter *pid_filt = NULL;
  505. struct fpga_inode *temp_int = find_inode(config->dev);
  506. struct fpga_internal *inter = NULL;
  507. int ret = 0;
  508. pid_filt = kzalloc(sizeof(struct netup_hw_pid_filter), GFP_KERNEL);
  509. ci_dbg_print("%s\n", __func__);
  510. if (!pid_filt) {
  511. ret = -ENOMEM;
  512. goto err;
  513. }
  514. if (temp_int != NULL) {
  515. inter = temp_int->internal;
  516. (inter->filts_used)++;
  517. ci_dbg_print("%s: Find Internal Structure!\n", __func__);
  518. } else {
  519. inter = kzalloc(sizeof(struct fpga_internal), GFP_KERNEL);
  520. if (!inter) {
  521. ret = -ENOMEM;
  522. goto err;
  523. }
  524. temp_int = append_internal(inter);
  525. if (!temp_int) {
  526. ret = -ENOMEM;
  527. goto err;
  528. }
  529. inter->filts_used = 1;
  530. inter->dev = config->dev;
  531. inter->fpga_rw = config->fpga_rw;
  532. mutex_init(&inter->fpga_mutex);
  533. inter->strt_wrk = 1;
  534. ci_dbg_print("%s: Create New Internal Structure!\n", __func__);
  535. }
  536. ci_dbg_print("%s: setting hw pid filter = %p for ci = %d\n", __func__,
  537. pid_filt, hw_filt_nr - 1);
  538. inter->pid_filt[hw_filt_nr - 1] = pid_filt;
  539. pid_filt->demux = config->demux;
  540. pid_filt->internal = inter;
  541. pid_filt->nr = hw_filt_nr - 1;
  542. /* store old feed controls */
  543. pid_filt->start_feed = config->demux->start_feed;
  544. pid_filt->stop_feed = config->demux->stop_feed;
  545. /* replace with new feed controls */
  546. if (hw_filt_nr == 1) {
  547. pid_filt->demux->start_feed = altera_ci_start_feed_1;
  548. pid_filt->demux->stop_feed = altera_ci_stop_feed_1;
  549. } else if (hw_filt_nr == 2) {
  550. pid_filt->demux->start_feed = altera_ci_start_feed_2;
  551. pid_filt->demux->stop_feed = altera_ci_stop_feed_2;
  552. }
  553. altera_toggle_fullts_streaming(pid_filt, 0, 1);
  554. return 0;
  555. err:
  556. ci_dbg_print("%s: Can't init hardware filter: Error %d\n",
  557. __func__, ret);
  558. kfree(pid_filt);
  559. kfree(inter);
  560. return ret;
  561. }
  562. int altera_ci_init(struct altera_ci_config *config, int ci_nr)
  563. {
  564. struct altera_ci_state *state;
  565. struct fpga_inode *temp_int = find_inode(config->dev);
  566. struct fpga_internal *inter = NULL;
  567. int ret = 0;
  568. u8 store = 0;
  569. state = kzalloc(sizeof(struct altera_ci_state), GFP_KERNEL);
  570. ci_dbg_print("%s\n", __func__);
  571. if (!state) {
  572. ret = -ENOMEM;
  573. goto err;
  574. }
  575. if (temp_int != NULL) {
  576. inter = temp_int->internal;
  577. (inter->cis_used)++;
  578. inter->fpga_rw = config->fpga_rw;
  579. ci_dbg_print("%s: Find Internal Structure!\n", __func__);
  580. } else {
  581. inter = kzalloc(sizeof(struct fpga_internal), GFP_KERNEL);
  582. if (!inter) {
  583. ret = -ENOMEM;
  584. goto err;
  585. }
  586. temp_int = append_internal(inter);
  587. if (!temp_int) {
  588. ret = -ENOMEM;
  589. goto err;
  590. }
  591. inter->cis_used = 1;
  592. inter->dev = config->dev;
  593. inter->fpga_rw = config->fpga_rw;
  594. mutex_init(&inter->fpga_mutex);
  595. inter->strt_wrk = 1;
  596. ci_dbg_print("%s: Create New Internal Structure!\n", __func__);
  597. }
  598. ci_dbg_print("%s: setting state = %p for ci = %d\n", __func__,
  599. state, ci_nr - 1);
  600. state->internal = inter;
  601. state->nr = ci_nr - 1;
  602. state->ca.owner = THIS_MODULE;
  603. state->ca.read_attribute_mem = altera_ci_read_attribute_mem;
  604. state->ca.write_attribute_mem = altera_ci_write_attribute_mem;
  605. state->ca.read_cam_control = altera_ci_read_cam_ctl;
  606. state->ca.write_cam_control = altera_ci_write_cam_ctl;
  607. state->ca.slot_reset = altera_ci_slot_reset;
  608. state->ca.slot_shutdown = altera_ci_slot_shutdown;
  609. state->ca.slot_ts_enable = altera_ci_slot_ts_ctl;
  610. state->ca.poll_slot_status = altera_poll_ci_slot_status;
  611. state->ca.data = state;
  612. ret = dvb_ca_en50221_init(config->adapter,
  613. &state->ca,
  614. /* flags */ 0,
  615. /* n_slots */ 1);
  616. if (0 != ret)
  617. goto err;
  618. inter->state[ci_nr - 1] = state;
  619. altera_hw_filt_init(config, ci_nr);
  620. if (inter->strt_wrk) {
  621. INIT_WORK(&inter->work, netup_read_ci_status);
  622. inter->strt_wrk = 0;
  623. }
  624. ci_dbg_print("%s: CI initialized!\n", __func__);
  625. mutex_lock(&inter->fpga_mutex);
  626. /* Enable div */
  627. netup_fpga_op_rw(inter, NETUP_CI_TSA_DIV, 0x0, 0);
  628. netup_fpga_op_rw(inter, NETUP_CI_TSB_DIV, 0x0, 0);
  629. /* enable TS out */
  630. store = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, 0, NETUP_CI_FLG_RD);
  631. store |= (3 << 4);
  632. netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, store, 0);
  633. ret = netup_fpga_op_rw(inter, NETUP_CI_REVISION, 0, NETUP_CI_FLG_RD);
  634. /* enable irq */
  635. netup_fpga_op_rw(inter, NETUP_CI_INT_CTRL, 0x44, 0);
  636. mutex_unlock(&inter->fpga_mutex);
  637. ci_dbg_print("%s: NetUP CI Revision = 0x%x\n", __func__, ret);
  638. schedule_work(&inter->work);
  639. return 0;
  640. err:
  641. ci_dbg_print("%s: Cannot initialize CI: Error %d.\n", __func__, ret);
  642. kfree(state);
  643. kfree(inter);
  644. return ret;
  645. }
  646. EXPORT_SYMBOL(altera_ci_init);
  647. int altera_ci_tuner_reset(void *dev, int ci_nr)
  648. {
  649. struct fpga_inode *temp_int = find_inode(dev);
  650. struct fpga_internal *inter = NULL;
  651. u8 store;
  652. ci_dbg_print("%s\n", __func__);
  653. if (temp_int == NULL)
  654. return -1;
  655. if (temp_int->internal == NULL)
  656. return -1;
  657. inter = temp_int->internal;
  658. mutex_lock(&inter->fpga_mutex);
  659. store = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, 0, NETUP_CI_FLG_RD);
  660. store &= ~(4 << (2 - ci_nr));
  661. netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, store, 0);
  662. msleep(100);
  663. store |= (4 << (2 - ci_nr));
  664. netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, store, 0);
  665. mutex_unlock(&inter->fpga_mutex);
  666. return 0;
  667. }
  668. EXPORT_SYMBOL(altera_ci_tuner_reset);