4 * CI driver in conjunction with NetUp Dual DVB-T/C RF CI card
6 * Copyright (C) 2010,2011 NetUP Inc.
7 * Copyright (C) 2010,2011 Igor M. Liplianin <liplianin@netup.ru>
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.
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
18 * GNU General Public License for more details.
22 * currently cx23885 GPIO's used.
25 * GPIO-2 ~reset chips out
26 * GPIO-3 to GPIO-10 data/addr for CA in/out
37 * Bit definitions for MC417_RWD and MC417_OEN registers
42 * bit 15 bit 14 bit 13 bit 12 bit 11 bit 10 bit 9 bit 8
43 * +-------+-------+-------+-------+-------+-------+-------+-------+
44 * | TDI | TDO | TCK | RDY# | #RD | #WR | AD_RG | #CS |
45 * +-------+-------+-------+-------+-------+-------+-------+-------+
46 * bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
47 * +-------+-------+-------+-------+-------+-------+-------+-------+
48 * | DATA7| DATA6| DATA5| DATA4| DATA3| DATA2| DATA1| DATA0|
49 * +-------+-------+-------+-------+-------+-------+-------+-------+
51 #include <dvb_demux.h>
52 #include <dvb_frontend.h>
53 #include "altera-ci.h"
54 #include "dvb_ca_en50221.h"
57 #define NETUP_CI_INT_CTRL 0x00
58 #define NETUP_CI_BUSCTRL2 0x01
59 #define NETUP_CI_ADDR0 0x04
60 #define NETUP_CI_ADDR1 0x05
61 #define NETUP_CI_DATA 0x06
62 #define NETUP_CI_BUSCTRL 0x07
63 #define NETUP_CI_PID_ADDR0 0x08
64 #define NETUP_CI_PID_ADDR1 0x09
65 #define NETUP_CI_PID_DATA 0x0a
66 #define NETUP_CI_TSA_DIV 0x0c
67 #define NETUP_CI_TSB_DIV 0x0d
68 #define NETUP_CI_REVISION 0x0f
71 #define NETUP_CI_FLG_CTL 1
72 #define NETUP_CI_FLG_RD 1
73 #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");
79 static unsigned int pid_dbg;
80 module_param(pid_dbg, int, 0644);
81 MODULE_PARM_DESC(pid_dbg, "Enable PID filtering debugging");
83 MODULE_DESCRIPTION("altera FPGA CI module");
84 MODULE_AUTHOR("Igor M. Liplianin <liplianin@netup.ru>");
85 MODULE_LICENSE("GPL");
87 #define ci_dbg_print(args...) \
90 printk(KERN_DEBUG args); \
93 #define pid_dbg_print(args...) \
96 printk(KERN_DEBUG args); \
99 struct altera_ci_state;
100 struct netup_hw_pid_filter;
102 struct fpga_internal {
104 struct mutex fpga_mutex;/* two CI's on the same fpga */
105 struct netup_hw_pid_filter *pid_filt[2];
106 struct altera_ci_state *state[2];
107 struct work_struct work;
108 int (*fpga_rw) (void *dev, int flag, int data, int rw);
114 /* stores all private variables for communication with CI */
115 struct altera_ci_state {
116 struct fpga_internal *internal;
117 struct dvb_ca_en50221 ca;
122 /* stores all private variables for hardware pid filtering */
123 struct netup_hw_pid_filter {
124 struct fpga_internal *internal;
125 struct dvb_demux *demux;
126 /* save old functions */
127 int (*start_feed)(struct dvb_demux_feed *feed);
128 int (*stop_feed)(struct dvb_demux_feed *feed);
134 /* internal params node */
136 /* pointer for internal params, one for each pair of CI's */
137 struct fpga_internal *internal;
138 struct fpga_inode *next_inode;
141 /* first internal params */
142 static struct fpga_inode *fpga_first_inode;
144 /* find chip by dev */
145 static struct fpga_inode *find_inode(void *dev)
147 struct fpga_inode *temp_chip = fpga_first_inode;
149 if (temp_chip == NULL)
153 Search for the last fpga CI chip or
155 while ((temp_chip != NULL) &&
156 (temp_chip->internal->dev != dev))
157 temp_chip = temp_chip->next_inode;
162 static struct fpga_internal *check_filter(struct fpga_internal *temp_int,
163 void *demux_dev, int filt_nr)
165 if (temp_int == NULL)
168 if ((temp_int->pid_filt[filt_nr]) == NULL)
171 if (temp_int->pid_filt[filt_nr]->demux == demux_dev)
177 /* find chip by demux */
178 static struct fpga_inode *find_dinode(void *demux_dev)
180 struct fpga_inode *temp_chip = fpga_first_inode;
181 struct fpga_internal *temp_int;
184 * Search of the last fpga CI chip or
187 while (temp_chip != NULL) {
188 if (temp_chip->internal != NULL) {
189 temp_int = temp_chip->internal;
190 if (check_filter(temp_int, demux_dev, 0))
192 if (check_filter(temp_int, demux_dev, 1))
196 temp_chip = temp_chip->next_inode;
202 /* deallocating chip */
203 static void remove_inode(struct fpga_internal *internal)
205 struct fpga_inode *prev_node = fpga_first_inode;
206 struct fpga_inode *del_node = find_inode(internal->dev);
208 if (del_node != NULL) {
209 if (del_node == fpga_first_inode) {
210 fpga_first_inode = del_node->next_inode;
212 while (prev_node->next_inode != del_node)
213 prev_node = prev_node->next_inode;
215 if (del_node->next_inode == NULL)
216 prev_node->next_inode = NULL;
218 prev_node->next_inode =
219 prev_node->next_inode->next_inode;
226 /* allocating new chip */
227 static struct fpga_inode *append_internal(struct fpga_internal *internal)
229 struct fpga_inode *new_node = fpga_first_inode;
231 if (new_node == NULL) {
232 new_node = kmalloc(sizeof(struct fpga_inode), GFP_KERNEL);
233 fpga_first_inode = new_node;
235 while (new_node->next_inode != NULL)
236 new_node = new_node->next_inode;
238 new_node->next_inode =
239 kmalloc(sizeof(struct fpga_inode), GFP_KERNEL);
240 if (new_node->next_inode != NULL)
241 new_node = new_node->next_inode;
246 if (new_node != NULL) {
247 new_node->internal = internal;
248 new_node->next_inode = NULL;
254 static int netup_fpga_op_rw(struct fpga_internal *inter, int addr,
257 inter->fpga_rw(inter->dev, NETUP_CI_FLG_AD, addr, 0);
258 return inter->fpga_rw(inter->dev, 0, val, read);
261 /* flag - mem/io, read - read/write */
262 static int altera_ci_op_cam(struct dvb_ca_en50221 *en50221, int slot,
263 u8 flag, u8 read, int addr, u8 val)
266 struct altera_ci_state *state = en50221->data;
267 struct fpga_internal *inter = state->internal;
275 mutex_lock(&inter->fpga_mutex);
277 netup_fpga_op_rw(inter, NETUP_CI_ADDR0, ((addr << 1) & 0xfe), 0);
278 netup_fpga_op_rw(inter, NETUP_CI_ADDR1, ((addr >> 7) & 0x7f), 0);
279 store = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, 0, NETUP_CI_FLG_RD);
282 store |= ((state->nr << 7) | (flag << 6));
284 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, store, 0);
285 mem = netup_fpga_op_rw(inter, NETUP_CI_DATA, val, read);
287 mutex_unlock(&inter->fpga_mutex);
289 ci_dbg_print("%s: %s: addr=[0x%02x], %s=%x\n", __func__,
290 (read) ? "read" : "write", addr,
291 (flag == NETUP_CI_FLG_CTL) ? "ctl" : "mem",
297 static int altera_ci_read_attribute_mem(struct dvb_ca_en50221 *en50221,
300 return altera_ci_op_cam(en50221, slot, 0, NETUP_CI_FLG_RD, addr, 0);
303 static int altera_ci_write_attribute_mem(struct dvb_ca_en50221 *en50221,
304 int slot, int addr, u8 data)
306 return altera_ci_op_cam(en50221, slot, 0, 0, addr, data);
309 static int altera_ci_read_cam_ctl(struct dvb_ca_en50221 *en50221,
312 return altera_ci_op_cam(en50221, slot, NETUP_CI_FLG_CTL,
313 NETUP_CI_FLG_RD, addr, 0);
316 static int altera_ci_write_cam_ctl(struct dvb_ca_en50221 *en50221, int slot,
319 return altera_ci_op_cam(en50221, slot, NETUP_CI_FLG_CTL, 0, addr, data);
322 static int altera_ci_slot_reset(struct dvb_ca_en50221 *en50221, int slot)
324 struct altera_ci_state *state = en50221->data;
325 struct fpga_internal *inter = state->internal;
326 /* reasonable timeout for CI reset is 10 seconds */
327 unsigned long t_out = jiffies + msecs_to_jiffies(9999);
330 ci_dbg_print("%s\n", __func__);
335 mutex_lock(&inter->fpga_mutex);
337 ret = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, 0, NETUP_CI_FLG_RD);
338 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL,
339 (ret & 0xcf) | (1 << (5 - state->nr)), 0);
341 mutex_unlock(&inter->fpga_mutex);
346 mutex_lock(&inter->fpga_mutex);
348 ret = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL,
350 mutex_unlock(&inter->fpga_mutex);
352 if ((ret & (1 << (5 - state->nr))) == 0)
354 if (time_after(jiffies, t_out))
359 ci_dbg_print("%s: %d msecs\n", __func__,
360 jiffies_to_msecs(jiffies + msecs_to_jiffies(9999) - t_out));
365 static int altera_ci_slot_shutdown(struct dvb_ca_en50221 *en50221, int slot)
367 /* not implemented */
371 static int altera_ci_slot_ts_ctl(struct dvb_ca_en50221 *en50221, int slot)
373 struct altera_ci_state *state = en50221->data;
374 struct fpga_internal *inter = state->internal;
377 ci_dbg_print("%s\n", __func__);
382 mutex_lock(&inter->fpga_mutex);
384 ret = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, 0, NETUP_CI_FLG_RD);
385 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL,
386 (ret & 0x0f) | (1 << (3 - state->nr)), 0);
388 mutex_unlock(&inter->fpga_mutex);
394 static void netup_read_ci_status(struct work_struct *work)
396 struct fpga_internal *inter =
397 container_of(work, struct fpga_internal, work);
400 ci_dbg_print("%s\n", __func__);
402 mutex_lock(&inter->fpga_mutex);
404 ret = netup_fpga_op_rw(inter, NETUP_CI_INT_CTRL, 0, NETUP_CI_FLG_RD);
405 ret = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, 0, NETUP_CI_FLG_RD);
407 mutex_unlock(&inter->fpga_mutex);
409 if (inter->state[1] != NULL) {
410 inter->state[1]->status =
412 DVB_CA_EN50221_POLL_CAM_PRESENT |
413 DVB_CA_EN50221_POLL_CAM_READY : 0);
414 ci_dbg_print("%s: setting CI[1] status = 0x%x\n",
415 __func__, inter->state[1]->status);
418 if (inter->state[0] != NULL) {
419 inter->state[0]->status =
421 DVB_CA_EN50221_POLL_CAM_PRESENT |
422 DVB_CA_EN50221_POLL_CAM_READY : 0);
423 ci_dbg_print("%s: setting CI[0] status = 0x%x\n",
424 __func__, inter->state[0]->status);
429 int altera_ci_irq(void *dev)
431 struct fpga_inode *temp_int = NULL;
432 struct fpga_internal *inter = NULL;
434 ci_dbg_print("%s\n", __func__);
437 temp_int = find_inode(dev);
438 if (temp_int != NULL) {
439 inter = temp_int->internal;
440 schedule_work(&inter->work);
446 EXPORT_SYMBOL(altera_ci_irq);
448 static int altera_poll_ci_slot_status(struct dvb_ca_en50221 *en50221,
451 struct altera_ci_state *state = en50221->data;
456 return state->status;
459 static void altera_hw_filt_release(void *main_dev, int filt_nr)
461 struct fpga_inode *temp_int = find_inode(main_dev);
462 struct netup_hw_pid_filter *pid_filt = NULL;
464 ci_dbg_print("%s\n", __func__);
466 if (temp_int != NULL) {
467 pid_filt = temp_int->internal->pid_filt[filt_nr - 1];
468 /* stored old feed controls */
469 pid_filt->demux->start_feed = pid_filt->start_feed;
470 pid_filt->demux->stop_feed = pid_filt->stop_feed;
472 if (((--(temp_int->internal->filts_used)) <= 0) &&
473 ((temp_int->internal->cis_used) <= 0)) {
475 ci_dbg_print("%s: Actually removing\n", __func__);
477 remove_inode(temp_int->internal);
478 kfree(pid_filt->internal);
486 EXPORT_SYMBOL(altera_hw_filt_release);
488 void altera_ci_release(void *dev, int ci_nr)
490 struct fpga_inode *temp_int = find_inode(dev);
491 struct altera_ci_state *state = NULL;
493 ci_dbg_print("%s\n", __func__);
495 if (temp_int != NULL) {
496 state = temp_int->internal->state[ci_nr - 1];
497 altera_hw_filt_release(dev, ci_nr);
500 if (((temp_int->internal->filts_used) <= 0) &&
501 ((--(temp_int->internal->cis_used)) <= 0)) {
503 ci_dbg_print("%s: Actually removing\n", __func__);
505 remove_inode(temp_int->internal);
506 kfree(state->internal);
510 if (state->ca.data != NULL)
511 dvb_ca_en50221_release(&state->ca);
518 EXPORT_SYMBOL(altera_ci_release);
520 static void altera_pid_control(struct netup_hw_pid_filter *pid_filt,
523 struct fpga_internal *inter = pid_filt->internal;
526 /* pid 0-0x1f always enabled, don't touch them */
527 if ((pid == 0x2000) || (pid < 0x20))
530 mutex_lock(&inter->fpga_mutex);
532 netup_fpga_op_rw(inter, NETUP_CI_PID_ADDR0, (pid >> 3) & 0xff, 0);
533 netup_fpga_op_rw(inter, NETUP_CI_PID_ADDR1,
534 ((pid >> 11) & 0x03) | (pid_filt->nr << 2), 0);
536 store = netup_fpga_op_rw(inter, NETUP_CI_PID_DATA, 0, NETUP_CI_FLG_RD);
538 if (onoff)/* 0 - on, 1 - off */
539 store |= (1 << (pid & 7));
541 store &= ~(1 << (pid & 7));
543 netup_fpga_op_rw(inter, NETUP_CI_PID_DATA, store, 0);
545 mutex_unlock(&inter->fpga_mutex);
547 pid_dbg_print("%s: (%d) set pid: %5d 0x%04x '%s'\n", __func__,
548 pid_filt->nr, pid, pid, onoff ? "off" : "on");
551 static void altera_toggle_fullts_streaming(struct netup_hw_pid_filter *pid_filt,
552 int filt_nr, int onoff)
554 struct fpga_internal *inter = pid_filt->internal;
558 pid_dbg_print("%s: pid_filt->nr[%d] now %s\n", __func__, pid_filt->nr,
559 onoff ? "off" : "on");
561 if (onoff)/* 0 - on, 1 - off */
562 store = 0xff;/* ignore pid */
564 store = 0;/* enable pid */
566 mutex_lock(&inter->fpga_mutex);
568 for (i = 0; i < 1024; i++) {
569 netup_fpga_op_rw(inter, NETUP_CI_PID_ADDR0, i & 0xff, 0);
571 netup_fpga_op_rw(inter, NETUP_CI_PID_ADDR1,
572 ((i >> 8) & 0x03) | (pid_filt->nr << 2), 0);
573 /* pid 0-0x1f always enabled */
574 netup_fpga_op_rw(inter, NETUP_CI_PID_DATA,
575 (i > 3 ? store : 0), 0);
578 mutex_unlock(&inter->fpga_mutex);
581 static int altera_pid_feed_control(void *demux_dev, int filt_nr,
582 struct dvb_demux_feed *feed, int onoff)
584 struct fpga_inode *temp_int = find_dinode(demux_dev);
585 struct fpga_internal *inter = temp_int->internal;
586 struct netup_hw_pid_filter *pid_filt = inter->pid_filt[filt_nr - 1];
588 altera_pid_control(pid_filt, feed->pid, onoff ? 0 : 1);
589 /* call old feed proc's */
591 pid_filt->start_feed(feed);
593 pid_filt->stop_feed(feed);
595 if (feed->pid == 0x2000)
596 altera_toggle_fullts_streaming(pid_filt, filt_nr,
601 EXPORT_SYMBOL(altera_pid_feed_control);
603 static int altera_ci_start_feed(struct dvb_demux_feed *feed, int num)
605 altera_pid_feed_control(feed->demux, num, feed, 1);
610 static int altera_ci_stop_feed(struct dvb_demux_feed *feed, int num)
612 altera_pid_feed_control(feed->demux, num, feed, 0);
617 static int altera_ci_start_feed_1(struct dvb_demux_feed *feed)
619 return altera_ci_start_feed(feed, 1);
622 static int altera_ci_stop_feed_1(struct dvb_demux_feed *feed)
624 return altera_ci_stop_feed(feed, 1);
627 static int altera_ci_start_feed_2(struct dvb_demux_feed *feed)
629 return altera_ci_start_feed(feed, 2);
632 static int altera_ci_stop_feed_2(struct dvb_demux_feed *feed)
634 return altera_ci_stop_feed(feed, 2);
637 static int altera_hw_filt_init(struct altera_ci_config *config, int hw_filt_nr)
639 struct netup_hw_pid_filter *pid_filt = NULL;
640 struct fpga_inode *temp_int = find_inode(config->dev);
641 struct fpga_internal *inter = NULL;
644 pid_filt = kzalloc(sizeof(struct netup_hw_pid_filter), GFP_KERNEL);
646 ci_dbg_print("%s\n", __func__);
653 if (temp_int != NULL) {
654 inter = temp_int->internal;
655 (inter->filts_used)++;
656 ci_dbg_print("%s: Find Internal Structure!\n", __func__);
658 inter = kzalloc(sizeof(struct fpga_internal), GFP_KERNEL);
664 temp_int = append_internal(inter);
665 inter->filts_used = 1;
666 inter->dev = config->dev;
667 inter->fpga_rw = config->fpga_rw;
668 mutex_init(&inter->fpga_mutex);
670 ci_dbg_print("%s: Create New Internal Structure!\n", __func__);
673 ci_dbg_print("%s: setting hw pid filter = %p for ci = %d\n", __func__,
674 pid_filt, hw_filt_nr - 1);
675 inter->pid_filt[hw_filt_nr - 1] = pid_filt;
676 pid_filt->demux = config->demux;
677 pid_filt->internal = inter;
678 pid_filt->nr = hw_filt_nr - 1;
679 /* store old feed controls */
680 pid_filt->start_feed = config->demux->start_feed;
681 pid_filt->stop_feed = config->demux->stop_feed;
682 /* replace with new feed controls */
683 if (hw_filt_nr == 1) {
684 pid_filt->demux->start_feed = altera_ci_start_feed_1;
685 pid_filt->demux->stop_feed = altera_ci_stop_feed_1;
686 } else if (hw_filt_nr == 2) {
687 pid_filt->demux->start_feed = altera_ci_start_feed_2;
688 pid_filt->demux->stop_feed = altera_ci_stop_feed_2;
691 altera_toggle_fullts_streaming(pid_filt, 0, 1);
695 ci_dbg_print("%s: Can't init hardware filter: Error %d\n",
702 EXPORT_SYMBOL(altera_hw_filt_init);
704 int altera_ci_init(struct altera_ci_config *config, int ci_nr)
706 struct altera_ci_state *state;
707 struct fpga_inode *temp_int = find_inode(config->dev);
708 struct fpga_internal *inter = NULL;
712 state = kzalloc(sizeof(struct altera_ci_state), GFP_KERNEL);
714 ci_dbg_print("%s\n", __func__);
721 if (temp_int != NULL) {
722 inter = temp_int->internal;
724 inter->fpga_rw = config->fpga_rw;
725 ci_dbg_print("%s: Find Internal Structure!\n", __func__);
727 inter = kzalloc(sizeof(struct fpga_internal), GFP_KERNEL);
733 temp_int = append_internal(inter);
735 inter->dev = config->dev;
736 inter->fpga_rw = config->fpga_rw;
737 mutex_init(&inter->fpga_mutex);
739 ci_dbg_print("%s: Create New Internal Structure!\n", __func__);
742 ci_dbg_print("%s: setting state = %p for ci = %d\n", __func__,
744 state->internal = inter;
745 state->nr = ci_nr - 1;
747 state->ca.owner = THIS_MODULE;
748 state->ca.read_attribute_mem = altera_ci_read_attribute_mem;
749 state->ca.write_attribute_mem = altera_ci_write_attribute_mem;
750 state->ca.read_cam_control = altera_ci_read_cam_ctl;
751 state->ca.write_cam_control = altera_ci_write_cam_ctl;
752 state->ca.slot_reset = altera_ci_slot_reset;
753 state->ca.slot_shutdown = altera_ci_slot_shutdown;
754 state->ca.slot_ts_enable = altera_ci_slot_ts_ctl;
755 state->ca.poll_slot_status = altera_poll_ci_slot_status;
756 state->ca.data = state;
758 ret = dvb_ca_en50221_init(config->adapter,
765 inter->state[ci_nr - 1] = state;
767 altera_hw_filt_init(config, ci_nr);
769 if (inter->strt_wrk) {
770 INIT_WORK(&inter->work, netup_read_ci_status);
774 ci_dbg_print("%s: CI initialized!\n", __func__);
776 mutex_lock(&inter->fpga_mutex);
779 netup_fpga_op_rw(inter, NETUP_CI_TSA_DIV, 0x0, 0);
780 netup_fpga_op_rw(inter, NETUP_CI_TSB_DIV, 0x0, 0);
783 store = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, 0, NETUP_CI_FLG_RD);
785 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, store, 0);
787 ret = netup_fpga_op_rw(inter, NETUP_CI_REVISION, 0, NETUP_CI_FLG_RD);
789 netup_fpga_op_rw(inter, NETUP_CI_INT_CTRL, 0x44, 0);
791 mutex_unlock(&inter->fpga_mutex);
793 ci_dbg_print("%s: NetUP CI Revision = 0x%x\n", __func__, ret);
795 schedule_work(&inter->work);
799 ci_dbg_print("%s: Cannot initialize CI: Error %d.\n", __func__, ret);
805 EXPORT_SYMBOL(altera_ci_init);
807 int altera_ci_tuner_reset(void *dev, int ci_nr)
809 struct fpga_inode *temp_int = find_inode(dev);
810 struct fpga_internal *inter = NULL;
813 ci_dbg_print("%s\n", __func__);
815 if (temp_int == NULL)
818 if (temp_int->internal == NULL)
821 inter = temp_int->internal;
823 mutex_lock(&inter->fpga_mutex);
825 store = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, 0, NETUP_CI_FLG_RD);
826 store &= ~(4 << (2 - ci_nr));
827 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, store, 0);
829 store |= (4 << (2 - ci_nr));
830 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, store, 0);
832 mutex_unlock(&inter->fpga_mutex);
836 EXPORT_SYMBOL(altera_ci_tuner_reset);