1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the NXP SAA7164 PCIe bridge
5 * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
8 #include <linux/init.h>
9 #include <linux/list.h>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/kmod.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/interrupt.h>
16 #include <linux/debugfs.h>
17 #include <linux/delay.h>
18 #include <asm/div64.h>
22 MODULE_DESCRIPTION("Driver for NXP SAA7164 based TV cards");
23 MODULE_AUTHOR("Steven Toth <stoth@kernellabs.com>");
24 MODULE_LICENSE("GPL");
35 unsigned int saa_debug
;
36 module_param_named(debug
, saa_debug
, int, 0644);
37 MODULE_PARM_DESC(debug
, "enable debug messages");
39 static unsigned int fw_debug
;
40 module_param(fw_debug
, int, 0644);
41 MODULE_PARM_DESC(fw_debug
, "Firmware debug level def:2");
43 unsigned int encoder_buffers
= SAA7164_MAX_ENCODER_BUFFERS
;
44 module_param(encoder_buffers
, int, 0644);
45 MODULE_PARM_DESC(encoder_buffers
, "Total buffers in read queue 16-512 def:64");
47 unsigned int vbi_buffers
= SAA7164_MAX_VBI_BUFFERS
;
48 module_param(vbi_buffers
, int, 0644);
49 MODULE_PARM_DESC(vbi_buffers
, "Total buffers in read queue 16-512 def:64");
51 unsigned int waitsecs
= 10;
52 module_param(waitsecs
, int, 0644);
53 MODULE_PARM_DESC(waitsecs
, "timeout on firmware messages");
55 static unsigned int card
[] = {[0 ... (SAA7164_MAXBOARDS
- 1)] = UNSET
};
56 module_param_array(card
, int, NULL
, 0444);
57 MODULE_PARM_DESC(card
, "card type");
59 static unsigned int print_histogram
= 64;
60 module_param(print_histogram
, int, 0644);
61 MODULE_PARM_DESC(print_histogram
, "print histogram values once");
63 unsigned int crc_checking
= 1;
64 module_param(crc_checking
, int, 0644);
65 MODULE_PARM_DESC(crc_checking
, "enable crc sanity checking on buffers");
67 static unsigned int guard_checking
= 1;
68 module_param(guard_checking
, int, 0644);
69 MODULE_PARM_DESC(guard_checking
,
70 "enable dma sanity checking for buffer overruns");
72 static bool enable_msi
= true;
73 module_param(enable_msi
, bool, 0444);
74 MODULE_PARM_DESC(enable_msi
,
75 "enable the use of an msi interrupt if available");
77 static unsigned int saa7164_devcount
;
79 static DEFINE_MUTEX(devlist
);
80 LIST_HEAD(saa7164_devlist
);
84 static void saa7164_pack_verifier(struct saa7164_buffer
*buf
)
86 u8
*p
= (u8
*)buf
->cpu
;
89 for (i
= 0; i
< buf
->actual_size
; i
+= 2048) {
91 if ((*(p
+ i
+ 0) != 0x00) || (*(p
+ i
+ 1) != 0x00) ||
92 (*(p
+ i
+ 2) != 0x01) || (*(p
+ i
+ 3) != 0xBA)) {
93 printk(KERN_ERR
"No pack at 0x%x\n", i
);
95 print_hex_dump(KERN_INFO
, "", DUMP_PREFIX_OFFSET
, 16, 1,
102 #define FIXED_VIDEO_PID 0xf1
103 #define FIXED_AUDIO_PID 0xf2
105 static void saa7164_ts_verifier(struct saa7164_buffer
*buf
)
107 struct saa7164_port
*port
= buf
->port
;
111 u8
*bufcpu
= (u8
*)buf
->cpu
;
113 port
->sync_errors
= 0;
114 port
->v_cc_errors
= 0;
115 port
->a_cc_errors
= 0;
117 for (i
= 0; i
< buf
->actual_size
; i
+= 188) {
118 if (*(bufcpu
+ i
) != 0x47)
121 /* TODO: Query pid lower 8 bits, ignoring upper bits intensionally */
122 pid
= ((*(bufcpu
+ i
+ 1) & 0x1f) << 8) | *(bufcpu
+ i
+ 2);
123 cc
= *(bufcpu
+ i
+ 3) & 0x0f;
125 if (pid
== FIXED_VIDEO_PID
) {
126 a
= ((port
->last_v_cc
+ 1) & 0x0f);
128 printk(KERN_ERR
"video cc last = %x current = %x i = %d\n",
129 port
->last_v_cc
, cc
, i
);
133 port
->last_v_cc
= cc
;
135 if (pid
== FIXED_AUDIO_PID
) {
136 a
= ((port
->last_a_cc
+ 1) & 0x0f);
138 printk(KERN_ERR
"audio cc last = %x current = %x i = %d\n",
139 port
->last_a_cc
, cc
, i
);
143 port
->last_a_cc
= cc
;
148 /* Only report errors if we've been through this function at least
149 * once already and the cached cc values are primed. First time through
150 * always generates errors.
152 if (port
->v_cc_errors
&& (port
->done_first_interrupt
> 1))
153 printk(KERN_ERR
"video pid cc, %d errors\n", port
->v_cc_errors
);
155 if (port
->a_cc_errors
&& (port
->done_first_interrupt
> 1))
156 printk(KERN_ERR
"audio pid cc, %d errors\n", port
->a_cc_errors
);
158 if (port
->sync_errors
&& (port
->done_first_interrupt
> 1))
159 printk(KERN_ERR
"sync_errors = %d\n", port
->sync_errors
);
161 if (port
->done_first_interrupt
== 1)
162 port
->done_first_interrupt
++;
165 static void saa7164_histogram_reset(struct saa7164_histogram
*hg
, char *name
)
169 memset(hg
, 0, sizeof(struct saa7164_histogram
));
170 strscpy(hg
->name
, name
, sizeof(hg
->name
));
172 /* First 30ms x 1ms */
173 for (i
= 0; i
< 30; i
++)
174 hg
->counter1
[0 + i
].val
= i
;
176 /* 30 - 200ms x 10ms */
177 for (i
= 0; i
< 18; i
++)
178 hg
->counter1
[30 + i
].val
= 30 + (i
* 10);
180 /* 200 - 2000ms x 100ms */
181 for (i
= 0; i
< 15; i
++)
182 hg
->counter1
[48 + i
].val
= 200 + (i
* 200);
184 /* Catch all massive value (2secs) */
185 hg
->counter1
[55].val
= 2000;
187 /* Catch all massive value (4secs) */
188 hg
->counter1
[56].val
= 4000;
190 /* Catch all massive value (8secs) */
191 hg
->counter1
[57].val
= 8000;
193 /* Catch all massive value (15secs) */
194 hg
->counter1
[58].val
= 15000;
196 /* Catch all massive value (30secs) */
197 hg
->counter1
[59].val
= 30000;
199 /* Catch all massive value (60secs) */
200 hg
->counter1
[60].val
= 60000;
202 /* Catch all massive value (5mins) */
203 hg
->counter1
[61].val
= 300000;
205 /* Catch all massive value (15mins) */
206 hg
->counter1
[62].val
= 900000;
208 /* Catch all massive values (1hr) */
209 hg
->counter1
[63].val
= 3600000;
212 void saa7164_histogram_update(struct saa7164_histogram
*hg
, u32 val
)
215 for (i
= 0; i
< 64; i
++) {
216 if (val
<= hg
->counter1
[i
].val
) {
217 hg
->counter1
[i
].count
++;
218 hg
->counter1
[i
].update_time
= jiffies
;
224 static void saa7164_histogram_print(struct saa7164_port
*port
,
225 struct saa7164_histogram
*hg
)
230 printk(KERN_ERR
"Histogram named %s (ms, count, last_update_jiffy)\n", hg
->name
);
231 for (i
= 0; i
< 64; i
++) {
232 if (hg
->counter1
[i
].count
== 0)
235 printk(KERN_ERR
" %4d %12d %Ld\n",
237 hg
->counter1
[i
].count
,
238 hg
->counter1
[i
].update_time
);
242 printk(KERN_ERR
"Total: %d\n", entries
);
245 static void saa7164_work_enchandler_helper(struct saa7164_port
*port
, int bufnr
)
247 struct saa7164_dev
*dev
= port
->dev
;
248 struct saa7164_buffer
*buf
= NULL
;
249 struct saa7164_user_buffer
*ubuf
= NULL
;
250 struct list_head
*c
, *n
;
254 mutex_lock(&port
->dmaqueue_lock
);
255 list_for_each_safe(c
, n
, &port
->dmaqueue
.list
) {
257 buf
= list_entry(c
, struct saa7164_buffer
, list
);
258 if (i
++ > port
->hwcfg
.buffercount
) {
259 printk(KERN_ERR
"%s() illegal i count %d\n",
264 if (buf
->idx
== bufnr
) {
266 /* Found the buffer, deal with it */
267 dprintk(DBGLVL_IRQ
, "%s() bufnr: %d\n", __func__
, bufnr
);
270 /* Throw a new checksum on the dma buffer */
271 buf
->crc
= crc32(0, buf
->cpu
, buf
->actual_size
);
274 if (guard_checking
) {
276 if ((*(p
+ buf
->actual_size
+ 0) != 0xff) ||
277 (*(p
+ buf
->actual_size
+ 1) != 0xff) ||
278 (*(p
+ buf
->actual_size
+ 2) != 0xff) ||
279 (*(p
+ buf
->actual_size
+ 3) != 0xff) ||
280 (*(p
+ buf
->actual_size
+ 0x10) != 0xff) ||
281 (*(p
+ buf
->actual_size
+ 0x11) != 0xff) ||
282 (*(p
+ buf
->actual_size
+ 0x12) != 0xff) ||
283 (*(p
+ buf
->actual_size
+ 0x13) != 0xff)) {
284 printk(KERN_ERR
"%s() buf %p guard buffer breach\n",
287 print_hex_dump(KERN_INFO
, "", DUMP_PREFIX_OFFSET
, 16, 1,
288 p
+ buf
->actual_size
- 32, 64, false);
293 if ((port
->nr
!= SAA7164_PORT_VBI1
) && (port
->nr
!= SAA7164_PORT_VBI2
)) {
294 /* Validate the incoming buffer content */
295 if (port
->encoder_params
.stream_type
== V4L2_MPEG_STREAM_TYPE_MPEG2_TS
)
296 saa7164_ts_verifier(buf
);
297 else if (port
->encoder_params
.stream_type
== V4L2_MPEG_STREAM_TYPE_MPEG2_PS
)
298 saa7164_pack_verifier(buf
);
301 /* find a free user buffer and clone to it */
302 if (!list_empty(&port
->list_buf_free
.list
)) {
304 /* Pull the first buffer from the used list */
305 ubuf
= list_first_entry(&port
->list_buf_free
.list
,
306 struct saa7164_user_buffer
, list
);
308 if (buf
->actual_size
<= ubuf
->actual_size
) {
310 memcpy(ubuf
->data
, buf
->cpu
, ubuf
->actual_size
);
313 /* Throw a new checksum on the read buffer */
314 ubuf
->crc
= crc32(0, ubuf
->data
, ubuf
->actual_size
);
317 /* Requeue the buffer on the free list */
320 list_move_tail(&ubuf
->list
,
321 &port
->list_buf_used
.list
);
323 /* Flag any userland waiters */
324 wake_up_interruptible(&port
->wait_read
);
327 printk(KERN_ERR
"buf %p bufsize fails match\n", buf
);
331 printk(KERN_ERR
"encirq no free buffers, increase param encoder_buffers\n");
333 /* Ensure offset into buffer remains 0, fill buffer
334 * with known bad data. We check for this data at a later point
336 saa7164_buffer_zero_offsets(port
, bufnr
);
337 memset(buf
->cpu
, 0xff, buf
->pci_size
);
339 /* Throw yet aanother new checksum on the dma buffer */
340 buf
->crc
= crc32(0, buf
->cpu
, buf
->actual_size
);
346 mutex_unlock(&port
->dmaqueue_lock
);
349 static void saa7164_work_enchandler(struct work_struct
*w
)
351 struct saa7164_port
*port
=
352 container_of(w
, struct saa7164_port
, workenc
);
353 struct saa7164_dev
*dev
= port
->dev
;
357 port
->last_svc_msecs_diff
= port
->last_svc_msecs
;
358 port
->last_svc_msecs
= jiffies_to_msecs(jiffies
);
360 port
->last_svc_msecs_diff
= port
->last_svc_msecs
-
361 port
->last_svc_msecs_diff
;
363 saa7164_histogram_update(&port
->svc_interval
,
364 port
->last_svc_msecs_diff
);
366 port
->last_irq_svc_msecs_diff
= port
->last_svc_msecs
-
367 port
->last_irq_msecs
;
369 saa7164_histogram_update(&port
->irq_svc_interval
,
370 port
->last_irq_svc_msecs_diff
);
373 "%s() %Ldms elapsed irq->deferred %Ldms wp: %d rp: %d\n",
375 port
->last_svc_msecs_diff
,
376 port
->last_irq_svc_msecs_diff
,
381 /* Current write position */
382 wp
= saa7164_readl(port
->bufcounter
);
383 if (wp
> (port
->hwcfg
.buffercount
- 1)) {
384 printk(KERN_ERR
"%s() illegal buf count %d\n", __func__
, wp
);
388 /* Most current complete buffer */
390 mcb
= (port
->hwcfg
.buffercount
- 1);
395 if (port
->done_first_interrupt
== 0) {
396 port
->done_first_interrupt
++;
399 rp
= (port
->last_svc_rp
+ 1) % 8;
401 if (rp
> (port
->hwcfg
.buffercount
- 1)) {
402 printk(KERN_ERR
"%s() illegal rp count %d\n", __func__
, rp
);
406 saa7164_work_enchandler_helper(port
, rp
);
407 port
->last_svc_rp
= rp
;
413 /* TODO: Convert this into a /proc/saa7164 style readable file */
414 if (print_histogram
== port
->nr
) {
415 saa7164_histogram_print(port
, &port
->irq_interval
);
416 saa7164_histogram_print(port
, &port
->svc_interval
);
417 saa7164_histogram_print(port
, &port
->irq_svc_interval
);
418 saa7164_histogram_print(port
, &port
->read_interval
);
419 saa7164_histogram_print(port
, &port
->poll_interval
);
420 /* TODO: fix this to preserve any previous state */
421 print_histogram
= 64 + port
->nr
;
425 static void saa7164_work_vbihandler(struct work_struct
*w
)
427 struct saa7164_port
*port
=
428 container_of(w
, struct saa7164_port
, workenc
);
429 struct saa7164_dev
*dev
= port
->dev
;
433 port
->last_svc_msecs_diff
= port
->last_svc_msecs
;
434 port
->last_svc_msecs
= jiffies_to_msecs(jiffies
);
435 port
->last_svc_msecs_diff
= port
->last_svc_msecs
-
436 port
->last_svc_msecs_diff
;
438 saa7164_histogram_update(&port
->svc_interval
,
439 port
->last_svc_msecs_diff
);
441 port
->last_irq_svc_msecs_diff
= port
->last_svc_msecs
-
442 port
->last_irq_msecs
;
444 saa7164_histogram_update(&port
->irq_svc_interval
,
445 port
->last_irq_svc_msecs_diff
);
448 "%s() %Ldms elapsed irq->deferred %Ldms wp: %d rp: %d\n",
450 port
->last_svc_msecs_diff
,
451 port
->last_irq_svc_msecs_diff
,
456 /* Current write position */
457 wp
= saa7164_readl(port
->bufcounter
);
458 if (wp
> (port
->hwcfg
.buffercount
- 1)) {
459 printk(KERN_ERR
"%s() illegal buf count %d\n", __func__
, wp
);
463 /* Most current complete buffer */
465 mcb
= (port
->hwcfg
.buffercount
- 1);
470 if (port
->done_first_interrupt
== 0) {
471 port
->done_first_interrupt
++;
474 rp
= (port
->last_svc_rp
+ 1) % 8;
476 if (rp
> (port
->hwcfg
.buffercount
- 1)) {
477 printk(KERN_ERR
"%s() illegal rp count %d\n", __func__
, rp
);
481 saa7164_work_enchandler_helper(port
, rp
);
482 port
->last_svc_rp
= rp
;
488 /* TODO: Convert this into a /proc/saa7164 style readable file */
489 if (print_histogram
== port
->nr
) {
490 saa7164_histogram_print(port
, &port
->irq_interval
);
491 saa7164_histogram_print(port
, &port
->svc_interval
);
492 saa7164_histogram_print(port
, &port
->irq_svc_interval
);
493 saa7164_histogram_print(port
, &port
->read_interval
);
494 saa7164_histogram_print(port
, &port
->poll_interval
);
495 /* TODO: fix this to preserve any previous state */
496 print_histogram
= 64 + port
->nr
;
500 static void saa7164_work_cmdhandler(struct work_struct
*w
)
502 struct saa7164_dev
*dev
= container_of(w
, struct saa7164_dev
, workcmd
);
504 /* Wake up any complete commands */
505 saa7164_irq_dequeue(dev
);
508 static void saa7164_buffer_deliver(struct saa7164_buffer
*buf
)
510 struct saa7164_port
*port
= buf
->port
;
512 /* Feed the transport payload into the kernel demux */
513 dvb_dmx_swfilter_packets(&port
->dvb
.demux
, (u8
*)buf
->cpu
,
514 SAA7164_TS_NUMBER_OF_LINES
);
518 static irqreturn_t
saa7164_irq_vbi(struct saa7164_port
*port
)
520 struct saa7164_dev
*dev
= port
->dev
;
523 port
->last_irq_msecs_diff
= port
->last_irq_msecs
;
525 /* Collect new stats */
526 port
->last_irq_msecs
= jiffies_to_msecs(jiffies
);
528 /* Calculate stats */
529 port
->last_irq_msecs_diff
= port
->last_irq_msecs
-
530 port
->last_irq_msecs_diff
;
532 saa7164_histogram_update(&port
->irq_interval
,
533 port
->last_irq_msecs_diff
);
535 dprintk(DBGLVL_IRQ
, "%s() %Ldms elapsed\n", __func__
,
536 port
->last_irq_msecs_diff
);
538 /* Tis calls the vbi irq handler */
539 schedule_work(&port
->workenc
);
543 static irqreturn_t
saa7164_irq_encoder(struct saa7164_port
*port
)
545 struct saa7164_dev
*dev
= port
->dev
;
548 port
->last_irq_msecs_diff
= port
->last_irq_msecs
;
550 /* Collect new stats */
551 port
->last_irq_msecs
= jiffies_to_msecs(jiffies
);
553 /* Calculate stats */
554 port
->last_irq_msecs_diff
= port
->last_irq_msecs
-
555 port
->last_irq_msecs_diff
;
557 saa7164_histogram_update(&port
->irq_interval
,
558 port
->last_irq_msecs_diff
);
560 dprintk(DBGLVL_IRQ
, "%s() %Ldms elapsed\n", __func__
,
561 port
->last_irq_msecs_diff
);
563 schedule_work(&port
->workenc
);
567 static irqreturn_t
saa7164_irq_ts(struct saa7164_port
*port
)
569 struct saa7164_dev
*dev
= port
->dev
;
570 struct saa7164_buffer
*buf
;
571 struct list_head
*c
, *n
;
574 /* Find the current write point from the hardware */
575 wp
= saa7164_readl(port
->bufcounter
);
577 BUG_ON(wp
> (port
->hwcfg
.buffercount
- 1));
579 /* Find the previous buffer to the current write point */
581 rp
= (port
->hwcfg
.buffercount
- 1);
585 /* Lookup the WP in the buffer list */
586 /* TODO: turn this into a worker thread */
587 list_for_each_safe(c
, n
, &port
->dmaqueue
.list
) {
588 buf
= list_entry(c
, struct saa7164_buffer
, list
);
589 BUG_ON(i
> port
->hwcfg
.buffercount
);
592 if (buf
->idx
== rp
) {
593 /* Found the buffer, deal with it */
594 dprintk(DBGLVL_IRQ
, "%s() wp: %d processing: %d\n",
596 saa7164_buffer_deliver(buf
);
604 /* Primary IRQ handler and dispatch mechanism */
605 static irqreturn_t
saa7164_irq(int irq
, void *dev_id
)
607 struct saa7164_dev
*dev
= dev_id
;
608 struct saa7164_port
*porta
, *portb
, *portc
, *portd
, *porte
, *portf
;
610 u32 intid
, intstat
[INT_SIZE
/4];
611 int i
, handled
= 0, bit
;
614 printk(KERN_ERR
"%s() No device specified\n", __func__
);
619 porta
= &dev
->ports
[SAA7164_PORT_TS1
];
620 portb
= &dev
->ports
[SAA7164_PORT_TS2
];
621 portc
= &dev
->ports
[SAA7164_PORT_ENC1
];
622 portd
= &dev
->ports
[SAA7164_PORT_ENC2
];
623 porte
= &dev
->ports
[SAA7164_PORT_VBI1
];
624 portf
= &dev
->ports
[SAA7164_PORT_VBI2
];
626 /* Check that the hardware is accessible. If the status bytes are
627 * 0xFF then the device is not accessible, the IRQ belongs
629 * 4 x u32 interrupt registers.
631 for (i
= 0; i
< INT_SIZE
/4; i
++) {
633 /* TODO: Convert into saa7164_readl() */
634 /* Read the 4 hardware interrupt registers */
635 intstat
[i
] = saa7164_readl(dev
->int_status
+ (i
* 4));
643 /* For each of the HW interrupt registers */
644 for (i
= 0; i
< INT_SIZE
/4; i
++) {
647 /* Each function of the board has it's own interruptid.
648 * Find the function that triggered then call
651 for (bit
= 0; bit
< 32; bit
++) {
653 if (((intstat
[i
] >> bit
) & 0x00000001) == 0)
656 /* Calculate the interrupt id (0x00 to 0x7f) */
658 intid
= (i
* 32) + bit
;
659 if (intid
== dev
->intfdesc
.bInterruptId
) {
660 /* A response to an cmd/api call */
661 schedule_work(&dev
->workcmd
);
662 } else if (intid
== porta
->hwcfg
.interruptid
) {
664 /* Transport path 1 */
665 saa7164_irq_ts(porta
);
667 } else if (intid
== portb
->hwcfg
.interruptid
) {
669 /* Transport path 2 */
670 saa7164_irq_ts(portb
);
672 } else if (intid
== portc
->hwcfg
.interruptid
) {
675 saa7164_irq_encoder(portc
);
677 } else if (intid
== portd
->hwcfg
.interruptid
) {
680 saa7164_irq_encoder(portd
);
682 } else if (intid
== porte
->hwcfg
.interruptid
) {
685 saa7164_irq_vbi(porte
);
687 } else if (intid
== portf
->hwcfg
.interruptid
) {
690 saa7164_irq_vbi(portf
);
693 /* Find the function */
695 "%s() unhandled interrupt reg 0x%x bit 0x%x intid = 0x%x\n",
696 __func__
, i
, bit
, intid
);
701 saa7164_writel(dev
->int_ack
+ (i
* 4), intstat
[i
]);
706 return IRQ_RETVAL(handled
);
709 void saa7164_getfirmwarestatus(struct saa7164_dev
*dev
)
711 struct saa7164_fw_status
*s
= &dev
->fw_status
;
713 dev
->fw_status
.status
= saa7164_readl(SAA_DEVICE_SYSINIT_STATUS
);
714 dev
->fw_status
.mode
= saa7164_readl(SAA_DEVICE_SYSINIT_MODE
);
715 dev
->fw_status
.spec
= saa7164_readl(SAA_DEVICE_SYSINIT_SPEC
);
716 dev
->fw_status
.inst
= saa7164_readl(SAA_DEVICE_SYSINIT_INST
);
717 dev
->fw_status
.cpuload
= saa7164_readl(SAA_DEVICE_SYSINIT_CPULOAD
);
718 dev
->fw_status
.remainheap
=
719 saa7164_readl(SAA_DEVICE_SYSINIT_REMAINHEAP
);
721 dprintk(1, "Firmware status:\n");
722 dprintk(1, " .status = 0x%08x\n", s
->status
);
723 dprintk(1, " .mode = 0x%08x\n", s
->mode
);
724 dprintk(1, " .spec = 0x%08x\n", s
->spec
);
725 dprintk(1, " .inst = 0x%08x\n", s
->inst
);
726 dprintk(1, " .cpuload = 0x%08x\n", s
->cpuload
);
727 dprintk(1, " .remainheap = 0x%08x\n", s
->remainheap
);
730 u32
saa7164_getcurrentfirmwareversion(struct saa7164_dev
*dev
)
734 reg
= saa7164_readl(SAA_DEVICE_VERSION
);
735 dprintk(1, "Device running firmware version %d.%d.%d.%d (0x%x)\n",
736 (reg
& 0x0000fc00) >> 10,
737 (reg
& 0x000003e0) >> 5,
739 (reg
& 0xffff0000) >> 16,
745 /* TODO: Debugging func, remove */
746 void saa7164_dumpregs(struct saa7164_dev
*dev
, u32 addr
)
750 dprintk(1, "--------------------> 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
752 for (i
= 0; i
< 0x100; i
+= 16)
753 dprintk(1, "region0[0x%08x] = %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
755 (u8
)saa7164_readb(addr
+ i
+ 0),
756 (u8
)saa7164_readb(addr
+ i
+ 1),
757 (u8
)saa7164_readb(addr
+ i
+ 2),
758 (u8
)saa7164_readb(addr
+ i
+ 3),
759 (u8
)saa7164_readb(addr
+ i
+ 4),
760 (u8
)saa7164_readb(addr
+ i
+ 5),
761 (u8
)saa7164_readb(addr
+ i
+ 6),
762 (u8
)saa7164_readb(addr
+ i
+ 7),
763 (u8
)saa7164_readb(addr
+ i
+ 8),
764 (u8
)saa7164_readb(addr
+ i
+ 9),
765 (u8
)saa7164_readb(addr
+ i
+ 10),
766 (u8
)saa7164_readb(addr
+ i
+ 11),
767 (u8
)saa7164_readb(addr
+ i
+ 12),
768 (u8
)saa7164_readb(addr
+ i
+ 13),
769 (u8
)saa7164_readb(addr
+ i
+ 14),
770 (u8
)saa7164_readb(addr
+ i
+ 15)
774 static void saa7164_dump_hwdesc(struct saa7164_dev
*dev
)
776 dprintk(1, "@0x%p hwdesc sizeof(struct tmComResHWDescr) = %d bytes\n",
777 &dev
->hwdesc
, (u32
)sizeof(struct tmComResHWDescr
));
779 dprintk(1, " .bLength = 0x%x\n", dev
->hwdesc
.bLength
);
780 dprintk(1, " .bDescriptorType = 0x%x\n", dev
->hwdesc
.bDescriptorType
);
781 dprintk(1, " .bDescriptorSubtype = 0x%x\n",
782 dev
->hwdesc
.bDescriptorSubtype
);
784 dprintk(1, " .bcdSpecVersion = 0x%x\n", dev
->hwdesc
.bcdSpecVersion
);
785 dprintk(1, " .dwClockFrequency = 0x%x\n", dev
->hwdesc
.dwClockFrequency
);
786 dprintk(1, " .dwClockUpdateRes = 0x%x\n", dev
->hwdesc
.dwClockUpdateRes
);
787 dprintk(1, " .bCapabilities = 0x%x\n", dev
->hwdesc
.bCapabilities
);
788 dprintk(1, " .dwDeviceRegistersLocation = 0x%x\n",
789 dev
->hwdesc
.dwDeviceRegistersLocation
);
791 dprintk(1, " .dwHostMemoryRegion = 0x%x\n",
792 dev
->hwdesc
.dwHostMemoryRegion
);
794 dprintk(1, " .dwHostMemoryRegionSize = 0x%x\n",
795 dev
->hwdesc
.dwHostMemoryRegionSize
);
797 dprintk(1, " .dwHostHibernatMemRegion = 0x%x\n",
798 dev
->hwdesc
.dwHostHibernatMemRegion
);
800 dprintk(1, " .dwHostHibernatMemRegionSize = 0x%x\n",
801 dev
->hwdesc
.dwHostHibernatMemRegionSize
);
804 static void saa7164_dump_intfdesc(struct saa7164_dev
*dev
)
806 dprintk(1, "@0x%p intfdesc sizeof(struct tmComResInterfaceDescr) = %d bytes\n",
807 &dev
->intfdesc
, (u32
)sizeof(struct tmComResInterfaceDescr
));
809 dprintk(1, " .bLength = 0x%x\n", dev
->intfdesc
.bLength
);
810 dprintk(1, " .bDescriptorType = 0x%x\n", dev
->intfdesc
.bDescriptorType
);
811 dprintk(1, " .bDescriptorSubtype = 0x%x\n",
812 dev
->intfdesc
.bDescriptorSubtype
);
814 dprintk(1, " .bFlags = 0x%x\n", dev
->intfdesc
.bFlags
);
815 dprintk(1, " .bInterfaceType = 0x%x\n", dev
->intfdesc
.bInterfaceType
);
816 dprintk(1, " .bInterfaceId = 0x%x\n", dev
->intfdesc
.bInterfaceId
);
817 dprintk(1, " .bBaseInterface = 0x%x\n", dev
->intfdesc
.bBaseInterface
);
818 dprintk(1, " .bInterruptId = 0x%x\n", dev
->intfdesc
.bInterruptId
);
819 dprintk(1, " .bDebugInterruptId = 0x%x\n",
820 dev
->intfdesc
.bDebugInterruptId
);
822 dprintk(1, " .BARLocation = 0x%x\n", dev
->intfdesc
.BARLocation
);
825 static void saa7164_dump_busdesc(struct saa7164_dev
*dev
)
827 dprintk(1, "@0x%p busdesc sizeof(struct tmComResBusDescr) = %d bytes\n",
828 &dev
->busdesc
, (u32
)sizeof(struct tmComResBusDescr
));
830 dprintk(1, " .CommandRing = 0x%016Lx\n", dev
->busdesc
.CommandRing
);
831 dprintk(1, " .ResponseRing = 0x%016Lx\n", dev
->busdesc
.ResponseRing
);
832 dprintk(1, " .CommandWrite = 0x%x\n", dev
->busdesc
.CommandWrite
);
833 dprintk(1, " .CommandRead = 0x%x\n", dev
->busdesc
.CommandRead
);
834 dprintk(1, " .ResponseWrite = 0x%x\n", dev
->busdesc
.ResponseWrite
);
835 dprintk(1, " .ResponseRead = 0x%x\n", dev
->busdesc
.ResponseRead
);
838 /* Much of the hardware configuration and PCI registers are configured
839 * dynamically depending on firmware. We have to cache some initial
840 * structures then use these to locate other important structures
843 static void saa7164_get_descriptors(struct saa7164_dev
*dev
)
845 memcpy_fromio(&dev
->hwdesc
, dev
->bmmio
, sizeof(struct tmComResHWDescr
));
846 memcpy_fromio(&dev
->intfdesc
, dev
->bmmio
+ sizeof(struct tmComResHWDescr
),
847 sizeof(struct tmComResInterfaceDescr
));
848 memcpy_fromio(&dev
->busdesc
, dev
->bmmio
+ dev
->intfdesc
.BARLocation
,
849 sizeof(struct tmComResBusDescr
));
851 if (dev
->hwdesc
.bLength
!= sizeof(struct tmComResHWDescr
)) {
852 printk(KERN_ERR
"Structure struct tmComResHWDescr is mangled\n");
853 printk(KERN_ERR
"Need %x got %d\n", dev
->hwdesc
.bLength
,
854 (u32
)sizeof(struct tmComResHWDescr
));
856 saa7164_dump_hwdesc(dev
);
858 if (dev
->intfdesc
.bLength
!= sizeof(struct tmComResInterfaceDescr
)) {
859 printk(KERN_ERR
"struct struct tmComResInterfaceDescr is mangled\n");
860 printk(KERN_ERR
"Need %x got %d\n", dev
->intfdesc
.bLength
,
861 (u32
)sizeof(struct tmComResInterfaceDescr
));
863 saa7164_dump_intfdesc(dev
);
865 saa7164_dump_busdesc(dev
);
868 static int saa7164_pci_quirks(struct saa7164_dev
*dev
)
873 static int get_resources(struct saa7164_dev
*dev
)
875 if (request_mem_region(pci_resource_start(dev
->pci
, 0),
876 pci_resource_len(dev
->pci
, 0), dev
->name
)) {
878 if (request_mem_region(pci_resource_start(dev
->pci
, 2),
879 pci_resource_len(dev
->pci
, 2), dev
->name
))
883 printk(KERN_ERR
"%s: can't get MMIO memory @ 0x%llx or 0x%llx\n",
885 (u64
)pci_resource_start(dev
->pci
, 0),
886 (u64
)pci_resource_start(dev
->pci
, 2));
891 static int saa7164_port_init(struct saa7164_dev
*dev
, int portnr
)
893 struct saa7164_port
*port
= NULL
;
895 BUG_ON((portnr
< 0) || (portnr
>= SAA7164_MAX_PORTS
));
897 port
= &dev
->ports
[portnr
];
902 if ((portnr
== SAA7164_PORT_TS1
) || (portnr
== SAA7164_PORT_TS2
))
903 port
->type
= SAA7164_MPEG_DVB
;
905 if ((portnr
== SAA7164_PORT_ENC1
) || (portnr
== SAA7164_PORT_ENC2
)) {
906 port
->type
= SAA7164_MPEG_ENCODER
;
908 /* We need a deferred interrupt handler for cmd handling */
909 INIT_WORK(&port
->workenc
, saa7164_work_enchandler
);
910 } else if ((portnr
== SAA7164_PORT_VBI1
) || (portnr
== SAA7164_PORT_VBI2
)) {
911 port
->type
= SAA7164_MPEG_VBI
;
913 /* We need a deferred interrupt handler for cmd handling */
914 INIT_WORK(&port
->workenc
, saa7164_work_vbihandler
);
918 /* Init all the critical resources */
919 mutex_init(&port
->dvb
.lock
);
920 INIT_LIST_HEAD(&port
->dmaqueue
.list
);
921 mutex_init(&port
->dmaqueue_lock
);
923 INIT_LIST_HEAD(&port
->list_buf_used
.list
);
924 INIT_LIST_HEAD(&port
->list_buf_free
.list
);
925 init_waitqueue_head(&port
->wait_read
);
928 saa7164_histogram_reset(&port
->irq_interval
, "irq intervals");
929 saa7164_histogram_reset(&port
->svc_interval
, "deferred intervals");
930 saa7164_histogram_reset(&port
->irq_svc_interval
,
931 "irq to deferred intervals");
932 saa7164_histogram_reset(&port
->read_interval
,
933 "encoder/vbi read() intervals");
934 saa7164_histogram_reset(&port
->poll_interval
,
935 "encoder/vbi poll() intervals");
940 static int saa7164_dev_setup(struct saa7164_dev
*dev
)
944 mutex_init(&dev
->lock
);
945 atomic_inc(&dev
->refcount
);
946 dev
->nr
= saa7164_devcount
++;
948 snprintf(dev
->name
, sizeof(dev
->name
), "saa7164[%d]", dev
->nr
);
950 mutex_lock(&devlist
);
951 list_add_tail(&dev
->devlist
, &saa7164_devlist
);
952 mutex_unlock(&devlist
);
956 if (card
[dev
->nr
] < saa7164_bcount
)
957 dev
->board
= card
[dev
->nr
];
959 for (i
= 0; UNSET
== dev
->board
&& i
< saa7164_idcount
; i
++)
960 if (dev
->pci
->subsystem_vendor
== saa7164_subids
[i
].subvendor
&&
961 dev
->pci
->subsystem_device
==
962 saa7164_subids
[i
].subdevice
)
963 dev
->board
= saa7164_subids
[i
].card
;
965 if (UNSET
== dev
->board
) {
966 dev
->board
= SAA7164_BOARD_UNKNOWN
;
967 saa7164_card_list(dev
);
970 dev
->pci_bus
= dev
->pci
->bus
->number
;
971 dev
->pci_slot
= PCI_SLOT(dev
->pci
->devfn
);
973 /* I2C Defaults / setup */
974 dev
->i2c_bus
[0].dev
= dev
;
975 dev
->i2c_bus
[0].nr
= 0;
976 dev
->i2c_bus
[1].dev
= dev
;
977 dev
->i2c_bus
[1].nr
= 1;
978 dev
->i2c_bus
[2].dev
= dev
;
979 dev
->i2c_bus
[2].nr
= 2;
981 /* Transport + Encoder ports 1, 2, 3, 4 - Defaults / setup */
982 saa7164_port_init(dev
, SAA7164_PORT_TS1
);
983 saa7164_port_init(dev
, SAA7164_PORT_TS2
);
984 saa7164_port_init(dev
, SAA7164_PORT_ENC1
);
985 saa7164_port_init(dev
, SAA7164_PORT_ENC2
);
986 saa7164_port_init(dev
, SAA7164_PORT_VBI1
);
987 saa7164_port_init(dev
, SAA7164_PORT_VBI2
);
989 if (get_resources(dev
) < 0) {
990 printk(KERN_ERR
"CORE %s No more PCIe resources for subsystem: %04x:%04x\n",
991 dev
->name
, dev
->pci
->subsystem_vendor
,
992 dev
->pci
->subsystem_device
);
998 /* PCI/e allocations */
999 dev
->lmmio
= ioremap(pci_resource_start(dev
->pci
, 0),
1000 pci_resource_len(dev
->pci
, 0));
1002 dev
->lmmio2
= ioremap(pci_resource_start(dev
->pci
, 2),
1003 pci_resource_len(dev
->pci
, 2));
1005 dev
->bmmio
= (u8 __iomem
*)dev
->lmmio
;
1006 dev
->bmmio2
= (u8 __iomem
*)dev
->lmmio2
;
1008 /* Interrupt and ack register locations offset of bmmio */
1009 dev
->int_status
= 0x183000 + 0xf80;
1010 dev
->int_ack
= 0x183000 + 0xf90;
1013 "CORE %s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
1014 dev
->name
, dev
->pci
->subsystem_vendor
,
1015 dev
->pci
->subsystem_device
, saa7164_boards
[dev
->board
].name
,
1016 dev
->board
, card
[dev
->nr
] == dev
->board
?
1017 "insmod option" : "autodetected");
1019 saa7164_pci_quirks(dev
);
1024 static void saa7164_dev_unregister(struct saa7164_dev
*dev
)
1026 dprintk(1, "%s()\n", __func__
);
1028 release_mem_region(pci_resource_start(dev
->pci
, 0),
1029 pci_resource_len(dev
->pci
, 0));
1031 release_mem_region(pci_resource_start(dev
->pci
, 2),
1032 pci_resource_len(dev
->pci
, 2));
1034 if (!atomic_dec_and_test(&dev
->refcount
))
1037 iounmap(dev
->lmmio
);
1038 iounmap(dev
->lmmio2
);
1043 #ifdef CONFIG_DEBUG_FS
1044 static void *saa7164_seq_start(struct seq_file
*s
, loff_t
*pos
)
1046 struct saa7164_dev
*dev
;
1047 loff_t index
= *pos
;
1049 mutex_lock(&devlist
);
1050 list_for_each_entry(dev
, &saa7164_devlist
, devlist
) {
1052 mutex_unlock(&devlist
);
1056 mutex_unlock(&devlist
);
1061 static void *saa7164_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
1063 struct saa7164_dev
*dev
= v
;
1066 mutex_lock(&devlist
);
1067 if (list_is_last(&dev
->devlist
, &saa7164_devlist
))
1070 ret
= list_next_entry(dev
, devlist
);
1071 mutex_unlock(&devlist
);
1078 static void saa7164_seq_stop(struct seq_file
*s
, void *v
)
1082 static int saa7164_seq_show(struct seq_file
*m
, void *v
)
1084 struct saa7164_dev
*dev
= v
;
1085 struct tmComResBusInfo
*b
;
1088 seq_printf(m
, "%s = %p\n", dev
->name
, dev
);
1090 /* Lock the bus from any other access */
1092 mutex_lock(&b
->lock
);
1094 seq_printf(m
, " .m_pdwSetWritePos = 0x%x (0x%08x)\n",
1095 b
->m_dwSetReadPos
, saa7164_readl(b
->m_dwSetReadPos
));
1097 seq_printf(m
, " .m_pdwSetReadPos = 0x%x (0x%08x)\n",
1098 b
->m_dwSetWritePos
, saa7164_readl(b
->m_dwSetWritePos
));
1100 seq_printf(m
, " .m_pdwGetWritePos = 0x%x (0x%08x)\n",
1101 b
->m_dwGetReadPos
, saa7164_readl(b
->m_dwGetReadPos
));
1103 seq_printf(m
, " .m_pdwGetReadPos = 0x%x (0x%08x)\n",
1104 b
->m_dwGetWritePos
, saa7164_readl(b
->m_dwGetWritePos
));
1106 seq_puts(m
, "\n Set Ring:\n");
1107 seq_puts(m
, "\n addr 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
1108 for (i
= 0; i
< b
->m_dwSizeSetRing
; i
++) {
1110 seq_printf(m
, " %04x:", i
);
1112 seq_printf(m
, " %02x", readb(b
->m_pdwSetRing
+ i
));
1121 seq_puts(m
, "\n Get Ring:\n");
1122 seq_puts(m
, "\n addr 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
1123 for (i
= 0; i
< b
->m_dwSizeGetRing
; i
++) {
1125 seq_printf(m
, " %04x:", i
);
1127 seq_printf(m
, " %02x", readb(b
->m_pdwGetRing
+ i
));
1135 mutex_unlock(&b
->lock
);
1140 static const struct seq_operations saa7164_sops
= {
1141 .start
= saa7164_seq_start
,
1142 .next
= saa7164_seq_next
,
1143 .stop
= saa7164_seq_stop
,
1144 .show
= saa7164_seq_show
,
1147 DEFINE_SEQ_ATTRIBUTE(saa7164
);
1149 static struct dentry
*saa7614_dentry
;
1151 static void __init
saa7164_debugfs_create(void)
1153 saa7614_dentry
= debugfs_create_file("saa7164", 0444, NULL
, NULL
,
1157 static void __exit
saa7164_debugfs_remove(void)
1159 debugfs_remove(saa7614_dentry
);
1162 static void saa7164_debugfs_create(void) { }
1163 static void saa7164_debugfs_remove(void) { }
1166 static int saa7164_thread_function(void *data
)
1168 struct saa7164_dev
*dev
= data
;
1169 struct tmFwInfoStruct fwinfo
;
1170 u64 last_poll_time
= 0;
1172 dprintk(DBGLVL_THR
, "thread started\n");
1177 msleep_interruptible(100);
1178 if (kthread_should_stop())
1182 dprintk(DBGLVL_THR
, "thread running\n");
1184 /* Dump the firmware debug message to console */
1185 /* Polling this costs us 1-2% of the arm CPU */
1186 /* convert this into a respnde to interrupt 0x7a */
1187 saa7164_api_collect_debug(dev
);
1189 /* Monitor CPU load every 1 second */
1190 if ((last_poll_time
+ 1000 /* ms */) < jiffies_to_msecs(jiffies
)) {
1191 saa7164_api_get_load_info(dev
, &fwinfo
);
1192 last_poll_time
= jiffies_to_msecs(jiffies
);
1197 dprintk(DBGLVL_THR
, "thread exiting\n");
1201 static bool saa7164_enable_msi(struct pci_dev
*pci_dev
, struct saa7164_dev
*dev
)
1206 printk(KERN_WARNING
"%s() MSI disabled by module parameter 'enable_msi'"
1211 err
= pci_enable_msi(pci_dev
);
1214 printk(KERN_ERR
"%s() Failed to enable MSI interrupt. Falling back to a shared IRQ\n",
1219 /* no error - so request an msi interrupt */
1220 err
= request_irq(pci_dev
->irq
, saa7164_irq
, 0,
1224 /* fall back to legacy interrupt */
1225 printk(KERN_ERR
"%s() Failed to get an MSI interrupt. Falling back to a shared IRQ\n",
1227 pci_disable_msi(pci_dev
);
1234 static int saa7164_initdev(struct pci_dev
*pci_dev
,
1235 const struct pci_device_id
*pci_id
)
1237 struct saa7164_dev
*dev
;
1241 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1245 err
= v4l2_device_register(&pci_dev
->dev
, &dev
->v4l2_dev
);
1247 dev_err(&pci_dev
->dev
, "v4l2_device_register failed\n");
1253 if (pci_enable_device(pci_dev
)) {
1258 if (saa7164_dev_setup(dev
) < 0) {
1263 /* print pci info */
1264 dev
->pci_rev
= pci_dev
->revision
;
1265 pci_read_config_byte(pci_dev
, PCI_LATENCY_TIMER
, &dev
->pci_lat
);
1266 printk(KERN_INFO
"%s/0: found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n",
1268 pci_name(pci_dev
), dev
->pci_rev
, pci_dev
->irq
,
1270 (unsigned long long)pci_resource_start(pci_dev
, 0));
1272 pci_set_master(pci_dev
);
1274 err
= dma_set_mask(&pci_dev
->dev
, 0xffffffff);
1276 printk("%s/0: Oops: no 32bit PCI DMA ???\n", dev
->name
);
1281 if (saa7164_enable_msi(pci_dev
, dev
)) {
1284 /* if we have an error (i.e. we don't have an interrupt)
1285 or msi is not enabled - fallback to shared interrupt */
1287 err
= request_irq(pci_dev
->irq
, saa7164_irq
,
1288 IRQF_SHARED
, dev
->name
, dev
);
1291 printk(KERN_ERR
"%s: can't get IRQ %d\n", dev
->name
,
1298 pci_set_drvdata(pci_dev
, dev
);
1300 /* Init the internal command list */
1301 for (i
= 0; i
< SAA_CMD_MAX_MSG_UNITS
; i
++) {
1302 dev
->cmds
[i
].seqno
= i
;
1303 dev
->cmds
[i
].inuse
= 0;
1304 mutex_init(&dev
->cmds
[i
].lock
);
1305 init_waitqueue_head(&dev
->cmds
[i
].wait
);
1308 /* We need a deferred interrupt handler for cmd handling */
1309 INIT_WORK(&dev
->workcmd
, saa7164_work_cmdhandler
);
1311 /* Only load the firmware if we know the board */
1312 if (dev
->board
!= SAA7164_BOARD_UNKNOWN
) {
1314 err
= saa7164_downloadfirmware(dev
);
1317 "Failed to boot firmware, no features registered\n");
1321 saa7164_get_descriptors(dev
);
1322 saa7164_dumpregs(dev
, 0);
1323 saa7164_getcurrentfirmwareversion(dev
);
1324 saa7164_getfirmwarestatus(dev
);
1325 err
= saa7164_bus_setup(dev
);
1328 "Failed to setup the bus, will continue\n");
1329 saa7164_bus_dump(dev
);
1331 /* Ping the running firmware via the command bus and get the
1332 * firmware version, this checks the bus is running OK.
1335 if (saa7164_api_get_fw_version(dev
, &version
) == SAA_OK
)
1336 dprintk(1, "Bus is operating correctly using version %d.%d.%d.%d (0x%x)\n",
1337 (version
& 0x0000fc00) >> 10,
1338 (version
& 0x000003e0) >> 5,
1339 (version
& 0x0000001f),
1340 (version
& 0xffff0000) >> 16,
1344 "Failed to communicate with the firmware\n");
1346 /* Bring up the I2C buses */
1347 saa7164_i2c_register(&dev
->i2c_bus
[0]);
1348 saa7164_i2c_register(&dev
->i2c_bus
[1]);
1349 saa7164_i2c_register(&dev
->i2c_bus
[2]);
1350 saa7164_gpio_setup(dev
);
1351 saa7164_card_setup(dev
);
1353 /* Parse the dynamic device configuration, find various
1354 * media endpoints (MPEG, WMV, PS, TS) and cache their
1355 * configuration details into the driver, so we can
1356 * reference them later during simething_register() func,
1357 * interrupt handlers, deferred work handlers etc.
1359 saa7164_api_enum_subdevs(dev
);
1361 /* Begin to create the video sub-systems and register funcs */
1362 if (saa7164_boards
[dev
->board
].porta
== SAA7164_MPEG_DVB
) {
1363 if (saa7164_dvb_register(&dev
->ports
[SAA7164_PORT_TS1
]) < 0) {
1364 printk(KERN_ERR
"%s() Failed to register dvb adapters on porta\n",
1369 if (saa7164_boards
[dev
->board
].portb
== SAA7164_MPEG_DVB
) {
1370 if (saa7164_dvb_register(&dev
->ports
[SAA7164_PORT_TS2
]) < 0) {
1371 printk(KERN_ERR
"%s() Failed to register dvb adapters on portb\n",
1376 if (saa7164_boards
[dev
->board
].portc
== SAA7164_MPEG_ENCODER
) {
1377 if (saa7164_encoder_register(&dev
->ports
[SAA7164_PORT_ENC1
]) < 0) {
1378 printk(KERN_ERR
"%s() Failed to register mpeg encoder\n",
1383 if (saa7164_boards
[dev
->board
].portd
== SAA7164_MPEG_ENCODER
) {
1384 if (saa7164_encoder_register(&dev
->ports
[SAA7164_PORT_ENC2
]) < 0) {
1385 printk(KERN_ERR
"%s() Failed to register mpeg encoder\n",
1390 if (saa7164_boards
[dev
->board
].porte
== SAA7164_MPEG_VBI
) {
1391 if (saa7164_vbi_register(&dev
->ports
[SAA7164_PORT_VBI1
]) < 0) {
1392 printk(KERN_ERR
"%s() Failed to register vbi device\n",
1397 if (saa7164_boards
[dev
->board
].portf
== SAA7164_MPEG_VBI
) {
1398 if (saa7164_vbi_register(&dev
->ports
[SAA7164_PORT_VBI2
]) < 0) {
1399 printk(KERN_ERR
"%s() Failed to register vbi device\n",
1403 saa7164_api_set_debug(dev
, fw_debug
);
1406 dev
->kthread
= kthread_run(saa7164_thread_function
, dev
,
1408 if (IS_ERR(dev
->kthread
)) {
1409 dev
->kthread
= NULL
;
1410 printk(KERN_ERR
"%s() Failed to create debug kernel thread\n",
1415 } /* != BOARD_UNKNOWN */
1417 printk(KERN_ERR
"%s() Unsupported board detected, registering without firmware\n",
1420 dprintk(1, "%s() parameter debug = %d\n", __func__
, saa_debug
);
1421 dprintk(1, "%s() parameter waitsecs = %d\n", __func__
, waitsecs
);
1427 saa7164_dev_unregister(dev
);
1429 pci_disable_device(pci_dev
);
1431 v4l2_device_unregister(&dev
->v4l2_dev
);
1436 static void saa7164_shutdown(struct saa7164_dev
*dev
)
1438 dprintk(1, "%s()\n", __func__
);
1441 static void saa7164_finidev(struct pci_dev
*pci_dev
)
1443 struct saa7164_dev
*dev
= pci_get_drvdata(pci_dev
);
1445 if (dev
->board
!= SAA7164_BOARD_UNKNOWN
) {
1446 if (fw_debug
&& dev
->kthread
) {
1447 kthread_stop(dev
->kthread
);
1448 dev
->kthread
= NULL
;
1450 if (dev
->firmwareloaded
)
1451 saa7164_api_set_debug(dev
, 0x00);
1454 saa7164_histogram_print(&dev
->ports
[SAA7164_PORT_ENC1
],
1455 &dev
->ports
[SAA7164_PORT_ENC1
].irq_interval
);
1456 saa7164_histogram_print(&dev
->ports
[SAA7164_PORT_ENC1
],
1457 &dev
->ports
[SAA7164_PORT_ENC1
].svc_interval
);
1458 saa7164_histogram_print(&dev
->ports
[SAA7164_PORT_ENC1
],
1459 &dev
->ports
[SAA7164_PORT_ENC1
].irq_svc_interval
);
1460 saa7164_histogram_print(&dev
->ports
[SAA7164_PORT_ENC1
],
1461 &dev
->ports
[SAA7164_PORT_ENC1
].read_interval
);
1462 saa7164_histogram_print(&dev
->ports
[SAA7164_PORT_ENC1
],
1463 &dev
->ports
[SAA7164_PORT_ENC1
].poll_interval
);
1464 saa7164_histogram_print(&dev
->ports
[SAA7164_PORT_VBI1
],
1465 &dev
->ports
[SAA7164_PORT_VBI1
].read_interval
);
1466 saa7164_histogram_print(&dev
->ports
[SAA7164_PORT_VBI2
],
1467 &dev
->ports
[SAA7164_PORT_VBI2
].poll_interval
);
1469 saa7164_shutdown(dev
);
1471 if (saa7164_boards
[dev
->board
].porta
== SAA7164_MPEG_DVB
)
1472 saa7164_dvb_unregister(&dev
->ports
[SAA7164_PORT_TS1
]);
1474 if (saa7164_boards
[dev
->board
].portb
== SAA7164_MPEG_DVB
)
1475 saa7164_dvb_unregister(&dev
->ports
[SAA7164_PORT_TS2
]);
1477 if (saa7164_boards
[dev
->board
].portc
== SAA7164_MPEG_ENCODER
)
1478 saa7164_encoder_unregister(&dev
->ports
[SAA7164_PORT_ENC1
]);
1480 if (saa7164_boards
[dev
->board
].portd
== SAA7164_MPEG_ENCODER
)
1481 saa7164_encoder_unregister(&dev
->ports
[SAA7164_PORT_ENC2
]);
1483 if (saa7164_boards
[dev
->board
].porte
== SAA7164_MPEG_VBI
)
1484 saa7164_vbi_unregister(&dev
->ports
[SAA7164_PORT_VBI1
]);
1486 if (saa7164_boards
[dev
->board
].portf
== SAA7164_MPEG_VBI
)
1487 saa7164_vbi_unregister(&dev
->ports
[SAA7164_PORT_VBI2
]);
1489 saa7164_i2c_unregister(&dev
->i2c_bus
[0]);
1490 saa7164_i2c_unregister(&dev
->i2c_bus
[1]);
1491 saa7164_i2c_unregister(&dev
->i2c_bus
[2]);
1493 /* unregister stuff */
1494 free_irq(pci_dev
->irq
, dev
);
1497 pci_disable_msi(pci_dev
);
1501 pci_disable_device(pci_dev
);
1503 mutex_lock(&devlist
);
1504 list_del(&dev
->devlist
);
1505 mutex_unlock(&devlist
);
1507 saa7164_dev_unregister(dev
);
1508 v4l2_device_unregister(&dev
->v4l2_dev
);
1512 static const struct pci_device_id saa7164_pci_tbl
[] = {
1517 .subvendor
= PCI_ANY_ID
,
1518 .subdevice
= PCI_ANY_ID
,
1520 /* --- end of list --- */
1523 MODULE_DEVICE_TABLE(pci
, saa7164_pci_tbl
);
1525 static struct pci_driver saa7164_pci_driver
= {
1527 .id_table
= saa7164_pci_tbl
,
1528 .probe
= saa7164_initdev
,
1529 .remove
= saa7164_finidev
,
1532 static int __init
saa7164_init(void)
1534 int ret
= pci_register_driver(&saa7164_pci_driver
);
1539 saa7164_debugfs_create();
1541 pr_info("saa7164 driver loaded\n");
1546 static void __exit
saa7164_fini(void)
1548 saa7164_debugfs_remove();
1549 pci_unregister_driver(&saa7164_pci_driver
);
1552 module_init(saa7164_init
);
1553 module_exit(saa7164_fini
);