Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Linux driver the digital TV devices equipped with B2C2 FlexcopII(b)/III
0004  * flexcop-pci.c - covers the PCI part including DMA transfers
0005  * see flexcop.c for copyright information
0006  */
0007 
0008 #define FC_LOG_PREFIX "flexcop-pci"
0009 #include "flexcop-common.h"
0010 
0011 static int enable_pid_filtering = 1;
0012 module_param(enable_pid_filtering, int, 0444);
0013 MODULE_PARM_DESC(enable_pid_filtering,
0014     "enable hardware pid filtering: supported values: 0 (fullts), 1");
0015 
0016 static int irq_chk_intv = 100;
0017 module_param(irq_chk_intv, int, 0644);
0018 MODULE_PARM_DESC(irq_chk_intv, "set the interval for IRQ streaming watchdog.");
0019 
0020 #ifdef CONFIG_DVB_B2C2_FLEXCOP_DEBUG
0021 #define dprintk(level, args...) \
0022     do { if ((debug & (level))) printk(args); } while (0)
0023 #define DEBSTATUS ""
0024 #else
0025 #define dprintk(level, args...) no_printk(args)
0026 #define DEBSTATUS " (debugging is not enabled)"
0027 #endif
0028 
0029 #define deb_info(args...) dprintk(0x01, args)
0030 #define deb_reg(args...) dprintk(0x02, args)
0031 #define deb_ts(args...) dprintk(0x04, args)
0032 #define deb_irq(args...) dprintk(0x08, args)
0033 #define deb_chk(args...) dprintk(0x10, args)
0034 
0035 static int debug;
0036 module_param(debug, int, 0644);
0037 MODULE_PARM_DESC(debug,
0038     "set debug level (1=info,2=regs,4=TS,8=irqdma,16=check (|-able))."
0039     DEBSTATUS);
0040 
0041 #define DRIVER_VERSION "0.1"
0042 #define DRIVER_NAME "flexcop-pci"
0043 #define DRIVER_AUTHOR "Patrick Boettcher <patrick.boettcher@posteo.de>"
0044 
0045 struct flexcop_pci {
0046     struct pci_dev *pdev;
0047 
0048 #define FC_PCI_INIT     0x01
0049 #define FC_PCI_DMA_INIT 0x02
0050     int init_state;
0051 
0052     void __iomem *io_mem;
0053     u32 irq;
0054     /* buffersize (at least for DMA1, need to be % 188 == 0,
0055      * this logic is required */
0056 #define FC_DEFAULT_DMA1_BUFSIZE (1280 * 188)
0057 #define FC_DEFAULT_DMA2_BUFSIZE (10 * 188)
0058     struct flexcop_dma dma[2];
0059 
0060     int active_dma1_addr; /* 0 = addr0 of dma1; 1 = addr1 of dma1 */
0061     u32 last_dma1_cur_pos;
0062     /* position of the pointer last time the timer/packet irq occurred */
0063     int count;
0064     int count_prev;
0065     int stream_problem;
0066 
0067     spinlock_t irq_lock;
0068     unsigned long last_irq;
0069 
0070     struct delayed_work irq_check_work;
0071     struct flexcop_device *fc_dev;
0072 };
0073 
0074 static int lastwreg, lastwval, lastrreg, lastrval;
0075 
0076 static flexcop_ibi_value flexcop_pci_read_ibi_reg(struct flexcop_device *fc,
0077         flexcop_ibi_register r)
0078 {
0079     struct flexcop_pci *fc_pci = fc->bus_specific;
0080     flexcop_ibi_value v;
0081     v.raw = readl(fc_pci->io_mem + r);
0082 
0083     if (lastrreg != r || lastrval != v.raw) {
0084         lastrreg = r; lastrval = v.raw;
0085         deb_reg("new rd: %3x: %08x\n", r, v.raw);
0086     }
0087 
0088     return v;
0089 }
0090 
0091 static int flexcop_pci_write_ibi_reg(struct flexcop_device *fc,
0092         flexcop_ibi_register r, flexcop_ibi_value v)
0093 {
0094     struct flexcop_pci *fc_pci = fc->bus_specific;
0095 
0096     if (lastwreg != r || lastwval != v.raw) {
0097         lastwreg = r; lastwval = v.raw;
0098         deb_reg("new wr: %3x: %08x\n", r, v.raw);
0099     }
0100 
0101     writel(v.raw, fc_pci->io_mem + r);
0102     return 0;
0103 }
0104 
0105 static void flexcop_pci_irq_check_work(struct work_struct *work)
0106 {
0107     struct flexcop_pci *fc_pci =
0108         container_of(work, struct flexcop_pci, irq_check_work.work);
0109     struct flexcop_device *fc = fc_pci->fc_dev;
0110 
0111     if (fc->feedcount) {
0112 
0113         if (fc_pci->count == fc_pci->count_prev) {
0114             deb_chk("no IRQ since the last check\n");
0115             if (fc_pci->stream_problem++ == 3) {
0116                 struct dvb_demux_feed *feed;
0117                 deb_info("flexcop-pci: stream problem, resetting pid filter\n");
0118 
0119                 spin_lock_irq(&fc->demux.lock);
0120                 list_for_each_entry(feed, &fc->demux.feed_list,
0121                         list_head) {
0122                     flexcop_pid_feed_control(fc, feed, 0);
0123                 }
0124 
0125                 list_for_each_entry(feed, &fc->demux.feed_list,
0126                         list_head) {
0127                     flexcop_pid_feed_control(fc, feed, 1);
0128                 }
0129                 spin_unlock_irq(&fc->demux.lock);
0130 
0131                 fc_pci->stream_problem = 0;
0132             }
0133         } else {
0134             fc_pci->stream_problem = 0;
0135             fc_pci->count_prev = fc_pci->count;
0136         }
0137     }
0138 
0139     schedule_delayed_work(&fc_pci->irq_check_work,
0140             msecs_to_jiffies(irq_chk_intv < 100 ? 100 : irq_chk_intv));
0141 }
0142 
0143 /* When PID filtering is turned on, we use the timer IRQ, because small amounts
0144  * of data need to be passed to the user space instantly as well. When PID
0145  * filtering is turned off, we use the page-change-IRQ */
0146 static irqreturn_t flexcop_pci_isr(int irq, void *dev_id)
0147 {
0148     struct flexcop_pci *fc_pci = dev_id;
0149     struct flexcop_device *fc = fc_pci->fc_dev;
0150     unsigned long flags;
0151     flexcop_ibi_value v;
0152     irqreturn_t ret = IRQ_HANDLED;
0153 
0154     spin_lock_irqsave(&fc_pci->irq_lock, flags);
0155     v = fc->read_ibi_reg(fc, irq_20c);
0156 
0157     /* errors */
0158     if (v.irq_20c.Data_receiver_error)
0159         deb_chk("data receiver error\n");
0160     if (v.irq_20c.Continuity_error_flag)
0161         deb_chk("Continuity error flag is set\n");
0162     if (v.irq_20c.LLC_SNAP_FLAG_set)
0163         deb_chk("LLC_SNAP_FLAG_set is set\n");
0164     if (v.irq_20c.Transport_Error)
0165         deb_chk("Transport error\n");
0166 
0167     if ((fc_pci->count % 1000) == 0)
0168         deb_chk("%d valid irq took place so far\n", fc_pci->count);
0169 
0170     if (v.irq_20c.DMA1_IRQ_Status == 1) {
0171         if (fc_pci->active_dma1_addr == 0)
0172             flexcop_pass_dmx_packets(fc_pci->fc_dev,
0173                     fc_pci->dma[0].cpu_addr0,
0174                     fc_pci->dma[0].size / 188);
0175         else
0176             flexcop_pass_dmx_packets(fc_pci->fc_dev,
0177                     fc_pci->dma[0].cpu_addr1,
0178                     fc_pci->dma[0].size / 188);
0179 
0180         deb_irq("page change to page: %d\n",!fc_pci->active_dma1_addr);
0181         fc_pci->active_dma1_addr = !fc_pci->active_dma1_addr;
0182         /* for the timer IRQ we only can use buffer dmx feeding, because we don't have
0183          * complete TS packets when reading from the DMA memory */
0184     } else if (v.irq_20c.DMA1_Timer_Status == 1) {
0185         dma_addr_t cur_addr =
0186             fc->read_ibi_reg(fc,dma1_008).dma_0x8.dma_cur_addr << 2;
0187         u32 cur_pos = cur_addr - fc_pci->dma[0].dma_addr0;
0188         if (cur_pos > fc_pci->dma[0].size * 2)
0189             goto error;
0190 
0191         deb_irq("%u irq: %08x cur_addr: %llx: cur_pos: %08x, last_cur_pos: %08x ",
0192                 jiffies_to_usecs(jiffies - fc_pci->last_irq),
0193                 v.raw, (unsigned long long)cur_addr, cur_pos,
0194                 fc_pci->last_dma1_cur_pos);
0195         fc_pci->last_irq = jiffies;
0196 
0197         /* buffer end was reached, restarted from the beginning
0198          * pass the data from last_cur_pos to the buffer end to the demux
0199          */
0200         if (cur_pos < fc_pci->last_dma1_cur_pos) {
0201             deb_irq(" end was reached: passing %d bytes ",
0202                 (fc_pci->dma[0].size*2 - 1) -
0203                 fc_pci->last_dma1_cur_pos);
0204             flexcop_pass_dmx_data(fc_pci->fc_dev,
0205                 fc_pci->dma[0].cpu_addr0 +
0206                     fc_pci->last_dma1_cur_pos,
0207                 (fc_pci->dma[0].size*2) -
0208                     fc_pci->last_dma1_cur_pos);
0209             fc_pci->last_dma1_cur_pos = 0;
0210         }
0211 
0212         if (cur_pos > fc_pci->last_dma1_cur_pos) {
0213             deb_irq(" passing %d bytes ",
0214                 cur_pos - fc_pci->last_dma1_cur_pos);
0215             flexcop_pass_dmx_data(fc_pci->fc_dev,
0216                 fc_pci->dma[0].cpu_addr0 +
0217                     fc_pci->last_dma1_cur_pos,
0218                 cur_pos - fc_pci->last_dma1_cur_pos);
0219         }
0220         deb_irq("\n");
0221 
0222         fc_pci->last_dma1_cur_pos = cur_pos;
0223         fc_pci->count++;
0224     } else {
0225         deb_irq("isr for flexcop called, apparently without reason (%08x)\n",
0226             v.raw);
0227         ret = IRQ_NONE;
0228     }
0229 
0230 error:
0231     spin_unlock_irqrestore(&fc_pci->irq_lock, flags);
0232     return ret;
0233 }
0234 
0235 static int flexcop_pci_stream_control(struct flexcop_device *fc, int onoff)
0236 {
0237     struct flexcop_pci *fc_pci = fc->bus_specific;
0238     if (onoff) {
0239         flexcop_dma_config(fc, &fc_pci->dma[0], FC_DMA_1);
0240         flexcop_dma_config(fc, &fc_pci->dma[1], FC_DMA_2);
0241         flexcop_dma_config_timer(fc, FC_DMA_1, 0);
0242         flexcop_dma_xfer_control(fc, FC_DMA_1,
0243                 FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1, 1);
0244         deb_irq("DMA xfer enabled\n");
0245 
0246         fc_pci->last_dma1_cur_pos = 0;
0247         flexcop_dma_control_timer_irq(fc, FC_DMA_1, 1);
0248         deb_irq("IRQ enabled\n");
0249         fc_pci->count_prev = fc_pci->count;
0250     } else {
0251         flexcop_dma_control_timer_irq(fc, FC_DMA_1, 0);
0252         deb_irq("IRQ disabled\n");
0253 
0254         flexcop_dma_xfer_control(fc, FC_DMA_1,
0255              FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1, 0);
0256         deb_irq("DMA xfer disabled\n");
0257     }
0258     return 0;
0259 }
0260 
0261 static int flexcop_pci_dma_init(struct flexcop_pci *fc_pci)
0262 {
0263     int ret;
0264     ret = flexcop_dma_allocate(fc_pci->pdev, &fc_pci->dma[0],
0265             FC_DEFAULT_DMA1_BUFSIZE);
0266     if (ret != 0)
0267         return ret;
0268 
0269     ret = flexcop_dma_allocate(fc_pci->pdev, &fc_pci->dma[1],
0270             FC_DEFAULT_DMA2_BUFSIZE);
0271     if (ret != 0) {
0272         flexcop_dma_free(&fc_pci->dma[0]);
0273         return ret;
0274     }
0275 
0276     flexcop_sram_set_dest(fc_pci->fc_dev, FC_SRAM_DEST_MEDIA |
0277             FC_SRAM_DEST_NET, FC_SRAM_DEST_TARGET_DMA1);
0278     flexcop_sram_set_dest(fc_pci->fc_dev, FC_SRAM_DEST_CAO |
0279             FC_SRAM_DEST_CAI, FC_SRAM_DEST_TARGET_DMA2);
0280     fc_pci->init_state |= FC_PCI_DMA_INIT;
0281     return ret;
0282 }
0283 
0284 static void flexcop_pci_dma_exit(struct flexcop_pci *fc_pci)
0285 {
0286     if (fc_pci->init_state & FC_PCI_DMA_INIT) {
0287         flexcop_dma_free(&fc_pci->dma[0]);
0288         flexcop_dma_free(&fc_pci->dma[1]);
0289     }
0290     fc_pci->init_state &= ~FC_PCI_DMA_INIT;
0291 }
0292 
0293 static int flexcop_pci_init(struct flexcop_pci *fc_pci)
0294 {
0295     int ret;
0296 
0297     info("card revision %x", fc_pci->pdev->revision);
0298 
0299     if ((ret = pci_enable_device(fc_pci->pdev)) != 0)
0300         return ret;
0301     pci_set_master(fc_pci->pdev);
0302 
0303     if ((ret = pci_request_regions(fc_pci->pdev, DRIVER_NAME)) != 0)
0304         goto err_pci_disable_device;
0305 
0306     fc_pci->io_mem = pci_iomap(fc_pci->pdev, 0, 0x800);
0307 
0308     if (!fc_pci->io_mem) {
0309         err("cannot map io memory\n");
0310         ret = -EIO;
0311         goto err_pci_release_regions;
0312     }
0313 
0314     pci_set_drvdata(fc_pci->pdev, fc_pci);
0315     spin_lock_init(&fc_pci->irq_lock);
0316     if ((ret = request_irq(fc_pci->pdev->irq, flexcop_pci_isr,
0317                     IRQF_SHARED, DRIVER_NAME, fc_pci)) != 0)
0318         goto err_pci_iounmap;
0319 
0320     fc_pci->init_state |= FC_PCI_INIT;
0321     return ret;
0322 
0323 err_pci_iounmap:
0324     pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
0325 err_pci_release_regions:
0326     pci_release_regions(fc_pci->pdev);
0327 err_pci_disable_device:
0328     pci_disable_device(fc_pci->pdev);
0329     return ret;
0330 }
0331 
0332 static void flexcop_pci_exit(struct flexcop_pci *fc_pci)
0333 {
0334     if (fc_pci->init_state & FC_PCI_INIT) {
0335         free_irq(fc_pci->pdev->irq, fc_pci);
0336         pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
0337         pci_release_regions(fc_pci->pdev);
0338         pci_disable_device(fc_pci->pdev);
0339     }
0340     fc_pci->init_state &= ~FC_PCI_INIT;
0341 }
0342 
0343 static int flexcop_pci_probe(struct pci_dev *pdev,
0344         const struct pci_device_id *ent)
0345 {
0346     struct flexcop_device *fc;
0347     struct flexcop_pci *fc_pci;
0348     int ret = -ENOMEM;
0349 
0350     if ((fc = flexcop_device_kmalloc(sizeof(struct flexcop_pci))) == NULL) {
0351         err("out of memory\n");
0352         return -ENOMEM;
0353     }
0354 
0355     /* general flexcop init */
0356     fc_pci = fc->bus_specific;
0357     fc_pci->fc_dev = fc;
0358 
0359     fc->read_ibi_reg = flexcop_pci_read_ibi_reg;
0360     fc->write_ibi_reg = flexcop_pci_write_ibi_reg;
0361     fc->i2c_request = flexcop_i2c_request;
0362     fc->get_mac_addr = flexcop_eeprom_check_mac_addr;
0363     fc->stream_control = flexcop_pci_stream_control;
0364 
0365     if (enable_pid_filtering)
0366         info("will use the HW PID filter.");
0367     else
0368         info("will pass the complete TS to the demuxer.");
0369 
0370     fc->pid_filtering = enable_pid_filtering;
0371     fc->bus_type = FC_PCI;
0372     fc->dev = &pdev->dev;
0373     fc->owner = THIS_MODULE;
0374 
0375     /* bus specific part */
0376     fc_pci->pdev = pdev;
0377     if ((ret = flexcop_pci_init(fc_pci)) != 0)
0378         goto err_kfree;
0379 
0380     /* init flexcop */
0381     if ((ret = flexcop_device_initialize(fc)) != 0)
0382         goto err_pci_exit;
0383 
0384     /* init dma */
0385     if ((ret = flexcop_pci_dma_init(fc_pci)) != 0)
0386         goto err_fc_exit;
0387 
0388     INIT_DELAYED_WORK(&fc_pci->irq_check_work, flexcop_pci_irq_check_work);
0389 
0390     if (irq_chk_intv > 0)
0391         schedule_delayed_work(&fc_pci->irq_check_work,
0392                 msecs_to_jiffies(irq_chk_intv < 100 ?
0393                     100 :
0394                     irq_chk_intv));
0395     return ret;
0396 
0397 err_fc_exit:
0398     flexcop_device_exit(fc);
0399 err_pci_exit:
0400     flexcop_pci_exit(fc_pci);
0401 err_kfree:
0402     flexcop_device_kfree(fc);
0403     return ret;
0404 }
0405 
0406 /* in theory every _exit function should be called exactly two times,
0407  * here and in the bail-out-part of the _init-function
0408  */
0409 static void flexcop_pci_remove(struct pci_dev *pdev)
0410 {
0411     struct flexcop_pci *fc_pci = pci_get_drvdata(pdev);
0412 
0413     if (irq_chk_intv > 0)
0414         cancel_delayed_work(&fc_pci->irq_check_work);
0415 
0416     flexcop_pci_dma_exit(fc_pci);
0417     flexcop_device_exit(fc_pci->fc_dev);
0418     flexcop_pci_exit(fc_pci);
0419     flexcop_device_kfree(fc_pci->fc_dev);
0420 }
0421 
0422 static const struct pci_device_id flexcop_pci_tbl[] = {
0423     { PCI_DEVICE(0x13d0, 0x2103) },
0424     { },
0425 };
0426 
0427 MODULE_DEVICE_TABLE(pci, flexcop_pci_tbl);
0428 
0429 static struct pci_driver flexcop_pci_driver = {
0430     .name     = "b2c2_flexcop_pci",
0431     .id_table = flexcop_pci_tbl,
0432     .probe    = flexcop_pci_probe,
0433     .remove   = flexcop_pci_remove,
0434 };
0435 
0436 module_pci_driver(flexcop_pci_driver);
0437 
0438 MODULE_AUTHOR(DRIVER_AUTHOR);
0439 MODULE_DESCRIPTION(DRIVER_NAME);
0440 MODULE_LICENSE("GPL");