2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/kmod.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/delay.h>
31 #include <asm/div64.h>
35 MODULE_DESCRIPTION("Driver for NXP SAA7164 based TV cards");
36 MODULE_AUTHOR("Steven Toth <stoth@kernellabs.com>");
37 MODULE_LICENSE("GPL");
48 unsigned int saa_debug
;
49 module_param_named(debug
, saa_debug
, int, 0644);
50 MODULE_PARM_DESC(debug
, "enable debug messages");
52 unsigned int encoder_buffers
= SAA7164_MAX_ENCODER_BUFFERS
;
53 module_param(encoder_buffers
, int, 0644);
54 MODULE_PARM_DESC(encoder_buffers
, "Total buffers in read queue 16-512 def:64");
56 unsigned int waitsecs
= 10;
57 module_param(waitsecs
, int, 0644);
58 MODULE_PARM_DESC(waitsecs
, "timeout on firmware messages");
60 static unsigned int card
[] = {[0 ... (SAA7164_MAXBOARDS
- 1)] = UNSET
};
61 module_param_array(card
, int, NULL
, 0444);
62 MODULE_PARM_DESC(card
, "card type");
64 unsigned int print_histogram
= 64;
65 module_param(print_histogram
, int, 0644);
66 MODULE_PARM_DESC(print_histogram
, "print histogram values once");
68 static unsigned int saa7164_devcount
;
70 static DEFINE_MUTEX(devlist
);
71 LIST_HEAD(saa7164_devlist
);
75 static void saa7164_ts_verifier(struct saa7164_buffer
*buf
)
77 struct saa7164_port
*port
= buf
->port
;
78 struct saa7164_dev
*dev
= port
->dev
;
81 u8
*bufcpu
= (u8
*)buf
->cpu
;
83 port
->sync_errors
= 0;
84 port
->v_cc_errors
= 0;
85 port
->a_cc_errors
= 0;
87 for (i
= 0; i
< buf
->actual_size
; i
+= 188) {
88 if (*(bufcpu
+ i
) != 0x47)
91 /* Query pid lower 8 bits */
92 tmp
= *(bufcpu
+ i
+ 2);
93 cc
= *(bufcpu
+ i
+ 3) & 0x0f;
96 a
= ((port
->last_v_cc
+ 1) & 0x0f);
98 printk(KERN_ERR
"video cc last = %x current = %x i = %d\n", port
->last_v_cc
, cc
, i
);
102 port
->last_v_cc
= cc
;
105 a
= ((port
->last_a_cc
+ 1) & 0x0f);
107 printk(KERN_ERR
"audio cc last = %x current = %x i = %d\n", port
->last_a_cc
, cc
, i
);
111 port
->last_a_cc
= cc
;
116 if (port
->v_cc_errors
)
117 printk(KERN_ERR
"video pid cc, %d errors\n", port
->v_cc_errors
);
119 if (port
->a_cc_errors
)
120 printk(KERN_ERR
"audio pid cc, %d errors\n", port
->a_cc_errors
);
122 if (port
->sync_errors
)
123 printk(KERN_ERR
"sync_errors = %d\n", port
->sync_errors
);
126 static void saa7164_histogram_reset(struct saa7164_histogram
*hg
, char *name
)
130 memset(hg
, 0, sizeof(struct saa7164_histogram
));
131 strcpy(hg
->name
, name
);
133 /* First 30ms x 1ms */
134 for (i
= 0; i
< 30; i
++) {
135 hg
->counter1
[0 + i
].val
= i
;
138 /* 30 - 200ms x 10ms */
139 for (i
= 0; i
< 18; i
++) {
140 hg
->counter1
[30 + i
].val
= 30 + (i
* 10);
143 /* 200 - 2000ms x 100ms */
144 for (i
= 0; i
< 15; i
++) {
145 hg
->counter1
[48 + i
].val
= 200 + (i
* 100);
148 /* Catch all massive value (1hr) */
149 hg
->counter1
[63].val
= 3600000;
152 static void saa7164_histogram_update(struct saa7164_histogram
*hg
, u32 val
)
155 for (i
= 0; i
< 64; i
++ ) {
156 if (val
<= hg
->counter1
[i
].val
) {
157 hg
->counter1
[i
].count
++;
158 hg
->counter1
[i
].update_time
= jiffies
;
164 static void saa7164_histogram_print(struct saa7164_port
*port
,
165 struct saa7164_histogram
*hg
)
167 struct saa7164_dev
*dev
= port
->dev
;
171 printk(KERN_ERR
"Histogram named %s\n", hg
->name
);
172 for (i
= 0; i
< 64; i
++ ) {
173 if (hg
->counter1
[i
].count
== 0)
176 printk(KERN_ERR
" %4d %12d %Ld\n",
178 hg
->counter1
[i
].count
,
179 hg
->counter1
[i
].update_time
);
183 printk(KERN_ERR
"Total: %d\n", entries
);
186 static void saa7164_work_enchandler(struct work_struct
*w
)
188 struct saa7164_port
*port
=
189 container_of(w
, struct saa7164_port
, workenc
);
190 struct saa7164_dev
*dev
= port
->dev
;
191 struct saa7164_buffer
*buf
;
192 struct saa7164_user_buffer
*ubuf
;
193 struct list_head
*c
, *n
;
196 port
->last_svc_msecs_diff
= port
->last_svc_msecs
;
197 port
->last_svc_msecs
= jiffies_to_msecs(jiffies
);
198 port
->last_svc_wp
= saa7164_readl(port
->bufcounter
);
199 port
->last_svc_rp
= port
->last_irq_rp
;
200 wp
= port
->last_svc_wp
;
201 rp
= port
->last_svc_rp
;
204 port
->last_svc_msecs_diff
= port
->last_svc_msecs
-
205 port
->last_svc_msecs_diff
;
207 saa7164_histogram_update(&port
->svc_interval
,
208 port
->last_svc_msecs_diff
);
210 port
->last_irq_svc_msecs_diff
= port
->last_svc_msecs
-
211 port
->last_irq_msecs
;
213 saa7164_histogram_update(&port
->irq_svc_interval
,
214 port
->last_irq_svc_msecs_diff
);
217 "%s() %Ldms elapsed irq->deferred %Ldms wp: %d rp: %d\n",
219 port
->last_svc_msecs_diff
,
220 port
->last_irq_svc_msecs_diff
,
225 if ((rp
< 0) || (rp
> 7)) {
226 printk(KERN_ERR
"%s() illegal rp count %d\n", __func__
, rp
);
230 mutex_lock(&port
->dmaqueue_lock
);
232 list_for_each_safe(c
, n
, &port
->dmaqueue
.list
) {
234 buf
= list_entry(c
, struct saa7164_buffer
, list
);
235 if (i
++ > port
->hwcfg
.buffercount
) {
236 printk(KERN_ERR
"%s() illegal i count %d\n",
241 if (buf
->idx
== rp
) {
242 /* Found the buffer, deal with it */
243 dprintk(DBGLVL_IRQ
, "%s() wp: %d processing: %d\n",
246 /* Validate the incoming buffer content */
247 if (port
->encoder_params
.stream_type
== V4L2_MPEG_STREAM_TYPE_MPEG2_TS
)
248 saa7164_ts_verifier(buf
);
250 /* find a free user buffer and clone to it */
251 if (!list_empty(&port
->list_buf_free
.list
)) {
253 /* Pull the first buffer from the used list */
254 ubuf
= list_first_entry(&port
->list_buf_free
.list
,
255 struct saa7164_user_buffer
, list
);
257 if (ubuf
->actual_size
== buf
->actual_size
)
258 memcpy(ubuf
->data
, buf
->cpu
,
261 /* Requeue the buffer on the free list */
264 list_move_tail(&ubuf
->list
,
265 &port
->list_buf_used
.list
);
267 /* Flag any userland waiters */
268 wake_up_interruptible(&port
->wait_read
);
271 printk(KERN_ERR
"encirq no free buffers, increase param encoder_buffers\n");
273 /* Ensure offset into buffer remains 0, fill buffer
274 * with known bad data. */
275 saa7164_buffer_zero_offsets(port
, rp
);
276 memset(buf
->cpu
, 0xDE, buf
->pci_size
);
282 mutex_unlock(&port
->dmaqueue_lock
);
284 if (print_histogram
== port
->nr
) {
285 saa7164_histogram_print(port
, &port
->irq_interval
);
286 saa7164_histogram_print(port
, &port
->svc_interval
);
287 saa7164_histogram_print(port
, &port
->irq_svc_interval
);
288 print_histogram
= 64 + port
->nr
;
292 static void saa7164_work_cmdhandler(struct work_struct
*w
)
294 struct saa7164_dev
*dev
= container_of(w
, struct saa7164_dev
, workcmd
);
296 /* Wake up any complete commands */
297 saa7164_irq_dequeue(dev
);
300 static void saa7164_buffer_deliver(struct saa7164_buffer
*buf
)
302 struct saa7164_port
*port
= buf
->port
;
304 /* Feed the transport payload into the kernel demux */
305 dvb_dmx_swfilter_packets(&port
->dvb
.demux
, (u8
*)buf
->cpu
,
306 SAA7164_TS_NUMBER_OF_LINES
);
310 static irqreturn_t
saa7164_irq_encoder(struct saa7164_port
*port
)
312 struct saa7164_dev
*dev
= port
->dev
;
315 /* Find the current write point from the hardware */
316 wp
= saa7164_readl(port
->bufcounter
);
317 if (wp
> (port
->hwcfg
.buffercount
- 1)) {
318 printk(KERN_ERR
"%s() illegal buf count %d\n", __func__
, wp
);
322 /* Find the previous buffer to the current write point */
328 if ((rp
< 0) || (rp
> 7)) {
329 printk(KERN_ERR
"%s() illegal rp count %d\n", __func__
, rp
);
333 if (rp
!= ((port
->last_irq_rp
+ 1) % 8)) {
334 printk(KERN_ERR
"%s() Multiple bufs on interrupt, port %p\n",
339 port
->last_irq_msecs_diff
= port
->last_irq_msecs
;
341 /* Collect new stats */
342 port
->last_irq_msecs
= jiffies_to_msecs(jiffies
);
343 port
->last_irq_wp
= wp
;
344 port
->last_irq_rp
= rp
;
346 /* Calculate stats */
347 port
->last_irq_msecs_diff
= port
->last_irq_msecs
-
348 port
->last_irq_msecs_diff
;
350 saa7164_histogram_update(&port
->irq_interval
,
351 port
->last_irq_msecs_diff
);
353 dprintk(DBGLVL_IRQ
, "%s() %Ldms elapsed wp: %d rp: %d\n",
355 port
->last_irq_msecs_diff
,
360 schedule_work(&port
->workenc
);
365 static irqreturn_t
saa7164_irq_ts(struct saa7164_port
*port
)
367 struct saa7164_dev
*dev
= port
->dev
;
368 struct saa7164_buffer
*buf
;
369 struct list_head
*c
, *n
;
372 /* Find the current write point from the hardware */
373 wp
= saa7164_readl(port
->bufcounter
);
374 if (wp
> (port
->hwcfg
.buffercount
- 1))
377 /* Find the previous buffer to the current write point */
383 /* Lookup the WP in the buffer list */
384 /* TODO: turn this into a worker thread */
385 list_for_each_safe(c
, n
, &port
->dmaqueue
.list
) {
386 buf
= list_entry(c
, struct saa7164_buffer
, list
);
387 if (i
++ > port
->hwcfg
.buffercount
)
390 if (buf
->idx
== rp
) {
391 /* Found the buffer, deal with it */
392 dprintk(DBGLVL_IRQ
, "%s() wp: %d processing: %d\n",
394 saa7164_buffer_deliver(buf
);
402 /* Primary IRQ handler and dispatch mechanism */
403 static irqreturn_t
saa7164_irq(int irq
, void *dev_id
)
405 struct saa7164_dev
*dev
= dev_id
;
406 struct saa7164_port
*porta
= &dev
->ports
[ SAA7164_PORT_TS1
];
407 struct saa7164_port
*portb
= &dev
->ports
[ SAA7164_PORT_TS2
];
408 struct saa7164_port
*portc
= &dev
->ports
[ SAA7164_PORT_ENC1
];
409 struct saa7164_port
*portd
= &dev
->ports
[ SAA7164_PORT_ENC2
];
411 u32 intid
, intstat
[INT_SIZE
/4];
412 int i
, handled
= 0, bit
;
415 printk(KERN_ERR
"%s() No device specified\n", __func__
);
420 /* Check that the hardware is accessable. If the status bytes are
421 * 0xFF then the device is not accessable, the the IRQ belongs
423 * 4 x u32 interrupt registers.
425 for (i
= 0; i
< INT_SIZE
/4; i
++) {
427 /* TODO: Convert into saa7164_readl() */
428 /* Read the 4 hardware interrupt registers */
429 intstat
[i
] = saa7164_readl(dev
->int_status
+ (i
* 4));
437 /* For each of the HW interrupt registers */
438 for (i
= 0; i
< INT_SIZE
/4; i
++) {
441 /* Each function of the board has it's own interruptid.
442 * Find the function that triggered then call
445 for (bit
= 0; bit
< 32; bit
++) {
447 if (((intstat
[i
] >> bit
) & 0x00000001) == 0)
450 /* Calculate the interrupt id (0x00 to 0x7f) */
452 intid
= (i
* 32) + bit
;
453 if (intid
== dev
->intfdesc
.bInterruptId
) {
454 /* A response to an cmd/api call */
455 schedule_work(&dev
->workcmd
);
456 } else if (intid
== porta
->hwcfg
.interruptid
) {
458 /* Transport path 1 */
459 saa7164_irq_ts(porta
);
461 } else if (intid
== portb
->hwcfg
.interruptid
) {
463 /* Transport path 2 */
464 saa7164_irq_ts(portb
);
466 } else if (intid
== portc
->hwcfg
.interruptid
) {
469 saa7164_irq_encoder(portc
);
471 } else if (intid
== portd
->hwcfg
.interruptid
) {
474 saa7164_irq_encoder(portd
);
477 /* Find the function */
479 "%s() unhandled interrupt "
482 __func__
, i
, bit
, intid
);
487 saa7164_writel(dev
->int_ack
+ (i
* 4), intstat
[i
]);
492 return IRQ_RETVAL(handled
);
495 void saa7164_getfirmwarestatus(struct saa7164_dev
*dev
)
497 struct saa7164_fw_status
*s
= &dev
->fw_status
;
499 dev
->fw_status
.status
= saa7164_readl(SAA_DEVICE_SYSINIT_STATUS
);
500 dev
->fw_status
.mode
= saa7164_readl(SAA_DEVICE_SYSINIT_MODE
);
501 dev
->fw_status
.spec
= saa7164_readl(SAA_DEVICE_SYSINIT_SPEC
);
502 dev
->fw_status
.inst
= saa7164_readl(SAA_DEVICE_SYSINIT_INST
);
503 dev
->fw_status
.cpuload
= saa7164_readl(SAA_DEVICE_SYSINIT_CPULOAD
);
504 dev
->fw_status
.remainheap
=
505 saa7164_readl(SAA_DEVICE_SYSINIT_REMAINHEAP
);
507 dprintk(1, "Firmware status:\n");
508 dprintk(1, " .status = 0x%08x\n", s
->status
);
509 dprintk(1, " .mode = 0x%08x\n", s
->mode
);
510 dprintk(1, " .spec = 0x%08x\n", s
->spec
);
511 dprintk(1, " .inst = 0x%08x\n", s
->inst
);
512 dprintk(1, " .cpuload = 0x%08x\n", s
->cpuload
);
513 dprintk(1, " .remainheap = 0x%08x\n", s
->remainheap
);
516 u32
saa7164_getcurrentfirmwareversion(struct saa7164_dev
*dev
)
520 reg
= saa7164_readl(SAA_DEVICE_VERSION
);
521 dprintk(1, "Device running firmware version %d.%d.%d.%d (0x%x)\n",
522 (reg
& 0x0000fc00) >> 10,
523 (reg
& 0x000003e0) >> 5,
525 (reg
& 0xffff0000) >> 16,
531 /* TODO: Debugging func, remove */
532 void saa7164_dumphex16(struct saa7164_dev
*dev
, u8
*buf
, int len
)
536 printk(KERN_INFO
"--------------------> "
537 "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
539 for (i
= 0; i
< len
; i
+= 16)
540 printk(KERN_INFO
" [0x%08x] "
541 "%02x %02x %02x %02x %02x %02x %02x %02x "
542 "%02x %02x %02x %02x %02x %02x %02x %02x\n", i
,
543 *(buf
+i
+0), *(buf
+i
+1), *(buf
+i
+2), *(buf
+i
+3),
544 *(buf
+i
+4), *(buf
+i
+5), *(buf
+i
+6), *(buf
+i
+7),
545 *(buf
+i
+8), *(buf
+i
+9), *(buf
+i
+10), *(buf
+i
+11),
546 *(buf
+i
+12), *(buf
+i
+13), *(buf
+i
+14), *(buf
+i
+15));
549 /* TODO: Debugging func, remove */
550 void saa7164_dumpregs(struct saa7164_dev
*dev
, u32 addr
)
554 dprintk(1, "--------------------> "
555 "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
557 for (i
= 0; i
< 0x100; i
+= 16)
558 dprintk(1, "region0[0x%08x] = "
559 "%02x %02x %02x %02x %02x %02x %02x %02x"
560 " %02x %02x %02x %02x %02x %02x %02x %02x\n", i
,
561 (u8
)saa7164_readb(addr
+ i
+ 0),
562 (u8
)saa7164_readb(addr
+ i
+ 1),
563 (u8
)saa7164_readb(addr
+ i
+ 2),
564 (u8
)saa7164_readb(addr
+ i
+ 3),
565 (u8
)saa7164_readb(addr
+ i
+ 4),
566 (u8
)saa7164_readb(addr
+ i
+ 5),
567 (u8
)saa7164_readb(addr
+ i
+ 6),
568 (u8
)saa7164_readb(addr
+ i
+ 7),
569 (u8
)saa7164_readb(addr
+ i
+ 8),
570 (u8
)saa7164_readb(addr
+ i
+ 9),
571 (u8
)saa7164_readb(addr
+ i
+ 10),
572 (u8
)saa7164_readb(addr
+ i
+ 11),
573 (u8
)saa7164_readb(addr
+ i
+ 12),
574 (u8
)saa7164_readb(addr
+ i
+ 13),
575 (u8
)saa7164_readb(addr
+ i
+ 14),
576 (u8
)saa7164_readb(addr
+ i
+ 15)
580 static void saa7164_dump_hwdesc(struct saa7164_dev
*dev
)
582 dprintk(1, "@0x%p hwdesc sizeof(tmComResHWDescr_t) = %d bytes\n",
583 &dev
->hwdesc
, (u32
)sizeof(tmComResHWDescr_t
));
585 dprintk(1, " .bLength = 0x%x\n", dev
->hwdesc
.bLength
);
586 dprintk(1, " .bDescriptorType = 0x%x\n", dev
->hwdesc
.bDescriptorType
);
587 dprintk(1, " .bDescriptorSubtype = 0x%x\n",
588 dev
->hwdesc
.bDescriptorSubtype
);
590 dprintk(1, " .bcdSpecVersion = 0x%x\n", dev
->hwdesc
.bcdSpecVersion
);
591 dprintk(1, " .dwClockFrequency = 0x%x\n", dev
->hwdesc
.dwClockFrequency
);
592 dprintk(1, " .dwClockUpdateRes = 0x%x\n", dev
->hwdesc
.dwClockUpdateRes
);
593 dprintk(1, " .bCapabilities = 0x%x\n", dev
->hwdesc
.bCapabilities
);
594 dprintk(1, " .dwDeviceRegistersLocation = 0x%x\n",
595 dev
->hwdesc
.dwDeviceRegistersLocation
);
597 dprintk(1, " .dwHostMemoryRegion = 0x%x\n",
598 dev
->hwdesc
.dwHostMemoryRegion
);
600 dprintk(1, " .dwHostMemoryRegionSize = 0x%x\n",
601 dev
->hwdesc
.dwHostMemoryRegionSize
);
603 dprintk(1, " .dwHostHibernatMemRegion = 0x%x\n",
604 dev
->hwdesc
.dwHostHibernatMemRegion
);
606 dprintk(1, " .dwHostHibernatMemRegionSize = 0x%x\n",
607 dev
->hwdesc
.dwHostHibernatMemRegionSize
);
610 static void saa7164_dump_intfdesc(struct saa7164_dev
*dev
)
612 dprintk(1, "@0x%p intfdesc "
613 "sizeof(tmComResInterfaceDescr_t) = %d bytes\n",
614 &dev
->intfdesc
, (u32
)sizeof(tmComResInterfaceDescr_t
));
616 dprintk(1, " .bLength = 0x%x\n", dev
->intfdesc
.bLength
);
617 dprintk(1, " .bDescriptorType = 0x%x\n", dev
->intfdesc
.bDescriptorType
);
618 dprintk(1, " .bDescriptorSubtype = 0x%x\n",
619 dev
->intfdesc
.bDescriptorSubtype
);
621 dprintk(1, " .bFlags = 0x%x\n", dev
->intfdesc
.bFlags
);
622 dprintk(1, " .bInterfaceType = 0x%x\n", dev
->intfdesc
.bInterfaceType
);
623 dprintk(1, " .bInterfaceId = 0x%x\n", dev
->intfdesc
.bInterfaceId
);
624 dprintk(1, " .bBaseInterface = 0x%x\n", dev
->intfdesc
.bBaseInterface
);
625 dprintk(1, " .bInterruptId = 0x%x\n", dev
->intfdesc
.bInterruptId
);
626 dprintk(1, " .bDebugInterruptId = 0x%x\n",
627 dev
->intfdesc
.bDebugInterruptId
);
629 dprintk(1, " .BARLocation = 0x%x\n", dev
->intfdesc
.BARLocation
);
632 static void saa7164_dump_busdesc(struct saa7164_dev
*dev
)
634 dprintk(1, "@0x%p busdesc sizeof(tmComResBusDescr_t) = %d bytes\n",
635 &dev
->busdesc
, (u32
)sizeof(tmComResBusDescr_t
));
637 dprintk(1, " .CommandRing = 0x%016Lx\n", dev
->busdesc
.CommandRing
);
638 dprintk(1, " .ResponseRing = 0x%016Lx\n", dev
->busdesc
.ResponseRing
);
639 dprintk(1, " .CommandWrite = 0x%x\n", dev
->busdesc
.CommandWrite
);
640 dprintk(1, " .CommandRead = 0x%x\n", dev
->busdesc
.CommandRead
);
641 dprintk(1, " .ResponseWrite = 0x%x\n", dev
->busdesc
.ResponseWrite
);
642 dprintk(1, " .ResponseRead = 0x%x\n", dev
->busdesc
.ResponseRead
);
645 /* Much of the hardware configuration and PCI registers are configured
646 * dynamically depending on firmware. We have to cache some initial
647 * structures then use these to locate other important structures
650 static void saa7164_get_descriptors(struct saa7164_dev
*dev
)
652 memcpy(&dev
->hwdesc
, dev
->bmmio
, sizeof(tmComResHWDescr_t
));
653 memcpy(&dev
->intfdesc
, dev
->bmmio
+ sizeof(tmComResHWDescr_t
),
654 sizeof(tmComResInterfaceDescr_t
));
655 memcpy(&dev
->busdesc
, dev
->bmmio
+ dev
->intfdesc
.BARLocation
,
656 sizeof(tmComResBusDescr_t
));
658 if (dev
->hwdesc
.bLength
!= sizeof(tmComResHWDescr_t
)) {
659 printk(KERN_ERR
"Structure tmComResHWDescr_t is mangled\n");
660 printk(KERN_ERR
"Need %x got %d\n", dev
->hwdesc
.bLength
,
661 (u32
)sizeof(tmComResHWDescr_t
));
663 saa7164_dump_hwdesc(dev
);
665 if (dev
->intfdesc
.bLength
!= sizeof(tmComResInterfaceDescr_t
)) {
666 printk(KERN_ERR
"struct tmComResInterfaceDescr_t is mangled\n");
667 printk(KERN_ERR
"Need %x got %d\n", dev
->intfdesc
.bLength
,
668 (u32
)sizeof(tmComResInterfaceDescr_t
));
670 saa7164_dump_intfdesc(dev
);
672 saa7164_dump_busdesc(dev
);
675 static int saa7164_pci_quirks(struct saa7164_dev
*dev
)
680 static int get_resources(struct saa7164_dev
*dev
)
682 if (request_mem_region(pci_resource_start(dev
->pci
, 0),
683 pci_resource_len(dev
->pci
, 0), dev
->name
)) {
685 if (request_mem_region(pci_resource_start(dev
->pci
, 2),
686 pci_resource_len(dev
->pci
, 2), dev
->name
))
690 printk(KERN_ERR
"%s: can't get MMIO memory @ 0x%llx or 0x%llx\n",
692 (u64
)pci_resource_start(dev
->pci
, 0),
693 (u64
)pci_resource_start(dev
->pci
, 2));
698 static int saa7164_port_init(struct saa7164_dev
*dev
, int portnr
)
700 struct saa7164_port
*port
= 0;
702 if ((portnr
< 0) || (portnr
>= SAA7164_MAX_PORTS
))
705 port
= &dev
->ports
[ portnr
];
710 if ((portnr
== SAA7164_PORT_TS1
) || (portnr
== SAA7164_PORT_TS2
))
711 port
->type
= SAA7164_MPEG_DVB
;
713 if ((portnr
== SAA7164_PORT_ENC1
) || (portnr
== SAA7164_PORT_ENC2
))
714 port
->type
= SAA7164_MPEG_ENCODER
;
718 /* Init all the critical resources */
719 mutex_init(&port
->dvb
.lock
);
720 INIT_LIST_HEAD(&port
->dmaqueue
.list
);
721 mutex_init(&port
->dmaqueue_lock
);
723 INIT_LIST_HEAD(&port
->list_buf_used
.list
);
724 INIT_LIST_HEAD(&port
->list_buf_free
.list
);
725 init_waitqueue_head(&port
->wait_read
);
727 /* We need a deferred interrupt handler for cmd handling */
728 INIT_WORK(&port
->workenc
, saa7164_work_enchandler
);
730 saa7164_histogram_reset(&port
->irq_interval
, "irq intervals");
731 saa7164_histogram_reset(&port
->svc_interval
, "deferred intervals");
732 saa7164_histogram_reset(&port
->irq_svc_interval
,
733 "irq to deferred intervals");
738 static int saa7164_dev_setup(struct saa7164_dev
*dev
)
742 mutex_init(&dev
->lock
);
743 atomic_inc(&dev
->refcount
);
744 dev
->nr
= saa7164_devcount
++;
746 sprintf(dev
->name
, "saa7164[%d]", dev
->nr
);
748 mutex_lock(&devlist
);
749 list_add_tail(&dev
->devlist
, &saa7164_devlist
);
750 mutex_unlock(&devlist
);
754 if (card
[dev
->nr
] < saa7164_bcount
)
755 dev
->board
= card
[dev
->nr
];
757 for (i
= 0; UNSET
== dev
->board
&& i
< saa7164_idcount
; i
++)
758 if (dev
->pci
->subsystem_vendor
== saa7164_subids
[i
].subvendor
&&
759 dev
->pci
->subsystem_device
==
760 saa7164_subids
[i
].subdevice
)
761 dev
->board
= saa7164_subids
[i
].card
;
763 if (UNSET
== dev
->board
) {
764 dev
->board
= SAA7164_BOARD_UNKNOWN
;
765 saa7164_card_list(dev
);
768 dev
->pci_bus
= dev
->pci
->bus
->number
;
769 dev
->pci_slot
= PCI_SLOT(dev
->pci
->devfn
);
771 /* I2C Defaults / setup */
772 dev
->i2c_bus
[0].dev
= dev
;
773 dev
->i2c_bus
[0].nr
= 0;
774 dev
->i2c_bus
[1].dev
= dev
;
775 dev
->i2c_bus
[1].nr
= 1;
776 dev
->i2c_bus
[2].dev
= dev
;
777 dev
->i2c_bus
[2].nr
= 2;
779 /* Transport + Encoder ports 1, 2, 3, 4 - Defaults / setup */
780 saa7164_port_init(dev
, SAA7164_PORT_TS1
);
781 saa7164_port_init(dev
, SAA7164_PORT_TS2
);
782 saa7164_port_init(dev
, SAA7164_PORT_ENC1
);
783 saa7164_port_init(dev
, SAA7164_PORT_ENC2
);
785 if (get_resources(dev
) < 0) {
786 printk(KERN_ERR
"CORE %s No more PCIe resources for "
787 "subsystem: %04x:%04x\n",
788 dev
->name
, dev
->pci
->subsystem_vendor
,
789 dev
->pci
->subsystem_device
);
795 /* PCI/e allocations */
796 dev
->lmmio
= ioremap(pci_resource_start(dev
->pci
, 0),
797 pci_resource_len(dev
->pci
, 0));
799 dev
->lmmio2
= ioremap(pci_resource_start(dev
->pci
, 2),
800 pci_resource_len(dev
->pci
, 2));
802 dev
->bmmio
= (u8 __iomem
*)dev
->lmmio
;
803 dev
->bmmio2
= (u8 __iomem
*)dev
->lmmio2
;
805 /* Inerrupt and ack register locations offset of bmmio */
806 dev
->int_status
= 0x183000 + 0xf80;
807 dev
->int_ack
= 0x183000 + 0xf90;
810 "CORE %s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
811 dev
->name
, dev
->pci
->subsystem_vendor
,
812 dev
->pci
->subsystem_device
, saa7164_boards
[dev
->board
].name
,
813 dev
->board
, card
[dev
->nr
] == dev
->board
?
814 "insmod option" : "autodetected");
816 saa7164_pci_quirks(dev
);
821 static void saa7164_dev_unregister(struct saa7164_dev
*dev
)
823 dprintk(1, "%s()\n", __func__
);
825 release_mem_region(pci_resource_start(dev
->pci
, 0),
826 pci_resource_len(dev
->pci
, 0));
828 release_mem_region(pci_resource_start(dev
->pci
, 2),
829 pci_resource_len(dev
->pci
, 2));
831 if (!atomic_dec_and_test(&dev
->refcount
))
835 iounmap(dev
->lmmio2
);
840 static int __devinit
saa7164_initdev(struct pci_dev
*pci_dev
,
841 const struct pci_device_id
*pci_id
)
843 struct saa7164_dev
*dev
;
847 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
853 if (pci_enable_device(pci_dev
)) {
858 if (saa7164_dev_setup(dev
) < 0) {
864 pci_read_config_byte(pci_dev
, PCI_CLASS_REVISION
, &dev
->pci_rev
);
865 pci_read_config_byte(pci_dev
, PCI_LATENCY_TIMER
, &dev
->pci_lat
);
866 printk(KERN_INFO
"%s/0: found at %s, rev: %d, irq: %d, "
867 "latency: %d, mmio: 0x%llx\n", dev
->name
,
868 pci_name(pci_dev
), dev
->pci_rev
, pci_dev
->irq
,
870 (unsigned long long)pci_resource_start(pci_dev
, 0));
872 pci_set_master(pci_dev
);
874 if (!pci_dma_supported(pci_dev
, 0xffffffff)) {
875 printk("%s/0: Oops: no 32bit PCI DMA ???\n", dev
->name
);
880 err
= request_irq(pci_dev
->irq
, saa7164_irq
,
881 IRQF_SHARED
| IRQF_DISABLED
, dev
->name
, dev
);
883 printk(KERN_ERR
"%s: can't get IRQ %d\n", dev
->name
,
889 pci_set_drvdata(pci_dev
, dev
);
891 /* Init the internal command list */
892 for (i
= 0; i
< SAA_CMD_MAX_MSG_UNITS
; i
++) {
893 dev
->cmds
[i
].seqno
= i
;
894 dev
->cmds
[i
].inuse
= 0;
895 mutex_init(&dev
->cmds
[i
].lock
);
896 init_waitqueue_head(&dev
->cmds
[i
].wait
);
899 /* We need a deferred interrupt handler for cmd handling */
900 INIT_WORK(&dev
->workcmd
, saa7164_work_cmdhandler
);
902 /* Only load the firmware if we know the board */
903 if (dev
->board
!= SAA7164_BOARD_UNKNOWN
) {
905 err
= saa7164_downloadfirmware(dev
);
908 "Failed to boot firmware, no features "
913 saa7164_get_descriptors(dev
);
914 saa7164_dumpregs(dev
, 0);
915 saa7164_getcurrentfirmwareversion(dev
);
916 saa7164_getfirmwarestatus(dev
);
917 err
= saa7164_bus_setup(dev
);
920 "Failed to setup the bus, will continue\n");
921 saa7164_bus_dump(dev
);
923 /* Ping the running firmware via the command bus and get the
924 * firmware version, this checks the bus is running OK.
927 if (saa7164_api_get_fw_version(dev
, &version
) == SAA_OK
)
928 dprintk(1, "Bus is operating correctly using "
929 "version %d.%d.%d.%d (0x%x)\n",
930 (version
& 0x0000fc00) >> 10,
931 (version
& 0x000003e0) >> 5,
932 (version
& 0x0000001f),
933 (version
& 0xffff0000) >> 16,
937 "Failed to communicate with the firmware\n");
939 /* Bring up the I2C buses */
940 saa7164_i2c_register(&dev
->i2c_bus
[0]);
941 saa7164_i2c_register(&dev
->i2c_bus
[1]);
942 saa7164_i2c_register(&dev
->i2c_bus
[2]);
943 saa7164_gpio_setup(dev
);
944 saa7164_card_setup(dev
);
947 /* Parse the dynamic device configuration, find various
948 * media endpoints (MPEG, WMV, PS, TS) and cache their
949 * configuration details into the driver, so we can
950 * reference them later during simething_register() func,
951 * interrupt handlers, deferred work handlers etc.
953 saa7164_api_enum_subdevs(dev
);
955 /* Begin to create the video sub-systems and register funcs */
956 if (saa7164_boards
[dev
->board
].porta
== SAA7164_MPEG_DVB
) {
957 if (saa7164_dvb_register(&dev
->ports
[ SAA7164_PORT_TS1
]) < 0) {
958 printk(KERN_ERR
"%s() Failed to register "
959 "dvb adapters on porta\n",
964 if (saa7164_boards
[dev
->board
].portb
== SAA7164_MPEG_DVB
) {
965 if (saa7164_dvb_register(&dev
->ports
[ SAA7164_PORT_TS2
]) < 0) {
966 printk(KERN_ERR
"%s() Failed to register "
967 "dvb adapters on portb\n",
972 if (saa7164_boards
[dev
->board
].portc
== SAA7164_MPEG_ENCODER
) {
973 if (saa7164_encoder_register(&dev
->ports
[ SAA7164_PORT_ENC1
]) < 0) {
974 printk(KERN_ERR
"%s() Failed to register "
975 "mpeg encoder\n", __func__
);
979 if (saa7164_boards
[dev
->board
].portd
== SAA7164_MPEG_ENCODER
) {
980 if (saa7164_encoder_register(&dev
->ports
[ SAA7164_PORT_ENC2
]) < 0) {
981 printk(KERN_ERR
"%s() Failed to register "
982 "mpeg encoder\n", __func__
);
986 } /* != BOARD_UNKNOWN */
988 printk(KERN_ERR
"%s() Unsupported board detected, "
989 "registering without firmware\n", __func__
);
991 dprintk(1, "%s() parameter debug = %d\n", __func__
, saa_debug
);
992 dprintk(1, "%s() parameter waitsecs = %d\n", __func__
, waitsecs
);
998 saa7164_dev_unregister(dev
);
1004 static void saa7164_shutdown(struct saa7164_dev
*dev
)
1006 dprintk(1, "%s()\n", __func__
);
1009 static void __devexit
saa7164_finidev(struct pci_dev
*pci_dev
)
1011 struct saa7164_dev
*dev
= pci_get_drvdata(pci_dev
);
1013 saa7164_histogram_print(&dev
->ports
[ SAA7164_PORT_ENC1
],
1014 &dev
->ports
[ SAA7164_PORT_ENC1
].irq_interval
);
1015 saa7164_histogram_print(&dev
->ports
[ SAA7164_PORT_ENC1
],
1016 &dev
->ports
[ SAA7164_PORT_ENC1
].svc_interval
);
1017 saa7164_histogram_print(&dev
->ports
[ SAA7164_PORT_ENC1
],
1018 &dev
->ports
[ SAA7164_PORT_ENC1
].irq_svc_interval
);
1020 saa7164_shutdown(dev
);
1022 if (saa7164_boards
[dev
->board
].porta
== SAA7164_MPEG_DVB
)
1023 saa7164_dvb_unregister(&dev
->ports
[ SAA7164_PORT_TS1
]);
1025 if (saa7164_boards
[dev
->board
].portb
== SAA7164_MPEG_DVB
)
1026 saa7164_dvb_unregister(&dev
->ports
[ SAA7164_PORT_TS2
]);
1028 if (saa7164_boards
[dev
->board
].portc
== SAA7164_MPEG_ENCODER
)
1029 saa7164_encoder_unregister(&dev
->ports
[ SAA7164_PORT_ENC1
]);
1031 if (saa7164_boards
[dev
->board
].portd
== SAA7164_MPEG_ENCODER
)
1032 saa7164_encoder_unregister(&dev
->ports
[ SAA7164_PORT_ENC2
]);
1034 saa7164_i2c_unregister(&dev
->i2c_bus
[0]);
1035 saa7164_i2c_unregister(&dev
->i2c_bus
[1]);
1036 saa7164_i2c_unregister(&dev
->i2c_bus
[2]);
1038 pci_disable_device(pci_dev
);
1040 /* unregister stuff */
1041 free_irq(pci_dev
->irq
, dev
);
1042 pci_set_drvdata(pci_dev
, NULL
);
1044 mutex_lock(&devlist
);
1045 list_del(&dev
->devlist
);
1046 mutex_unlock(&devlist
);
1048 saa7164_dev_unregister(dev
);
1052 static struct pci_device_id saa7164_pci_tbl
[] = {
1057 .subvendor
= PCI_ANY_ID
,
1058 .subdevice
= PCI_ANY_ID
,
1060 /* --- end of list --- */
1063 MODULE_DEVICE_TABLE(pci
, saa7164_pci_tbl
);
1065 static struct pci_driver saa7164_pci_driver
= {
1067 .id_table
= saa7164_pci_tbl
,
1068 .probe
= saa7164_initdev
,
1069 .remove
= __devexit_p(saa7164_finidev
),
1075 static int __init
saa7164_init(void)
1077 printk(KERN_INFO
"saa7164 driver loaded\n");
1078 return pci_register_driver(&saa7164_pci_driver
);
1081 static void __exit
saa7164_fini(void)
1083 pci_unregister_driver(&saa7164_pci_driver
);
1086 module_init(saa7164_init
);
1087 module_exit(saa7164_fini
);