altera-ci.c 20 KB

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