18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Linux driver the digital TV devices equipped with B2C2 FlexcopII(b)/III
48c2ecf20Sopenharmony_ci * flexcop-pci.c - covers the PCI part including DMA transfers
58c2ecf20Sopenharmony_ci * see flexcop.c for copyright information
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#define FC_LOG_PREFIX "flexcop-pci"
98c2ecf20Sopenharmony_ci#include "flexcop-common.h"
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cistatic int enable_pid_filtering = 1;
128c2ecf20Sopenharmony_cimodule_param(enable_pid_filtering, int, 0444);
138c2ecf20Sopenharmony_ciMODULE_PARM_DESC(enable_pid_filtering,
148c2ecf20Sopenharmony_ci	"enable hardware pid filtering: supported values: 0 (fullts), 1");
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistatic int irq_chk_intv = 100;
178c2ecf20Sopenharmony_cimodule_param(irq_chk_intv, int, 0644);
188c2ecf20Sopenharmony_ciMODULE_PARM_DESC(irq_chk_intv, "set the interval for IRQ streaming watchdog.");
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#ifdef CONFIG_DVB_B2C2_FLEXCOP_DEBUG
218c2ecf20Sopenharmony_ci#define dprintk(level,args...) \
228c2ecf20Sopenharmony_ci	do { if ((debug & level)) printk(args); } while (0)
238c2ecf20Sopenharmony_ci#define DEBSTATUS ""
248c2ecf20Sopenharmony_ci#else
258c2ecf20Sopenharmony_ci#define dprintk(level,args...)
268c2ecf20Sopenharmony_ci#define DEBSTATUS " (debugging is not enabled)"
278c2ecf20Sopenharmony_ci#endif
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define deb_info(args...) dprintk(0x01, args)
308c2ecf20Sopenharmony_ci#define deb_reg(args...) dprintk(0x02, args)
318c2ecf20Sopenharmony_ci#define deb_ts(args...) dprintk(0x04, args)
328c2ecf20Sopenharmony_ci#define deb_irq(args...) dprintk(0x08, args)
338c2ecf20Sopenharmony_ci#define deb_chk(args...) dprintk(0x10, args)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic int debug;
368c2ecf20Sopenharmony_cimodule_param(debug, int, 0644);
378c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug,
388c2ecf20Sopenharmony_ci	"set debug level (1=info,2=regs,4=TS,8=irqdma,16=check (|-able))."
398c2ecf20Sopenharmony_ci	DEBSTATUS);
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define DRIVER_VERSION "0.1"
428c2ecf20Sopenharmony_ci#define DRIVER_NAME "flexcop-pci"
438c2ecf20Sopenharmony_ci#define DRIVER_AUTHOR "Patrick Boettcher <patrick.boettcher@posteo.de>"
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistruct flexcop_pci {
468c2ecf20Sopenharmony_ci	struct pci_dev *pdev;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define FC_PCI_INIT     0x01
498c2ecf20Sopenharmony_ci#define FC_PCI_DMA_INIT 0x02
508c2ecf20Sopenharmony_ci	int init_state;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	void __iomem *io_mem;
538c2ecf20Sopenharmony_ci	u32 irq;
548c2ecf20Sopenharmony_ci	/* buffersize (at least for DMA1, need to be % 188 == 0,
558c2ecf20Sopenharmony_ci	 * this logic is required */
568c2ecf20Sopenharmony_ci#define FC_DEFAULT_DMA1_BUFSIZE (1280 * 188)
578c2ecf20Sopenharmony_ci#define FC_DEFAULT_DMA2_BUFSIZE (10 * 188)
588c2ecf20Sopenharmony_ci	struct flexcop_dma dma[2];
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	int active_dma1_addr; /* 0 = addr0 of dma1; 1 = addr1 of dma1 */
618c2ecf20Sopenharmony_ci	u32 last_dma1_cur_pos;
628c2ecf20Sopenharmony_ci	/* position of the pointer last time the timer/packet irq occurred */
638c2ecf20Sopenharmony_ci	int count;
648c2ecf20Sopenharmony_ci	int count_prev;
658c2ecf20Sopenharmony_ci	int stream_problem;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	spinlock_t irq_lock;
688c2ecf20Sopenharmony_ci	unsigned long last_irq;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	struct delayed_work irq_check_work;
718c2ecf20Sopenharmony_ci	struct flexcop_device *fc_dev;
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic int lastwreg, lastwval, lastrreg, lastrval;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic flexcop_ibi_value flexcop_pci_read_ibi_reg(struct flexcop_device *fc,
778c2ecf20Sopenharmony_ci		flexcop_ibi_register r)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	struct flexcop_pci *fc_pci = fc->bus_specific;
808c2ecf20Sopenharmony_ci	flexcop_ibi_value v;
818c2ecf20Sopenharmony_ci	v.raw = readl(fc_pci->io_mem + r);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	if (lastrreg != r || lastrval != v.raw) {
848c2ecf20Sopenharmony_ci		lastrreg = r; lastrval = v.raw;
858c2ecf20Sopenharmony_ci		deb_reg("new rd: %3x: %08x\n", r, v.raw);
868c2ecf20Sopenharmony_ci	}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	return v;
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic int flexcop_pci_write_ibi_reg(struct flexcop_device *fc,
928c2ecf20Sopenharmony_ci		flexcop_ibi_register r, flexcop_ibi_value v)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	struct flexcop_pci *fc_pci = fc->bus_specific;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	if (lastwreg != r || lastwval != v.raw) {
978c2ecf20Sopenharmony_ci		lastwreg = r; lastwval = v.raw;
988c2ecf20Sopenharmony_ci		deb_reg("new wr: %3x: %08x\n", r, v.raw);
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	writel(v.raw, fc_pci->io_mem + r);
1028c2ecf20Sopenharmony_ci	return 0;
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic void flexcop_pci_irq_check_work(struct work_struct *work)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	struct flexcop_pci *fc_pci =
1088c2ecf20Sopenharmony_ci		container_of(work, struct flexcop_pci, irq_check_work.work);
1098c2ecf20Sopenharmony_ci	struct flexcop_device *fc = fc_pci->fc_dev;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	if (fc->feedcount) {
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci		if (fc_pci->count == fc_pci->count_prev) {
1148c2ecf20Sopenharmony_ci			deb_chk("no IRQ since the last check\n");
1158c2ecf20Sopenharmony_ci			if (fc_pci->stream_problem++ == 3) {
1168c2ecf20Sopenharmony_ci				struct dvb_demux_feed *feed;
1178c2ecf20Sopenharmony_ci				deb_info("flexcop-pci: stream problem, resetting pid filter\n");
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci				spin_lock_irq(&fc->demux.lock);
1208c2ecf20Sopenharmony_ci				list_for_each_entry(feed, &fc->demux.feed_list,
1218c2ecf20Sopenharmony_ci						list_head) {
1228c2ecf20Sopenharmony_ci					flexcop_pid_feed_control(fc, feed, 0);
1238c2ecf20Sopenharmony_ci				}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci				list_for_each_entry(feed, &fc->demux.feed_list,
1268c2ecf20Sopenharmony_ci						list_head) {
1278c2ecf20Sopenharmony_ci					flexcop_pid_feed_control(fc, feed, 1);
1288c2ecf20Sopenharmony_ci				}
1298c2ecf20Sopenharmony_ci				spin_unlock_irq(&fc->demux.lock);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci				fc_pci->stream_problem = 0;
1328c2ecf20Sopenharmony_ci			}
1338c2ecf20Sopenharmony_ci		} else {
1348c2ecf20Sopenharmony_ci			fc_pci->stream_problem = 0;
1358c2ecf20Sopenharmony_ci			fc_pci->count_prev = fc_pci->count;
1368c2ecf20Sopenharmony_ci		}
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	schedule_delayed_work(&fc_pci->irq_check_work,
1408c2ecf20Sopenharmony_ci			msecs_to_jiffies(irq_chk_intv < 100 ? 100 : irq_chk_intv));
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci/* When PID filtering is turned on, we use the timer IRQ, because small amounts
1448c2ecf20Sopenharmony_ci * of data need to be passed to the user space instantly as well. When PID
1458c2ecf20Sopenharmony_ci * filtering is turned off, we use the page-change-IRQ */
1468c2ecf20Sopenharmony_cistatic irqreturn_t flexcop_pci_isr(int irq, void *dev_id)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	struct flexcop_pci *fc_pci = dev_id;
1498c2ecf20Sopenharmony_ci	struct flexcop_device *fc = fc_pci->fc_dev;
1508c2ecf20Sopenharmony_ci	unsigned long flags;
1518c2ecf20Sopenharmony_ci	flexcop_ibi_value v;
1528c2ecf20Sopenharmony_ci	irqreturn_t ret = IRQ_HANDLED;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	spin_lock_irqsave(&fc_pci->irq_lock, flags);
1558c2ecf20Sopenharmony_ci	v = fc->read_ibi_reg(fc, irq_20c);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	/* errors */
1588c2ecf20Sopenharmony_ci	if (v.irq_20c.Data_receiver_error)
1598c2ecf20Sopenharmony_ci		deb_chk("data receiver error\n");
1608c2ecf20Sopenharmony_ci	if (v.irq_20c.Continuity_error_flag)
1618c2ecf20Sopenharmony_ci		deb_chk("Continuity error flag is set\n");
1628c2ecf20Sopenharmony_ci	if (v.irq_20c.LLC_SNAP_FLAG_set)
1638c2ecf20Sopenharmony_ci		deb_chk("LLC_SNAP_FLAG_set is set\n");
1648c2ecf20Sopenharmony_ci	if (v.irq_20c.Transport_Error)
1658c2ecf20Sopenharmony_ci		deb_chk("Transport error\n");
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	if ((fc_pci->count % 1000) == 0)
1688c2ecf20Sopenharmony_ci		deb_chk("%d valid irq took place so far\n", fc_pci->count);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	if (v.irq_20c.DMA1_IRQ_Status == 1) {
1718c2ecf20Sopenharmony_ci		if (fc_pci->active_dma1_addr == 0)
1728c2ecf20Sopenharmony_ci			flexcop_pass_dmx_packets(fc_pci->fc_dev,
1738c2ecf20Sopenharmony_ci					fc_pci->dma[0].cpu_addr0,
1748c2ecf20Sopenharmony_ci					fc_pci->dma[0].size / 188);
1758c2ecf20Sopenharmony_ci		else
1768c2ecf20Sopenharmony_ci			flexcop_pass_dmx_packets(fc_pci->fc_dev,
1778c2ecf20Sopenharmony_ci					fc_pci->dma[0].cpu_addr1,
1788c2ecf20Sopenharmony_ci					fc_pci->dma[0].size / 188);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci		deb_irq("page change to page: %d\n",!fc_pci->active_dma1_addr);
1818c2ecf20Sopenharmony_ci		fc_pci->active_dma1_addr = !fc_pci->active_dma1_addr;
1828c2ecf20Sopenharmony_ci		/* for the timer IRQ we only can use buffer dmx feeding, because we don't have
1838c2ecf20Sopenharmony_ci		 * complete TS packets when reading from the DMA memory */
1848c2ecf20Sopenharmony_ci	} else if (v.irq_20c.DMA1_Timer_Status == 1) {
1858c2ecf20Sopenharmony_ci		dma_addr_t cur_addr =
1868c2ecf20Sopenharmony_ci			fc->read_ibi_reg(fc,dma1_008).dma_0x8.dma_cur_addr << 2;
1878c2ecf20Sopenharmony_ci		u32 cur_pos = cur_addr - fc_pci->dma[0].dma_addr0;
1888c2ecf20Sopenharmony_ci		if (cur_pos > fc_pci->dma[0].size * 2)
1898c2ecf20Sopenharmony_ci			goto error;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci		deb_irq("%u irq: %08x cur_addr: %llx: cur_pos: %08x, last_cur_pos: %08x ",
1928c2ecf20Sopenharmony_ci				jiffies_to_usecs(jiffies - fc_pci->last_irq),
1938c2ecf20Sopenharmony_ci				v.raw, (unsigned long long)cur_addr, cur_pos,
1948c2ecf20Sopenharmony_ci				fc_pci->last_dma1_cur_pos);
1958c2ecf20Sopenharmony_ci		fc_pci->last_irq = jiffies;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci		/* buffer end was reached, restarted from the beginning
1988c2ecf20Sopenharmony_ci		 * pass the data from last_cur_pos to the buffer end to the demux
1998c2ecf20Sopenharmony_ci		 */
2008c2ecf20Sopenharmony_ci		if (cur_pos < fc_pci->last_dma1_cur_pos) {
2018c2ecf20Sopenharmony_ci			deb_irq(" end was reached: passing %d bytes ",
2028c2ecf20Sopenharmony_ci				(fc_pci->dma[0].size*2 - 1) -
2038c2ecf20Sopenharmony_ci				fc_pci->last_dma1_cur_pos);
2048c2ecf20Sopenharmony_ci			flexcop_pass_dmx_data(fc_pci->fc_dev,
2058c2ecf20Sopenharmony_ci				fc_pci->dma[0].cpu_addr0 +
2068c2ecf20Sopenharmony_ci					fc_pci->last_dma1_cur_pos,
2078c2ecf20Sopenharmony_ci				(fc_pci->dma[0].size*2) -
2088c2ecf20Sopenharmony_ci					fc_pci->last_dma1_cur_pos);
2098c2ecf20Sopenharmony_ci			fc_pci->last_dma1_cur_pos = 0;
2108c2ecf20Sopenharmony_ci		}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci		if (cur_pos > fc_pci->last_dma1_cur_pos) {
2138c2ecf20Sopenharmony_ci			deb_irq(" passing %d bytes ",
2148c2ecf20Sopenharmony_ci				cur_pos - fc_pci->last_dma1_cur_pos);
2158c2ecf20Sopenharmony_ci			flexcop_pass_dmx_data(fc_pci->fc_dev,
2168c2ecf20Sopenharmony_ci				fc_pci->dma[0].cpu_addr0 +
2178c2ecf20Sopenharmony_ci					fc_pci->last_dma1_cur_pos,
2188c2ecf20Sopenharmony_ci				cur_pos - fc_pci->last_dma1_cur_pos);
2198c2ecf20Sopenharmony_ci		}
2208c2ecf20Sopenharmony_ci		deb_irq("\n");
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci		fc_pci->last_dma1_cur_pos = cur_pos;
2238c2ecf20Sopenharmony_ci		fc_pci->count++;
2248c2ecf20Sopenharmony_ci	} else {
2258c2ecf20Sopenharmony_ci		deb_irq("isr for flexcop called, apparently without reason (%08x)\n",
2268c2ecf20Sopenharmony_ci			v.raw);
2278c2ecf20Sopenharmony_ci		ret = IRQ_NONE;
2288c2ecf20Sopenharmony_ci	}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cierror:
2318c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&fc_pci->irq_lock, flags);
2328c2ecf20Sopenharmony_ci	return ret;
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic int flexcop_pci_stream_control(struct flexcop_device *fc, int onoff)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	struct flexcop_pci *fc_pci = fc->bus_specific;
2388c2ecf20Sopenharmony_ci	if (onoff) {
2398c2ecf20Sopenharmony_ci		flexcop_dma_config(fc, &fc_pci->dma[0], FC_DMA_1);
2408c2ecf20Sopenharmony_ci		flexcop_dma_config(fc, &fc_pci->dma[1], FC_DMA_2);
2418c2ecf20Sopenharmony_ci		flexcop_dma_config_timer(fc, FC_DMA_1, 0);
2428c2ecf20Sopenharmony_ci		flexcop_dma_xfer_control(fc, FC_DMA_1,
2438c2ecf20Sopenharmony_ci				FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1, 1);
2448c2ecf20Sopenharmony_ci		deb_irq("DMA xfer enabled\n");
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci		fc_pci->last_dma1_cur_pos = 0;
2478c2ecf20Sopenharmony_ci		flexcop_dma_control_timer_irq(fc, FC_DMA_1, 1);
2488c2ecf20Sopenharmony_ci		deb_irq("IRQ enabled\n");
2498c2ecf20Sopenharmony_ci		fc_pci->count_prev = fc_pci->count;
2508c2ecf20Sopenharmony_ci	} else {
2518c2ecf20Sopenharmony_ci		flexcop_dma_control_timer_irq(fc, FC_DMA_1, 0);
2528c2ecf20Sopenharmony_ci		deb_irq("IRQ disabled\n");
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci		flexcop_dma_xfer_control(fc, FC_DMA_1,
2558c2ecf20Sopenharmony_ci			 FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1, 0);
2568c2ecf20Sopenharmony_ci		deb_irq("DMA xfer disabled\n");
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci	return 0;
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic int flexcop_pci_dma_init(struct flexcop_pci *fc_pci)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	int ret;
2648c2ecf20Sopenharmony_ci	ret = flexcop_dma_allocate(fc_pci->pdev, &fc_pci->dma[0],
2658c2ecf20Sopenharmony_ci			FC_DEFAULT_DMA1_BUFSIZE);
2668c2ecf20Sopenharmony_ci	if (ret != 0)
2678c2ecf20Sopenharmony_ci		return ret;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	ret = flexcop_dma_allocate(fc_pci->pdev, &fc_pci->dma[1],
2708c2ecf20Sopenharmony_ci			FC_DEFAULT_DMA2_BUFSIZE);
2718c2ecf20Sopenharmony_ci	if (ret != 0) {
2728c2ecf20Sopenharmony_ci		flexcop_dma_free(&fc_pci->dma[0]);
2738c2ecf20Sopenharmony_ci		return ret;
2748c2ecf20Sopenharmony_ci	}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	flexcop_sram_set_dest(fc_pci->fc_dev, FC_SRAM_DEST_MEDIA |
2778c2ecf20Sopenharmony_ci			FC_SRAM_DEST_NET, FC_SRAM_DEST_TARGET_DMA1);
2788c2ecf20Sopenharmony_ci	flexcop_sram_set_dest(fc_pci->fc_dev, FC_SRAM_DEST_CAO |
2798c2ecf20Sopenharmony_ci			FC_SRAM_DEST_CAI, FC_SRAM_DEST_TARGET_DMA2);
2808c2ecf20Sopenharmony_ci	fc_pci->init_state |= FC_PCI_DMA_INIT;
2818c2ecf20Sopenharmony_ci	return ret;
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic void flexcop_pci_dma_exit(struct flexcop_pci *fc_pci)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	if (fc_pci->init_state & FC_PCI_DMA_INIT) {
2878c2ecf20Sopenharmony_ci		flexcop_dma_free(&fc_pci->dma[0]);
2888c2ecf20Sopenharmony_ci		flexcop_dma_free(&fc_pci->dma[1]);
2898c2ecf20Sopenharmony_ci	}
2908c2ecf20Sopenharmony_ci	fc_pci->init_state &= ~FC_PCI_DMA_INIT;
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_cistatic int flexcop_pci_init(struct flexcop_pci *fc_pci)
2948c2ecf20Sopenharmony_ci{
2958c2ecf20Sopenharmony_ci	int ret;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	info("card revision %x", fc_pci->pdev->revision);
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	if ((ret = pci_enable_device(fc_pci->pdev)) != 0)
3008c2ecf20Sopenharmony_ci		return ret;
3018c2ecf20Sopenharmony_ci	pci_set_master(fc_pci->pdev);
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	if ((ret = pci_request_regions(fc_pci->pdev, DRIVER_NAME)) != 0)
3048c2ecf20Sopenharmony_ci		goto err_pci_disable_device;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	fc_pci->io_mem = pci_iomap(fc_pci->pdev, 0, 0x800);
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	if (!fc_pci->io_mem) {
3098c2ecf20Sopenharmony_ci		err("cannot map io memory\n");
3108c2ecf20Sopenharmony_ci		ret = -EIO;
3118c2ecf20Sopenharmony_ci		goto err_pci_release_regions;
3128c2ecf20Sopenharmony_ci	}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	pci_set_drvdata(fc_pci->pdev, fc_pci);
3158c2ecf20Sopenharmony_ci	spin_lock_init(&fc_pci->irq_lock);
3168c2ecf20Sopenharmony_ci	if ((ret = request_irq(fc_pci->pdev->irq, flexcop_pci_isr,
3178c2ecf20Sopenharmony_ci					IRQF_SHARED, DRIVER_NAME, fc_pci)) != 0)
3188c2ecf20Sopenharmony_ci		goto err_pci_iounmap;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	fc_pci->init_state |= FC_PCI_INIT;
3218c2ecf20Sopenharmony_ci	return ret;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_cierr_pci_iounmap:
3248c2ecf20Sopenharmony_ci	pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
3258c2ecf20Sopenharmony_cierr_pci_release_regions:
3268c2ecf20Sopenharmony_ci	pci_release_regions(fc_pci->pdev);
3278c2ecf20Sopenharmony_cierr_pci_disable_device:
3288c2ecf20Sopenharmony_ci	pci_disable_device(fc_pci->pdev);
3298c2ecf20Sopenharmony_ci	return ret;
3308c2ecf20Sopenharmony_ci}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_cistatic void flexcop_pci_exit(struct flexcop_pci *fc_pci)
3338c2ecf20Sopenharmony_ci{
3348c2ecf20Sopenharmony_ci	if (fc_pci->init_state & FC_PCI_INIT) {
3358c2ecf20Sopenharmony_ci		free_irq(fc_pci->pdev->irq, fc_pci);
3368c2ecf20Sopenharmony_ci		pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
3378c2ecf20Sopenharmony_ci		pci_release_regions(fc_pci->pdev);
3388c2ecf20Sopenharmony_ci		pci_disable_device(fc_pci->pdev);
3398c2ecf20Sopenharmony_ci	}
3408c2ecf20Sopenharmony_ci	fc_pci->init_state &= ~FC_PCI_INIT;
3418c2ecf20Sopenharmony_ci}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_cistatic int flexcop_pci_probe(struct pci_dev *pdev,
3448c2ecf20Sopenharmony_ci		const struct pci_device_id *ent)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	struct flexcop_device *fc;
3478c2ecf20Sopenharmony_ci	struct flexcop_pci *fc_pci;
3488c2ecf20Sopenharmony_ci	int ret = -ENOMEM;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	if ((fc = flexcop_device_kmalloc(sizeof(struct flexcop_pci))) == NULL) {
3518c2ecf20Sopenharmony_ci		err("out of memory\n");
3528c2ecf20Sopenharmony_ci		return -ENOMEM;
3538c2ecf20Sopenharmony_ci	}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	/* general flexcop init */
3568c2ecf20Sopenharmony_ci	fc_pci = fc->bus_specific;
3578c2ecf20Sopenharmony_ci	fc_pci->fc_dev = fc;
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	fc->read_ibi_reg = flexcop_pci_read_ibi_reg;
3608c2ecf20Sopenharmony_ci	fc->write_ibi_reg = flexcop_pci_write_ibi_reg;
3618c2ecf20Sopenharmony_ci	fc->i2c_request = flexcop_i2c_request;
3628c2ecf20Sopenharmony_ci	fc->get_mac_addr = flexcop_eeprom_check_mac_addr;
3638c2ecf20Sopenharmony_ci	fc->stream_control = flexcop_pci_stream_control;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	if (enable_pid_filtering)
3668c2ecf20Sopenharmony_ci		info("will use the HW PID filter.");
3678c2ecf20Sopenharmony_ci	else
3688c2ecf20Sopenharmony_ci		info("will pass the complete TS to the demuxer.");
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	fc->pid_filtering = enable_pid_filtering;
3718c2ecf20Sopenharmony_ci	fc->bus_type = FC_PCI;
3728c2ecf20Sopenharmony_ci	fc->dev = &pdev->dev;
3738c2ecf20Sopenharmony_ci	fc->owner = THIS_MODULE;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	/* bus specific part */
3768c2ecf20Sopenharmony_ci	fc_pci->pdev = pdev;
3778c2ecf20Sopenharmony_ci	if ((ret = flexcop_pci_init(fc_pci)) != 0)
3788c2ecf20Sopenharmony_ci		goto err_kfree;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	/* init flexcop */
3818c2ecf20Sopenharmony_ci	if ((ret = flexcop_device_initialize(fc)) != 0)
3828c2ecf20Sopenharmony_ci		goto err_pci_exit;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	/* init dma */
3858c2ecf20Sopenharmony_ci	if ((ret = flexcop_pci_dma_init(fc_pci)) != 0)
3868c2ecf20Sopenharmony_ci		goto err_fc_exit;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&fc_pci->irq_check_work, flexcop_pci_irq_check_work);
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	if (irq_chk_intv > 0)
3918c2ecf20Sopenharmony_ci		schedule_delayed_work(&fc_pci->irq_check_work,
3928c2ecf20Sopenharmony_ci				msecs_to_jiffies(irq_chk_intv < 100 ?
3938c2ecf20Sopenharmony_ci					100 :
3948c2ecf20Sopenharmony_ci					irq_chk_intv));
3958c2ecf20Sopenharmony_ci	return ret;
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_cierr_fc_exit:
3988c2ecf20Sopenharmony_ci	flexcop_device_exit(fc);
3998c2ecf20Sopenharmony_cierr_pci_exit:
4008c2ecf20Sopenharmony_ci	flexcop_pci_exit(fc_pci);
4018c2ecf20Sopenharmony_cierr_kfree:
4028c2ecf20Sopenharmony_ci	flexcop_device_kfree(fc);
4038c2ecf20Sopenharmony_ci	return ret;
4048c2ecf20Sopenharmony_ci}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci/* in theory every _exit function should be called exactly two times,
4078c2ecf20Sopenharmony_ci * here and in the bail-out-part of the _init-function
4088c2ecf20Sopenharmony_ci */
4098c2ecf20Sopenharmony_cistatic void flexcop_pci_remove(struct pci_dev *pdev)
4108c2ecf20Sopenharmony_ci{
4118c2ecf20Sopenharmony_ci	struct flexcop_pci *fc_pci = pci_get_drvdata(pdev);
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	if (irq_chk_intv > 0)
4148c2ecf20Sopenharmony_ci		cancel_delayed_work(&fc_pci->irq_check_work);
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	flexcop_pci_dma_exit(fc_pci);
4178c2ecf20Sopenharmony_ci	flexcop_device_exit(fc_pci->fc_dev);
4188c2ecf20Sopenharmony_ci	flexcop_pci_exit(fc_pci);
4198c2ecf20Sopenharmony_ci	flexcop_device_kfree(fc_pci->fc_dev);
4208c2ecf20Sopenharmony_ci}
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_cistatic const struct pci_device_id flexcop_pci_tbl[] = {
4238c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x13d0, 0x2103) },
4248c2ecf20Sopenharmony_ci	{ },
4258c2ecf20Sopenharmony_ci};
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, flexcop_pci_tbl);
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_cistatic struct pci_driver flexcop_pci_driver = {
4308c2ecf20Sopenharmony_ci	.name     = "b2c2_flexcop_pci",
4318c2ecf20Sopenharmony_ci	.id_table = flexcop_pci_tbl,
4328c2ecf20Sopenharmony_ci	.probe    = flexcop_pci_probe,
4338c2ecf20Sopenharmony_ci	.remove   = flexcop_pci_remove,
4348c2ecf20Sopenharmony_ci};
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_cimodule_pci_driver(flexcop_pci_driver);
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ciMODULE_AUTHOR(DRIVER_AUTHOR);
4398c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_NAME);
4408c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
441