altera-ci.c 20 KB

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