altera-ci.c 20 KB

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