]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/pci/b2c2/flexcop-pci.c
Merge branches 'for-4.11/upstream-fixes', 'for-4.12/accutouch', 'for-4.12/cp2112...
[mirror_ubuntu-artful-kernel.git] / drivers / media / pci / b2c2 / flexcop-pci.c
CommitLineData
2add87a9 1/*
91b94366
UB
2 * Linux driver the digital TV devices equipped with B2C2 FlexcopII(b)/III
3 * flexcop-pci.c - covers the PCI part including DMA transfers
4 * see flexcop.c for copyright information
2add87a9
JS
5 */
6
7#define FC_LOG_PREFIX "flexcop-pci"
8#include "flexcop-common.h"
9
c4ee3fd4 10static int enable_pid_filtering = 1;
2add87a9 11module_param(enable_pid_filtering, int, 0444);
91b94366
UB
12MODULE_PARM_DESC(enable_pid_filtering,
13 "enable hardware pid filtering: supported values: 0 (fullts), 1");
2add87a9 14
382c5546 15static int irq_chk_intv = 100;
64221be7 16module_param(irq_chk_intv, int, 0644);
382c5546 17MODULE_PARM_DESC(irq_chk_intv, "set the interval for IRQ streaming watchdog.");
64221be7 18
2add87a9
JS
19#ifdef CONFIG_DVB_B2C2_FLEXCOP_DEBUG
20#define dprintk(level,args...) \
21 do { if ((debug & level)) printk(args); } while (0)
22#define DEBSTATUS ""
23#else
24#define dprintk(level,args...)
25#define DEBSTATUS " (debugging is not enabled)"
26#endif
27
91b94366
UB
28#define deb_info(args...) dprintk(0x01, args)
29#define deb_reg(args...) dprintk(0x02, args)
30#define deb_ts(args...) dprintk(0x04, args)
31#define deb_irq(args...) dprintk(0x08, args)
32#define deb_chk(args...) dprintk(0x10, args)
2add87a9 33
ff699e6b 34static int debug;
2add87a9 35module_param(debug, int, 0644);
382c5546
PB
36MODULE_PARM_DESC(debug,
37 "set debug level (1=info,2=regs,4=TS,8=irqdma,16=check (|-able))."
91b94366 38 DEBSTATUS);
2add87a9
JS
39
40#define DRIVER_VERSION "0.1"
b934c20d 41#define DRIVER_NAME "flexcop-pci"
99e44da7 42#define DRIVER_AUTHOR "Patrick Boettcher <patrick.boettcher@posteo.de>"
2add87a9
JS
43
44struct flexcop_pci {
45 struct pci_dev *pdev;
46
47#define FC_PCI_INIT 0x01
48#define FC_PCI_DMA_INIT 0x02
49 int init_state;
50
51 void __iomem *io_mem;
52 u32 irq;
91b94366
UB
53 /* buffersize (at least for DMA1, need to be % 188 == 0,
54 * this logic is required */
2add87a9
JS
55#define FC_DEFAULT_DMA1_BUFSIZE (1280 * 188)
56#define FC_DEFAULT_DMA2_BUFSIZE (10 * 188)
57 struct flexcop_dma dma[2];
58
59 int active_dma1_addr; /* 0 = addr0 of dma1; 1 = addr1 of dma1 */
91b94366 60 u32 last_dma1_cur_pos;
25985edc 61 /* position of the pointer last time the timer/packet irq occurred */
c4ee3fd4 62 int count;
382c5546
PB
63 int count_prev;
64 int stream_problem;
2add87a9 65
4853f16a 66 spinlock_t irq_lock;
64221be7
PB
67 unsigned long last_irq;
68
c4028958 69 struct delayed_work irq_check_work;
2add87a9
JS
70 struct flexcop_device *fc_dev;
71};
72
91b94366 73static int lastwreg, lastwval, lastrreg, lastrval;
2add87a9 74
91b94366
UB
75static flexcop_ibi_value flexcop_pci_read_ibi_reg(struct flexcop_device *fc,
76 flexcop_ibi_register r)
2add87a9
JS
77{
78 struct flexcop_pci *fc_pci = fc->bus_specific;
79 flexcop_ibi_value v;
80 v.raw = readl(fc_pci->io_mem + r);
81
82 if (lastrreg != r || lastrval != v.raw) {
83 lastrreg = r; lastrval = v.raw;
91b94366 84 deb_reg("new rd: %3x: %08x\n", r, v.raw);
2add87a9
JS
85 }
86
87 return v;
88}
89
91b94366
UB
90static int flexcop_pci_write_ibi_reg(struct flexcop_device *fc,
91 flexcop_ibi_register r, flexcop_ibi_value v)
2add87a9
JS
92{
93 struct flexcop_pci *fc_pci = fc->bus_specific;
94
95 if (lastwreg != r || lastwval != v.raw) {
96 lastwreg = r; lastwval = v.raw;
91b94366 97 deb_reg("new wr: %3x: %08x\n", r, v.raw);
2add87a9
JS
98 }
99
100 writel(v.raw, fc_pci->io_mem + r);
101 return 0;
102}
103
c4028958 104static void flexcop_pci_irq_check_work(struct work_struct *work)
64221be7 105{
c4028958
DH
106 struct flexcop_pci *fc_pci =
107 container_of(work, struct flexcop_pci, irq_check_work.work);
64221be7
PB
108 struct flexcop_device *fc = fc_pci->fc_dev;
109
382c5546
PB
110 if (fc->feedcount) {
111
112 if (fc_pci->count == fc_pci->count_prev) {
113 deb_chk("no IRQ since the last check\n");
114 if (fc_pci->stream_problem++ == 3) {
115 struct dvb_demux_feed *feed;
a27e4fd3 116 deb_info("flexcop-pci: stream problem, resetting pid filter\n");
382c5546
PB
117
118 spin_lock_irq(&fc->demux.lock);
119 list_for_each_entry(feed, &fc->demux.feed_list,
91b94366 120 list_head) {
382c5546
PB
121 flexcop_pid_feed_control(fc, feed, 0);
122 }
123
124 list_for_each_entry(feed, &fc->demux.feed_list,
91b94366 125 list_head) {
382c5546
PB
126 flexcop_pid_feed_control(fc, feed, 1);
127 }
128 spin_unlock_irq(&fc->demux.lock);
129
130 fc_pci->stream_problem = 0;
131 }
132 } else {
133 fc_pci->stream_problem = 0;
134 fc_pci->count_prev = fc_pci->count;
135 }
136 }
64221be7
PB
137
138 schedule_delayed_work(&fc_pci->irq_check_work,
139 msecs_to_jiffies(irq_chk_intv < 100 ? 100 : irq_chk_intv));
140}
141
2add87a9
JS
142/* When PID filtering is turned on, we use the timer IRQ, because small amounts
143 * of data need to be passed to the user space instantly as well. When PID
144 * filtering is turned off, we use the page-change-IRQ */
7d12e780 145static irqreturn_t flexcop_pci_isr(int irq, void *dev_id)
2add87a9
JS
146{
147 struct flexcop_pci *fc_pci = dev_id;
148 struct flexcop_device *fc = fc_pci->fc_dev;
4f210e07 149 unsigned long flags;
64221be7 150 flexcop_ibi_value v;
4853f16a
JS
151 irqreturn_t ret = IRQ_HANDLED;
152
91b94366
UB
153 spin_lock_irqsave(&fc_pci->irq_lock, flags);
154 v = fc->read_ibi_reg(fc, irq_20c);
64221be7 155
91b94366 156 /* errors */
64221be7
PB
157 if (v.irq_20c.Data_receiver_error)
158 deb_chk("data receiver error\n");
159 if (v.irq_20c.Continuity_error_flag)
187a1446 160 deb_chk("Continuity error flag is set\n");
64221be7
PB
161 if (v.irq_20c.LLC_SNAP_FLAG_set)
162 deb_chk("LLC_SNAP_FLAG_set is set\n");
163 if (v.irq_20c.Transport_Error)
164 deb_chk("Transport error\n");
165
166 if ((fc_pci->count % 1000) == 0)
91b94366 167 deb_chk("%d valid irq took place so far\n", fc_pci->count);
64221be7 168
2add87a9
JS
169 if (v.irq_20c.DMA1_IRQ_Status == 1) {
170 if (fc_pci->active_dma1_addr == 0)
91b94366
UB
171 flexcop_pass_dmx_packets(fc_pci->fc_dev,
172 fc_pci->dma[0].cpu_addr0,
173 fc_pci->dma[0].size / 188);
2add87a9 174 else
91b94366
UB
175 flexcop_pass_dmx_packets(fc_pci->fc_dev,
176 fc_pci->dma[0].cpu_addr1,
177 fc_pci->dma[0].size / 188);
2add87a9
JS
178
179 deb_irq("page change to page: %d\n",!fc_pci->active_dma1_addr);
180 fc_pci->active_dma1_addr = !fc_pci->active_dma1_addr;
2add87a9
JS
181 /* for the timer IRQ we only can use buffer dmx feeding, because we don't have
182 * complete TS packets when reading from the DMA memory */
91b94366 183 } else if (v.irq_20c.DMA1_Timer_Status == 1) {
2add87a9
JS
184 dma_addr_t cur_addr =
185 fc->read_ibi_reg(fc,dma1_008).dma_0x8.dma_cur_addr << 2;
186 u32 cur_pos = cur_addr - fc_pci->dma[0].dma_addr0;
187
d6376f0e 188 deb_irq("%u irq: %08x cur_addr: %llx: cur_pos: %08x, last_cur_pos: %08x ",
608268b2
RD
189 jiffies_to_usecs(jiffies - fc_pci->last_irq),
190 v.raw, (unsigned long long)cur_addr, cur_pos,
191 fc_pci->last_dma1_cur_pos);
64221be7 192 fc_pci->last_irq = jiffies;
c4ee3fd4 193
2add87a9
JS
194 /* buffer end was reached, restarted from the beginning
195 * pass the data from last_cur_pos to the buffer end to the demux
196 */
197 if (cur_pos < fc_pci->last_dma1_cur_pos) {
91b94366
UB
198 deb_irq(" end was reached: passing %d bytes ",
199 (fc_pci->dma[0].size*2 - 1) -
200 fc_pci->last_dma1_cur_pos);
2add87a9 201 flexcop_pass_dmx_data(fc_pci->fc_dev,
91b94366
UB
202 fc_pci->dma[0].cpu_addr0 +
203 fc_pci->last_dma1_cur_pos,
204 (fc_pci->dma[0].size*2) -
205 fc_pci->last_dma1_cur_pos);
2add87a9
JS
206 fc_pci->last_dma1_cur_pos = 0;
207 }
208
209 if (cur_pos > fc_pci->last_dma1_cur_pos) {
91b94366
UB
210 deb_irq(" passing %d bytes ",
211 cur_pos - fc_pci->last_dma1_cur_pos);
2add87a9 212 flexcop_pass_dmx_data(fc_pci->fc_dev,
91b94366
UB
213 fc_pci->dma[0].cpu_addr0 +
214 fc_pci->last_dma1_cur_pos,
215 cur_pos - fc_pci->last_dma1_cur_pos);
2add87a9 216 }
c4ee3fd4 217 deb_irq("\n");
2add87a9
JS
218
219 fc_pci->last_dma1_cur_pos = cur_pos;
64221be7
PB
220 fc_pci->count++;
221 } else {
d6376f0e
MCC
222 deb_irq("isr for flexcop called, apparently without reason (%08x)\n",
223 v.raw);
4853f16a 224 ret = IRQ_NONE;
64221be7 225 }
4853f16a 226
91b94366 227 spin_unlock_irqrestore(&fc_pci->irq_lock, flags);
4853f16a 228 return ret;
2add87a9
JS
229}
230
231static int flexcop_pci_stream_control(struct flexcop_device *fc, int onoff)
232{
233 struct flexcop_pci *fc_pci = fc->bus_specific;
234 if (onoff) {
91b94366
UB
235 flexcop_dma_config(fc, &fc_pci->dma[0], FC_DMA_1);
236 flexcop_dma_config(fc, &fc_pci->dma[1], FC_DMA_2);
237 flexcop_dma_config_timer(fc, FC_DMA_1, 0);
238 flexcop_dma_xfer_control(fc, FC_DMA_1,
239 FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1, 1);
64221be7 240 deb_irq("DMA xfer enabled\n");
2add87a9 241
64221be7 242 fc_pci->last_dma1_cur_pos = 0;
91b94366 243 flexcop_dma_control_timer_irq(fc, FC_DMA_1, 1);
64221be7 244 deb_irq("IRQ enabled\n");
382c5546 245 fc_pci->count_prev = fc_pci->count;
2add87a9 246 } else {
91b94366 247 flexcop_dma_control_timer_irq(fc, FC_DMA_1, 0);
64221be7 248 deb_irq("IRQ disabled\n");
2add87a9 249
91b94366
UB
250 flexcop_dma_xfer_control(fc, FC_DMA_1,
251 FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1, 0);
64221be7 252 deb_irq("DMA xfer disabled\n");
2add87a9 253 }
2add87a9
JS
254 return 0;
255}
256
257static int flexcop_pci_dma_init(struct flexcop_pci *fc_pci)
258{
259 int ret;
91b94366
UB
260 ret = flexcop_dma_allocate(fc_pci->pdev, &fc_pci->dma[0],
261 FC_DEFAULT_DMA1_BUFSIZE);
262 if (ret != 0)
2add87a9
JS
263 return ret;
264
91b94366
UB
265 ret = flexcop_dma_allocate(fc_pci->pdev, &fc_pci->dma[1],
266 FC_DEFAULT_DMA2_BUFSIZE);
267 if (ret != 0) {
8397703e
TP
268 flexcop_dma_free(&fc_pci->dma[0]);
269 return ret;
270 }
2add87a9 271
91b94366
UB
272 flexcop_sram_set_dest(fc_pci->fc_dev, FC_SRAM_DEST_MEDIA |
273 FC_SRAM_DEST_NET, FC_SRAM_DEST_TARGET_DMA1);
274 flexcop_sram_set_dest(fc_pci->fc_dev, FC_SRAM_DEST_CAO |
275 FC_SRAM_DEST_CAI, FC_SRAM_DEST_TARGET_DMA2);
2add87a9 276 fc_pci->init_state |= FC_PCI_DMA_INIT;
2add87a9
JS
277 return ret;
278}
279
280static void flexcop_pci_dma_exit(struct flexcop_pci *fc_pci)
281{
282 if (fc_pci->init_state & FC_PCI_DMA_INIT) {
283 flexcop_dma_free(&fc_pci->dma[0]);
284 flexcop_dma_free(&fc_pci->dma[1]);
285 }
286 fc_pci->init_state &= ~FC_PCI_DMA_INIT;
287}
288
289static int flexcop_pci_init(struct flexcop_pci *fc_pci)
290{
291 int ret;
2add87a9 292
abd34d8d 293 info("card revision %x", fc_pci->pdev->revision);
2add87a9
JS
294
295 if ((ret = pci_enable_device(fc_pci->pdev)) != 0)
296 return ret;
2add87a9
JS
297 pci_set_master(fc_pci->pdev);
298
2add87a9
JS
299 if ((ret = pci_request_regions(fc_pci->pdev, DRIVER_NAME)) != 0)
300 goto err_pci_disable_device;
301
302 fc_pci->io_mem = pci_iomap(fc_pci->pdev, 0, 0x800);
303
304 if (!fc_pci->io_mem) {
305 err("cannot map io memory\n");
306 ret = -EIO;
307 goto err_pci_release_regions;
308 }
309
310 pci_set_drvdata(fc_pci->pdev, fc_pci);
4f210e07 311 spin_lock_init(&fc_pci->irq_lock);
64221be7 312 if ((ret = request_irq(fc_pci->pdev->irq, flexcop_pci_isr,
8076fe32 313 IRQF_SHARED, DRIVER_NAME, fc_pci)) != 0)
2add87a9
JS
314 goto err_pci_iounmap;
315
316 fc_pci->init_state |= FC_PCI_INIT;
8397703e 317 return ret;
2add87a9
JS
318
319err_pci_iounmap:
320 pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
2add87a9
JS
321err_pci_release_regions:
322 pci_release_regions(fc_pci->pdev);
323err_pci_disable_device:
324 pci_disable_device(fc_pci->pdev);
2add87a9
JS
325 return ret;
326}
327
328static void flexcop_pci_exit(struct flexcop_pci *fc_pci)
329{
330 if (fc_pci->init_state & FC_PCI_INIT) {
331 free_irq(fc_pci->pdev->irq, fc_pci);
332 pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
2add87a9
JS
333 pci_release_regions(fc_pci->pdev);
334 pci_disable_device(fc_pci->pdev);
335 }
336 fc_pci->init_state &= ~FC_PCI_INIT;
337}
338
91b94366
UB
339static int flexcop_pci_probe(struct pci_dev *pdev,
340 const struct pci_device_id *ent)
2add87a9
JS
341{
342 struct flexcop_device *fc;
343 struct flexcop_pci *fc_pci;
344 int ret = -ENOMEM;
345
346 if ((fc = flexcop_device_kmalloc(sizeof(struct flexcop_pci))) == NULL) {
347 err("out of memory\n");
348 return -ENOMEM;
349 }
350
91b94366 351 /* general flexcop init */
2add87a9
JS
352 fc_pci = fc->bus_specific;
353 fc_pci->fc_dev = fc;
354
355 fc->read_ibi_reg = flexcop_pci_read_ibi_reg;
356 fc->write_ibi_reg = flexcop_pci_write_ibi_reg;
357 fc->i2c_request = flexcop_i2c_request;
358 fc->get_mac_addr = flexcop_eeprom_check_mac_addr;
2add87a9
JS
359 fc->stream_control = flexcop_pci_stream_control;
360
c4ee3fd4
JS
361 if (enable_pid_filtering)
362 info("will use the HW PID filter.");
363 else
364 info("will pass the complete TS to the demuxer.");
365
2add87a9
JS
366 fc->pid_filtering = enable_pid_filtering;
367 fc->bus_type = FC_PCI;
2add87a9 368 fc->dev = &pdev->dev;
59a7ad6c 369 fc->owner = THIS_MODULE;
2add87a9 370
91b94366 371 /* bus specific part */
2add87a9
JS
372 fc_pci->pdev = pdev;
373 if ((ret = flexcop_pci_init(fc_pci)) != 0)
374 goto err_kfree;
375
91b94366 376 /* init flexcop */
2add87a9
JS
377 if ((ret = flexcop_device_initialize(fc)) != 0)
378 goto err_pci_exit;
379
91b94366 380 /* init dma */
2add87a9
JS
381 if ((ret = flexcop_pci_dma_init(fc_pci)) != 0)
382 goto err_fc_exit;
383
c4028958 384 INIT_DELAYED_WORK(&fc_pci->irq_check_work, flexcop_pci_irq_check_work);
64221be7 385
91b94366
UB
386 if (irq_chk_intv > 0)
387 schedule_delayed_work(&fc_pci->irq_check_work,
388 msecs_to_jiffies(irq_chk_intv < 100 ?
389 100 :
390 irq_chk_intv));
8397703e
TP
391 return ret;
392
2add87a9
JS
393err_fc_exit:
394 flexcop_device_exit(fc);
395err_pci_exit:
396 flexcop_pci_exit(fc_pci);
397err_kfree:
398 flexcop_device_kfree(fc);
2add87a9
JS
399 return ret;
400}
401
402/* in theory every _exit function should be called exactly two times,
403 * here and in the bail-out-part of the _init-function
404 */
405static void flexcop_pci_remove(struct pci_dev *pdev)
406{
407 struct flexcop_pci *fc_pci = pci_get_drvdata(pdev);
408
382c5546
PB
409 if (irq_chk_intv > 0)
410 cancel_delayed_work(&fc_pci->irq_check_work);
411
2add87a9
JS
412 flexcop_pci_dma_exit(fc_pci);
413 flexcop_device_exit(fc_pci->fc_dev);
414 flexcop_pci_exit(fc_pci);
415 flexcop_device_kfree(fc_pci->fc_dev);
416}
417
418static struct pci_device_id flexcop_pci_tbl[] = {
419 { PCI_DEVICE(0x13d0, 0x2103) },
2add87a9
JS
420 { },
421};
422
423MODULE_DEVICE_TABLE(pci, flexcop_pci_tbl);
424
425static struct pci_driver flexcop_pci_driver = {
63b5c1c4 426 .name = "b2c2_flexcop_pci",
2add87a9 427 .id_table = flexcop_pci_tbl,
64221be7
PB
428 .probe = flexcop_pci_probe,
429 .remove = flexcop_pci_remove,
2add87a9
JS
430};
431
2c5f81d2 432module_pci_driver(flexcop_pci_driver);
2add87a9
JS
433
434MODULE_AUTHOR(DRIVER_AUTHOR);
435MODULE_DESCRIPTION(DRIVER_NAME);
436MODULE_LICENSE("GPL");