altera-ci.c 20 KB

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