[media] saa7164: allow variable length GOP sizes and switch encoder default to CBR
[deliverable/linux.git] / drivers / media / video / saa7164 / saa7164-core.c
CommitLineData
443c1228
ST
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
9b8b0199 4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
443c1228
ST
5 *
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.
10 *
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
14 *
15 * GNU General Public License for more details.
16 *
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.
20 */
21
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>
32
33#include "saa7164.h"
34
35MODULE_DESCRIPTION("Driver for NXP SAA7164 based TV cards");
9d119c33 36MODULE_AUTHOR("Steven Toth <stoth@kernellabs.com>");
443c1228
ST
37MODULE_LICENSE("GPL");
38
39/*
40 1 Basic
41 2
42 4 i2c
43 8 api
44 16 cmd
45 32 bus
46 */
47
b1912a85
IM
48unsigned int saa_debug;
49module_param_named(debug, saa_debug, int, 0644);
443c1228
ST
50MODULE_PARM_DESC(debug, "enable debug messages");
51
bbf504c3 52unsigned int waitsecs = 10;
dd1ee444
ST
53module_param(waitsecs, int, 0644);
54MODULE_PARM_DESC(debug, "timeout on firmware messages");
55
443c1228
ST
56static unsigned int card[] = {[0 ... (SAA7164_MAXBOARDS - 1)] = UNSET };
57module_param_array(card, int, NULL, 0444);
58MODULE_PARM_DESC(card, "card type");
59
91d80189
ST
60unsigned int print_histogram = 64;
61module_param(print_histogram, int, 0644);
62MODULE_PARM_DESC(debug, "print histogram values once");
63
443c1228
ST
64static unsigned int saa7164_devcount;
65
66static DEFINE_MUTEX(devlist);
67LIST_HEAD(saa7164_devlist);
68
69#define INT_SIZE 16
70
91d80189 71static void saa7164_histogram_reset(struct saa7164_histogram *hg, char *name)
443c1228 72{
91d80189 73 int i;
443c1228 74
91d80189
ST
75 memset(hg, 0, sizeof(struct saa7164_histogram));
76 strcpy(hg->name, name);
77
78 /* First 30ms x 1ms */
79 for (i = 0; i < 30; i++) {
80 hg->counter1[0 + i].val = i;
81 }
82
83 /* 30 - 200ms x 10ms */
84 for (i = 0; i < 18; i++) {
85 hg->counter1[30 + i].val = 30 + (i * 10);
86 }
87
88 /* 200 - 2000ms x 100ms */
89 for (i = 0; i < 15; i++) {
90 hg->counter1[48 + i].val = 200 + (i * 100);
91 }
92
93 /* Catch all massive value (1hr) */
94 hg->counter1[63].val = 3600000;
443c1228
ST
95}
96
91d80189 97static void saa7164_histogram_update(struct saa7164_histogram *hg, u32 val)
443c1228 98{
91d80189
ST
99 int i;
100 for (i = 0; i < 64; i++ ) {
101 if (val <= hg->counter1[i].val) {
102 hg->counter1[i].count++;
103 hg->counter1[i].update_time = jiffies;
104 break;
105 }
106 }
107}
443c1228 108
91d80189
ST
109static void saa7164_histogram_print(struct saa7164_port *port,
110 struct saa7164_histogram *hg)
111{
112 struct saa7164_dev *dev = port->dev;
113 u32 entries = 0;
114 int i;
115
116 printk(KERN_ERR "Histogram named %s\n", hg->name);
117 for (i = 0; i < 64; i++ ) {
118 if (hg->counter1[i].count == 0)
119 continue;
443c1228 120
91d80189
ST
121 printk(KERN_ERR " %4d %12d %Ld\n",
122 hg->counter1[i].val,
123 hg->counter1[i].count,
124 hg->counter1[i].update_time);
125
126 entries++;
127 }
128 printk(KERN_ERR "Total: %d\n", entries);
443c1228
ST
129}
130
91d80189 131static void saa7164_work_enchandler(struct work_struct *w)
7615e434 132{
91d80189
ST
133 struct saa7164_port *port =
134 container_of(w, struct saa7164_port, workenc);
7615e434
ST
135 struct saa7164_dev *dev = port->dev;
136 struct saa7164_buffer *buf;
137 struct saa7164_user_buffer *ubuf;
138 struct list_head *c, *n;
91d80189 139 int wp, rp, i = 0;
7615e434 140
91d80189
ST
141 port->last_svc_msecs_diff = port->last_svc_msecs;
142 port->last_svc_msecs = jiffies_to_msecs(jiffies);
143 port->last_svc_wp = saa7164_readl(port->bufcounter);
144 port->last_svc_rp = port->last_irq_rp;
145 wp = port->last_svc_wp;
146 rp = port->last_svc_rp;
7615e434 147
7615e434 148
91d80189
ST
149 port->last_svc_msecs_diff = port->last_svc_msecs -
150 port->last_svc_msecs_diff;
151
152 saa7164_histogram_update(&port->svc_interval,
153 port->last_svc_msecs_diff);
154
155 port->last_irq_svc_msecs_diff = port->last_svc_msecs -
156 port->last_irq_msecs;
157
158 saa7164_histogram_update(&port->irq_svc_interval,
159 port->last_irq_svc_msecs_diff);
160
161 dprintk(DBGLVL_IRQ,
162 "%s() %Ldms elapsed irq->deferred %Ldms wp: %d rp: %d\n",
163 __func__,
164 port->last_svc_msecs_diff,
165 port->last_irq_svc_msecs_diff,
166 port->last_svc_wp,
167 port->last_svc_rp
168 );
169
170 if ((rp < 0) || (rp > 7)) {
171 printk(KERN_ERR "%s() illegal rp count %d\n", __func__, rp);
172 return;
173 }
174
175 mutex_lock(&port->dmaqueue_lock);
176
7615e434 177 list_for_each_safe(c, n, &port->dmaqueue.list) {
91d80189 178
7615e434 179 buf = list_entry(c, struct saa7164_buffer, list);
91d80189
ST
180 if (i++ > port->hwcfg.buffercount) {
181 printk(KERN_ERR "%s() illegal i count %d\n",
182 __func__, i);
183 break;
184 }
7615e434
ST
185
186 if (buf->idx == rp) {
187 /* Found the buffer, deal with it */
188 dprintk(DBGLVL_IRQ, "%s() wp: %d processing: %d\n",
189 __func__, wp, rp);
190
191 /* */
192 /* find a free user buffer and clone to it */
193 if (!list_empty(&port->list_buf_free.list)) {
194
195 /* Pull the first buffer from the used list */
196 ubuf = list_first_entry(&port->list_buf_free.list,
197 struct saa7164_user_buffer, list);
198
199 if (ubuf->actual_size == buf->actual_size)
91d80189
ST
200 memcpy(ubuf->data, buf->cpu,
201 ubuf->actual_size);
7615e434
ST
202
203 /* Requeue the buffer on the free list */
204 ubuf->pos = 0;
205
91d80189
ST
206 list_move_tail(&ubuf->list,
207 &port->list_buf_used.list);
7615e434
ST
208
209 /* Flag any userland waiters */
210 wake_up_interruptible(&port->wait_read);
211
212 } else
213 printk(KERN_ERR "encirq no free buffers\n");
214
215 break;
216 }
217
218 }
91d80189
ST
219 mutex_unlock(&port->dmaqueue_lock);
220
221 if (print_histogram == port->nr) {
222 saa7164_histogram_print(port, &port->irq_interval);
223 saa7164_histogram_print(port, &port->svc_interval);
224 saa7164_histogram_print(port, &port->irq_svc_interval);
225 print_histogram = 64 + port->nr;
226 }
227}
228
229static void saa7164_work_cmdhandler(struct work_struct *w)
230{
231 struct saa7164_dev *dev = container_of(w, struct saa7164_dev, workcmd);
232
233 /* Wake up any complete commands */
234 saa7164_irq_dequeue(dev);
235}
236
237static void saa7164_buffer_deliver(struct saa7164_buffer *buf)
238{
239 struct saa7164_port *port = buf->port;
240
241 /* Feed the transport payload into the kernel demux */
242 dvb_dmx_swfilter_packets(&port->dvb.demux, (u8 *)buf->cpu,
243 SAA7164_TS_NUMBER_OF_LINES);
244
245}
246
247static irqreturn_t saa7164_irq_encoder(struct saa7164_port *port)
248{
249 struct saa7164_dev *dev = port->dev;
250 int wp, rp;
251
252 /* Find the current write point from the hardware */
253 wp = saa7164_readl(port->bufcounter);
254 if (wp > (port->hwcfg.buffercount - 1)) {
255 printk(KERN_ERR "%s() illegal buf count %d\n", __func__, wp);
256 return 0;
257 }
258
259 /* Find the previous buffer to the current write point */
260 if (wp == 0)
261 rp = 7;
262 else
263 rp = wp - 1;
264
265 if ((rp < 0) || (rp > 7)) {
266 printk(KERN_ERR "%s() illegal rp count %d\n", __func__, rp);
267 return 0;
268 }
269
07603131
ST
270 if (rp != ((port->last_irq_rp + 1) % 8)) {
271 printk(KERN_ERR "%s() Multiple bufs on interrupt, port %p\n",
272 __func__, port);
273 }
274
275 /* Store old time */
91d80189
ST
276 port->last_irq_msecs_diff = port->last_irq_msecs;
277
278 /* Collect new stats */
279 port->last_irq_msecs = jiffies_to_msecs(jiffies);
280 port->last_irq_wp = wp;
281 port->last_irq_rp = rp;
282
283 /* Calculate stats */
284 port->last_irq_msecs_diff = port->last_irq_msecs -
285 port->last_irq_msecs_diff;
286
287 saa7164_histogram_update(&port->irq_interval,
288 port->last_irq_msecs_diff);
289
290 dprintk(DBGLVL_IRQ, "%s() %Ldms elapsed wp: %d rp: %d\n",
291 __func__,
292 port->last_irq_msecs_diff,
293 port->last_irq_wp,
294 port->last_irq_rp
295 );
296
297 schedule_work(&port->workenc);
298
7615e434
ST
299 return 0;
300}
301
add3f580 302static irqreturn_t saa7164_irq_ts(struct saa7164_port *port)
443c1228
ST
303{
304 struct saa7164_dev *dev = port->dev;
305 struct saa7164_buffer *buf;
306 struct list_head *c, *n;
307 int wp, i = 0, rp;
308
309 /* Find the current write point from the hardware */
310 wp = saa7164_readl(port->bufcounter);
311 if (wp > (port->hwcfg.buffercount - 1))
312 BUG();
313
314 /* Find the previous buffer to the current write point */
315 if (wp == 0)
316 rp = 7;
317 else
318 rp = wp - 1;
319
320 /* Lookup the WP in the buffer list */
321 /* TODO: turn this into a worker thread */
322 list_for_each_safe(c, n, &port->dmaqueue.list) {
323 buf = list_entry(c, struct saa7164_buffer, list);
324 if (i++ > port->hwcfg.buffercount)
325 BUG();
326
add3f580 327 if (buf->idx == rp) {
443c1228
ST
328 /* Found the buffer, deal with it */
329 dprintk(DBGLVL_IRQ, "%s() wp: %d processing: %d\n",
330 __func__, wp, rp);
331 saa7164_buffer_deliver(buf);
332 break;
333 }
334
335 }
336 return 0;
337}
338
339/* Primary IRQ handler and dispatch mechanism */
340static irqreturn_t saa7164_irq(int irq, void *dev_id)
341{
342 struct saa7164_dev *dev = dev_id;
7615e434
ST
343 struct saa7164_port *porta = &dev->ports[ SAA7164_PORT_TS1 ];
344 struct saa7164_port *portb = &dev->ports[ SAA7164_PORT_TS2 ];
345 struct saa7164_port *portc = &dev->ports[ SAA7164_PORT_ENC1 ];
346 struct saa7164_port *portd = &dev->ports[ SAA7164_PORT_ENC2 ];
347
50bcb4ae 348 u32 intid, intstat[INT_SIZE/4];
443c1228
ST
349 int i, handled = 0, bit;
350
d888ea03
ST
351 if (dev == 0) {
352 printk(KERN_ERR "%s() No device specified\n", __func__);
353 handled = 0;
354 goto out;
355 }
356
443c1228
ST
357 /* Check that the hardware is accessable. If the status bytes are
358 * 0xFF then the device is not accessable, the the IRQ belongs
359 * to another driver.
1a6450d4 360 * 4 x u32 interrupt registers.
443c1228
ST
361 */
362 for (i = 0; i < INT_SIZE/4; i++) {
363
364 /* TODO: Convert into saa7164_readl() */
365 /* Read the 4 hardware interrupt registers */
1a6450d4 366 intstat[i] = saa7164_readl(dev->int_status + (i * 4));
443c1228 367
50bcb4ae
ST
368 if (intstat[i])
369 handled = 1;
443c1228 370 }
50bcb4ae 371 if (handled == 0)
443c1228 372 goto out;
443c1228
ST
373
374 /* For each of the HW interrupt registers */
375 for (i = 0; i < INT_SIZE/4; i++) {
376
377 if (intstat[i]) {
378 /* Each function of the board has it's own interruptid.
379 * Find the function that triggered then call
380 * it's handler.
381 */
382 for (bit = 0; bit < 32; bit++) {
383
384 if (((intstat[i] >> bit) & 0x00000001) == 0)
385 continue;
386
387 /* Calculate the interrupt id (0x00 to 0x7f) */
388
50bcb4ae
ST
389 intid = (i * 32) + bit;
390 if (intid == dev->intfdesc.bInterruptId) {
443c1228
ST
391 /* A response to an cmd/api call */
392 schedule_work(&dev->workcmd);
7615e434 393 } else if (intid == porta->hwcfg.interruptid) {
443c1228
ST
394
395 /* Transport path 1 */
7615e434 396 saa7164_irq_ts(porta);
443c1228 397
7615e434 398 } else if (intid == portb->hwcfg.interruptid) {
443c1228
ST
399
400 /* Transport path 2 */
7615e434
ST
401 saa7164_irq_ts(portb);
402
403 } else if (intid == portc->hwcfg.interruptid) {
404
405 /* Encoder path 1 */
406 saa7164_irq_encoder(portc);
407
408 } else if (intid == portd->hwcfg.interruptid) {
409
410 /* Encoder path 1 */
411 saa7164_irq_encoder(portd);
443c1228
ST
412
413 } else {
414 /* Find the function */
415 dprintk(DBGLVL_IRQ,
416 "%s() unhandled interrupt "
417 "reg 0x%x bit 0x%x "
418 "intid = 0x%x\n",
50bcb4ae 419 __func__, i, bit, intid);
443c1228
ST
420 }
421 }
422
443c1228 423 /* Ack it */
1a6450d4 424 saa7164_writel(dev->int_ack + (i * 4), intstat[i]);
443c1228
ST
425
426 }
427 }
428out:
429 return IRQ_RETVAL(handled);
430}
431
432void saa7164_getfirmwarestatus(struct saa7164_dev *dev)
433{
434 struct saa7164_fw_status *s = &dev->fw_status;
435
436 dev->fw_status.status = saa7164_readl(SAA_DEVICE_SYSINIT_STATUS);
437 dev->fw_status.mode = saa7164_readl(SAA_DEVICE_SYSINIT_MODE);
438 dev->fw_status.spec = saa7164_readl(SAA_DEVICE_SYSINIT_SPEC);
439 dev->fw_status.inst = saa7164_readl(SAA_DEVICE_SYSINIT_INST);
440 dev->fw_status.cpuload = saa7164_readl(SAA_DEVICE_SYSINIT_CPULOAD);
441 dev->fw_status.remainheap =
442 saa7164_readl(SAA_DEVICE_SYSINIT_REMAINHEAP);
443
444 dprintk(1, "Firmware status:\n");
445 dprintk(1, " .status = 0x%08x\n", s->status);
446 dprintk(1, " .mode = 0x%08x\n", s->mode);
447 dprintk(1, " .spec = 0x%08x\n", s->spec);
448 dprintk(1, " .inst = 0x%08x\n", s->inst);
449 dprintk(1, " .cpuload = 0x%08x\n", s->cpuload);
450 dprintk(1, " .remainheap = 0x%08x\n", s->remainheap);
451}
452
453u32 saa7164_getcurrentfirmwareversion(struct saa7164_dev *dev)
454{
455 u32 reg;
456
457 reg = saa7164_readl(SAA_DEVICE_VERSION);
458 dprintk(1, "Device running firmware version %d.%d.%d.%d (0x%x)\n",
459 (reg & 0x0000fc00) >> 10,
460 (reg & 0x000003e0) >> 5,
461 (reg & 0x0000001f),
462 (reg & 0xffff0000) >> 16,
463 reg);
464
465 return reg;
466}
467
468/* TODO: Debugging func, remove */
469void saa7164_dumphex16(struct saa7164_dev *dev, u8 *buf, int len)
470{
471 int i;
472
473 printk(KERN_INFO "--------------------> "
474 "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
475
476 for (i = 0; i < len; i += 16)
477 printk(KERN_INFO " [0x%08x] "
478 "%02x %02x %02x %02x %02x %02x %02x %02x "
479 "%02x %02x %02x %02x %02x %02x %02x %02x\n", i,
480 *(buf+i+0), *(buf+i+1), *(buf+i+2), *(buf+i+3),
481 *(buf+i+4), *(buf+i+5), *(buf+i+6), *(buf+i+7),
482 *(buf+i+8), *(buf+i+9), *(buf+i+10), *(buf+i+11),
483 *(buf+i+12), *(buf+i+13), *(buf+i+14), *(buf+i+15));
484}
485
486/* TODO: Debugging func, remove */
487void saa7164_dumpregs(struct saa7164_dev *dev, u32 addr)
488{
489 int i;
490
491 dprintk(1, "--------------------> "
492 "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
493
494 for (i = 0; i < 0x100; i += 16)
495 dprintk(1, "region0[0x%08x] = "
496 "%02x %02x %02x %02x %02x %02x %02x %02x"
497 " %02x %02x %02x %02x %02x %02x %02x %02x\n", i,
498 (u8)saa7164_readb(addr + i + 0),
499 (u8)saa7164_readb(addr + i + 1),
500 (u8)saa7164_readb(addr + i + 2),
501 (u8)saa7164_readb(addr + i + 3),
502 (u8)saa7164_readb(addr + i + 4),
503 (u8)saa7164_readb(addr + i + 5),
504 (u8)saa7164_readb(addr + i + 6),
505 (u8)saa7164_readb(addr + i + 7),
506 (u8)saa7164_readb(addr + i + 8),
507 (u8)saa7164_readb(addr + i + 9),
508 (u8)saa7164_readb(addr + i + 10),
509 (u8)saa7164_readb(addr + i + 11),
510 (u8)saa7164_readb(addr + i + 12),
511 (u8)saa7164_readb(addr + i + 13),
512 (u8)saa7164_readb(addr + i + 14),
513 (u8)saa7164_readb(addr + i + 15)
514 );
515}
516
517static void saa7164_dump_hwdesc(struct saa7164_dev *dev)
518{
207b42c4
ST
519 dprintk(1, "@0x%p hwdesc sizeof(tmComResHWDescr_t) = %d bytes\n",
520 &dev->hwdesc, (u32)sizeof(tmComResHWDescr_t));
443c1228
ST
521
522 dprintk(1, " .bLength = 0x%x\n", dev->hwdesc.bLength);
523 dprintk(1, " .bDescriptorType = 0x%x\n", dev->hwdesc.bDescriptorType);
524 dprintk(1, " .bDescriptorSubtype = 0x%x\n",
525 dev->hwdesc.bDescriptorSubtype);
526
527 dprintk(1, " .bcdSpecVersion = 0x%x\n", dev->hwdesc.bcdSpecVersion);
528 dprintk(1, " .dwClockFrequency = 0x%x\n", dev->hwdesc.dwClockFrequency);
529 dprintk(1, " .dwClockUpdateRes = 0x%x\n", dev->hwdesc.dwClockUpdateRes);
530 dprintk(1, " .bCapabilities = 0x%x\n", dev->hwdesc.bCapabilities);
531 dprintk(1, " .dwDeviceRegistersLocation = 0x%x\n",
532 dev->hwdesc.dwDeviceRegistersLocation);
533
534 dprintk(1, " .dwHostMemoryRegion = 0x%x\n",
535 dev->hwdesc.dwHostMemoryRegion);
536
537 dprintk(1, " .dwHostMemoryRegionSize = 0x%x\n",
538 dev->hwdesc.dwHostMemoryRegionSize);
539
540 dprintk(1, " .dwHostHibernatMemRegion = 0x%x\n",
541 dev->hwdesc.dwHostHibernatMemRegion);
542
543 dprintk(1, " .dwHostHibernatMemRegionSize = 0x%x\n",
544 dev->hwdesc.dwHostHibernatMemRegionSize);
545}
546
547static void saa7164_dump_intfdesc(struct saa7164_dev *dev)
548{
549 dprintk(1, "@0x%p intfdesc "
207b42c4
ST
550 "sizeof(tmComResInterfaceDescr_t) = %d bytes\n",
551 &dev->intfdesc, (u32)sizeof(tmComResInterfaceDescr_t));
443c1228
ST
552
553 dprintk(1, " .bLength = 0x%x\n", dev->intfdesc.bLength);
554 dprintk(1, " .bDescriptorType = 0x%x\n", dev->intfdesc.bDescriptorType);
555 dprintk(1, " .bDescriptorSubtype = 0x%x\n",
556 dev->intfdesc.bDescriptorSubtype);
557
558 dprintk(1, " .bFlags = 0x%x\n", dev->intfdesc.bFlags);
559 dprintk(1, " .bInterfaceType = 0x%x\n", dev->intfdesc.bInterfaceType);
560 dprintk(1, " .bInterfaceId = 0x%x\n", dev->intfdesc.bInterfaceId);
561 dprintk(1, " .bBaseInterface = 0x%x\n", dev->intfdesc.bBaseInterface);
562 dprintk(1, " .bInterruptId = 0x%x\n", dev->intfdesc.bInterruptId);
563 dprintk(1, " .bDebugInterruptId = 0x%x\n",
564 dev->intfdesc.bDebugInterruptId);
565
566 dprintk(1, " .BARLocation = 0x%x\n", dev->intfdesc.BARLocation);
567}
568
569static void saa7164_dump_busdesc(struct saa7164_dev *dev)
570{
207b42c4
ST
571 dprintk(1, "@0x%p busdesc sizeof(tmComResBusDescr_t) = %d bytes\n",
572 &dev->busdesc, (u32)sizeof(tmComResBusDescr_t));
443c1228
ST
573
574 dprintk(1, " .CommandRing = 0x%016Lx\n", dev->busdesc.CommandRing);
575 dprintk(1, " .ResponseRing = 0x%016Lx\n", dev->busdesc.ResponseRing);
576 dprintk(1, " .CommandWrite = 0x%x\n", dev->busdesc.CommandWrite);
577 dprintk(1, " .CommandRead = 0x%x\n", dev->busdesc.CommandRead);
578 dprintk(1, " .ResponseWrite = 0x%x\n", dev->busdesc.ResponseWrite);
579 dprintk(1, " .ResponseRead = 0x%x\n", dev->busdesc.ResponseRead);
580}
581
582/* Much of the hardware configuration and PCI registers are configured
583 * dynamically depending on firmware. We have to cache some initial
584 * structures then use these to locate other important structures
585 * from PCI space.
586 */
587static void saa7164_get_descriptors(struct saa7164_dev *dev)
588{
589 memcpy(&dev->hwdesc, dev->bmmio, sizeof(tmComResHWDescr_t));
590 memcpy(&dev->intfdesc, dev->bmmio + sizeof(tmComResHWDescr_t),
591 sizeof(tmComResInterfaceDescr_t));
592 memcpy(&dev->busdesc, dev->bmmio + dev->intfdesc.BARLocation,
593 sizeof(tmComResBusDescr_t));
594
595 if (dev->hwdesc.bLength != sizeof(tmComResHWDescr_t)) {
596 printk(KERN_ERR "Structure tmComResHWDescr_t is mangled\n");
207b42c4
ST
597 printk(KERN_ERR "Need %x got %d\n", dev->hwdesc.bLength,
598 (u32)sizeof(tmComResHWDescr_t));
443c1228
ST
599 } else
600 saa7164_dump_hwdesc(dev);
601
602 if (dev->intfdesc.bLength != sizeof(tmComResInterfaceDescr_t)) {
603 printk(KERN_ERR "struct tmComResInterfaceDescr_t is mangled\n");
207b42c4
ST
604 printk(KERN_ERR "Need %x got %d\n", dev->intfdesc.bLength,
605 (u32)sizeof(tmComResInterfaceDescr_t));
443c1228
ST
606 } else
607 saa7164_dump_intfdesc(dev);
608
609 saa7164_dump_busdesc(dev);
610}
611
612static int saa7164_pci_quirks(struct saa7164_dev *dev)
613{
614 return 0;
615}
616
617static int get_resources(struct saa7164_dev *dev)
618{
619 if (request_mem_region(pci_resource_start(dev->pci, 0),
620 pci_resource_len(dev->pci, 0), dev->name)) {
621
622 if (request_mem_region(pci_resource_start(dev->pci, 2),
623 pci_resource_len(dev->pci, 2), dev->name))
624 return 0;
625 }
626
627 printk(KERN_ERR "%s: can't get MMIO memory @ 0x%llx or 0x%llx\n",
628 dev->name,
629 (u64)pci_resource_start(dev->pci, 0),
630 (u64)pci_resource_start(dev->pci, 2));
631
632 return -EBUSY;
633}
634
7615e434
ST
635static int saa7164_port_init(struct saa7164_dev *dev, int portnr)
636{
637 struct saa7164_port *port = 0;
638
639 if ((portnr < 0) || (portnr >= SAA7164_MAX_PORTS))
640 BUG();
641
642 port = &dev->ports[ portnr ];
643
644 port->dev = dev;
645 port->nr = portnr;
646
647 if ((portnr == SAA7164_PORT_TS1) || (portnr == SAA7164_PORT_TS2))
648 port->type = SAA7164_MPEG_DVB;
649 else
650 if ((portnr == SAA7164_PORT_ENC1) || (portnr == SAA7164_PORT_ENC2))
651 port->type = SAA7164_MPEG_ENCODER;
652 else
653 BUG();
654
655 /* Init all the critical resources */
656 mutex_init(&port->dvb.lock);
657 INIT_LIST_HEAD(&port->dmaqueue.list);
658 mutex_init(&port->dmaqueue_lock);
659
660 INIT_LIST_HEAD(&port->list_buf_used.list);
661 INIT_LIST_HEAD(&port->list_buf_free.list);
662 init_waitqueue_head(&port->wait_read);
91d80189
ST
663
664 /* We need a deferred interrupt handler for cmd handling */
665 INIT_WORK(&port->workenc, saa7164_work_enchandler);
666
667 saa7164_histogram_reset(&port->irq_interval, "irq intervals");
668 saa7164_histogram_reset(&port->svc_interval, "deferred intervals");
669 saa7164_histogram_reset(&port->irq_svc_interval,
670 "irq to deferred intervals");
671
7615e434
ST
672 return 0;
673}
674
443c1228
ST
675static int saa7164_dev_setup(struct saa7164_dev *dev)
676{
677 int i;
678
679 mutex_init(&dev->lock);
680 atomic_inc(&dev->refcount);
681 dev->nr = saa7164_devcount++;
682
683 sprintf(dev->name, "saa7164[%d]", dev->nr);
684
685 mutex_lock(&devlist);
686 list_add_tail(&dev->devlist, &saa7164_devlist);
687 mutex_unlock(&devlist);
688
689 /* board config */
690 dev->board = UNSET;
691 if (card[dev->nr] < saa7164_bcount)
692 dev->board = card[dev->nr];
693
694 for (i = 0; UNSET == dev->board && i < saa7164_idcount; i++)
695 if (dev->pci->subsystem_vendor == saa7164_subids[i].subvendor &&
696 dev->pci->subsystem_device ==
697 saa7164_subids[i].subdevice)
698 dev->board = saa7164_subids[i].card;
699
700 if (UNSET == dev->board) {
701 dev->board = SAA7164_BOARD_UNKNOWN;
702 saa7164_card_list(dev);
703 }
704
705 dev->pci_bus = dev->pci->bus->number;
706 dev->pci_slot = PCI_SLOT(dev->pci->devfn);
707
708 /* I2C Defaults / setup */
709 dev->i2c_bus[0].dev = dev;
710 dev->i2c_bus[0].nr = 0;
711 dev->i2c_bus[1].dev = dev;
712 dev->i2c_bus[1].nr = 1;
713 dev->i2c_bus[2].dev = dev;
714 dev->i2c_bus[2].nr = 2;
715
7615e434
ST
716 /* Transport + Encoder ports 1, 2, 3, 4 - Defaults / setup */
717 saa7164_port_init(dev, SAA7164_PORT_TS1);
718 saa7164_port_init(dev, SAA7164_PORT_TS2);
719 saa7164_port_init(dev, SAA7164_PORT_ENC1);
720 saa7164_port_init(dev, SAA7164_PORT_ENC2);
443c1228
ST
721
722 if (get_resources(dev) < 0) {
723 printk(KERN_ERR "CORE %s No more PCIe resources for "
724 "subsystem: %04x:%04x\n",
725 dev->name, dev->pci->subsystem_vendor,
726 dev->pci->subsystem_device);
727
728 saa7164_devcount--;
729 return -ENODEV;
730 }
731
732 /* PCI/e allocations */
733 dev->lmmio = ioremap(pci_resource_start(dev->pci, 0),
734 pci_resource_len(dev->pci, 0));
735
736 dev->lmmio2 = ioremap(pci_resource_start(dev->pci, 2),
737 pci_resource_len(dev->pci, 2));
738
443c1228
ST
739 dev->bmmio = (u8 __iomem *)dev->lmmio;
740 dev->bmmio2 = (u8 __iomem *)dev->lmmio2;
443c1228 741
1a6450d4
ST
742 /* Inerrupt and ack register locations offset of bmmio */
743 dev->int_status = 0x183000 + 0xf80;
744 dev->int_ack = 0x183000 + 0xf90;
443c1228
ST
745
746 printk(KERN_INFO
747 "CORE %s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
748 dev->name, dev->pci->subsystem_vendor,
749 dev->pci->subsystem_device, saa7164_boards[dev->board].name,
750 dev->board, card[dev->nr] == dev->board ?
751 "insmod option" : "autodetected");
752
753 saa7164_pci_quirks(dev);
754
755 return 0;
756}
757
758static void saa7164_dev_unregister(struct saa7164_dev *dev)
759{
760 dprintk(1, "%s()\n", __func__);
761
762 release_mem_region(pci_resource_start(dev->pci, 0),
763 pci_resource_len(dev->pci, 0));
764
765 release_mem_region(pci_resource_start(dev->pci, 2),
766 pci_resource_len(dev->pci, 2));
767
768 if (!atomic_dec_and_test(&dev->refcount))
769 return;
770
771 iounmap(dev->lmmio);
772 iounmap(dev->lmmio2);
773
774 return;
775}
776
777static int __devinit saa7164_initdev(struct pci_dev *pci_dev,
778 const struct pci_device_id *pci_id)
779{
780 struct saa7164_dev *dev;
781 int err, i;
782 u32 version;
783
784 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
785 if (NULL == dev)
786 return -ENOMEM;
787
788 /* pci init */
789 dev->pci = pci_dev;
790 if (pci_enable_device(pci_dev)) {
791 err = -EIO;
792 goto fail_free;
793 }
794
795 if (saa7164_dev_setup(dev) < 0) {
796 err = -EINVAL;
797 goto fail_free;
798 }
799
800 /* print pci info */
801 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
802 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
803 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
804 "latency: %d, mmio: 0x%llx\n", dev->name,
805 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
806 dev->pci_lat,
807 (unsigned long long)pci_resource_start(pci_dev, 0));
808
809 pci_set_master(pci_dev);
810 /* TODO */
811 if (!pci_dma_supported(pci_dev, 0xffffffff)) {
812 printk("%s/0: Oops: no 32bit PCI DMA ???\n", dev->name);
813 err = -EIO;
814 goto fail_irq;
815 }
816
817 err = request_irq(pci_dev->irq, saa7164_irq,
818 IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
819 if (err < 0) {
820 printk(KERN_ERR "%s: can't get IRQ %d\n", dev->name,
821 pci_dev->irq);
822 err = -EIO;
823 goto fail_irq;
824 }
825
826 pci_set_drvdata(pci_dev, dev);
827
443c1228
ST
828 /* Init the internal command list */
829 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
830 dev->cmds[i].seqno = i;
831 dev->cmds[i].inuse = 0;
832 mutex_init(&dev->cmds[i].lock);
833 init_waitqueue_head(&dev->cmds[i].wait);
834 }
835
836 /* We need a deferred interrupt handler for cmd handling */
837 INIT_WORK(&dev->workcmd, saa7164_work_cmdhandler);
838
839 /* Only load the firmware if we know the board */
840 if (dev->board != SAA7164_BOARD_UNKNOWN) {
841
842 err = saa7164_downloadfirmware(dev);
843 if (err < 0) {
844 printk(KERN_ERR
50bcb4ae
ST
845 "Failed to boot firmware, no features "
846 "registered\n");
847 goto fail_fw;
443c1228
ST
848 }
849
850 saa7164_get_descriptors(dev);
851 saa7164_dumpregs(dev, 0);
852 saa7164_getcurrentfirmwareversion(dev);
853 saa7164_getfirmwarestatus(dev);
854 err = saa7164_bus_setup(dev);
855 if (err < 0)
856 printk(KERN_ERR
857 "Failed to setup the bus, will continue\n");
858 saa7164_bus_dump(dev);
859
860 /* Ping the running firmware via the command bus and get the
861 * firmware version, this checks the bus is running OK.
862 */
863 version = 0;
864 if (saa7164_api_get_fw_version(dev, &version) == SAA_OK)
865 dprintk(1, "Bus is operating correctly using "
866 "version %d.%d.%d.%d (0x%x)\n",
867 (version & 0x0000fc00) >> 10,
868 (version & 0x000003e0) >> 5,
869 (version & 0x0000001f),
870 (version & 0xffff0000) >> 16,
871 version);
872 else
873 printk(KERN_ERR
874 "Failed to communicate with the firmware\n");
875
876 /* Bring up the I2C buses */
877 saa7164_i2c_register(&dev->i2c_bus[0]);
878 saa7164_i2c_register(&dev->i2c_bus[1]);
879 saa7164_i2c_register(&dev->i2c_bus[2]);
880 saa7164_gpio_setup(dev);
881 saa7164_card_setup(dev);
882
883
884 /* Parse the dynamic device configuration, find various
885 * media endpoints (MPEG, WMV, PS, TS) and cache their
886 * configuration details into the driver, so we can
887 * reference them later during simething_register() func,
888 * interrupt handlers, deferred work handlers etc.
889 */
890 saa7164_api_enum_subdevs(dev);
891
443c1228
ST
892 /* Begin to create the video sub-systems and register funcs */
893 if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB) {
7615e434 894 if (saa7164_dvb_register(&dev->ports[ SAA7164_PORT_TS1 ]) < 0) {
443c1228
ST
895 printk(KERN_ERR "%s() Failed to register "
896 "dvb adapters on porta\n",
897 __func__);
898 }
899 }
900
901 if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB) {
7615e434 902 if (saa7164_dvb_register(&dev->ports[ SAA7164_PORT_TS2 ]) < 0) {
443c1228
ST
903 printk(KERN_ERR"%s() Failed to register "
904 "dvb adapters on portb\n",
905 __func__);
906 }
907 }
908
7615e434
ST
909 if (saa7164_boards[dev->board].portc == SAA7164_MPEG_ENCODER) {
910 if (saa7164_encoder_register(&dev->ports[ SAA7164_PORT_ENC1 ]) < 0) {
911 printk(KERN_ERR"%s() Failed to register "
912 "mpeg encoder\n", __func__);
913 }
914 }
915
916 if (saa7164_boards[dev->board].portd == SAA7164_MPEG_ENCODER) {
917 if (saa7164_encoder_register(&dev->ports[ SAA7164_PORT_ENC2 ]) < 0) {
918 printk(KERN_ERR"%s() Failed to register "
919 "mpeg encoder\n", __func__);
920 }
921 }
922
443c1228
ST
923 } /* != BOARD_UNKNOWN */
924 else
925 printk(KERN_ERR "%s() Unsupported board detected, "
926 "registering without firmware\n", __func__);
927
b1912a85 928 dprintk(1, "%s() parameter debug = %d\n", __func__, saa_debug);
2ceae8fd
ST
929 dprintk(1, "%s() parameter waitsecs = %d\n", __func__, waitsecs);
930
50bcb4ae 931fail_fw:
443c1228
ST
932 return 0;
933
934fail_irq:
935 saa7164_dev_unregister(dev);
936fail_free:
937 kfree(dev);
938 return err;
939}
940
941static void saa7164_shutdown(struct saa7164_dev *dev)
942{
943 dprintk(1, "%s()\n", __func__);
944}
945
946static void __devexit saa7164_finidev(struct pci_dev *pci_dev)
947{
948 struct saa7164_dev *dev = pci_get_drvdata(pci_dev);
949
91d80189
ST
950 saa7164_histogram_print(&dev->ports[ SAA7164_PORT_ENC1 ],
951 &dev->ports[ SAA7164_PORT_ENC1 ].irq_interval);
952 saa7164_histogram_print(&dev->ports[ SAA7164_PORT_ENC1 ],
953 &dev->ports[ SAA7164_PORT_ENC1 ].svc_interval);
954 saa7164_histogram_print(&dev->ports[ SAA7164_PORT_ENC1 ],
955 &dev->ports[ SAA7164_PORT_ENC1 ].irq_svc_interval);
956
443c1228
ST
957 saa7164_shutdown(dev);
958
959 if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB)
7615e434 960 saa7164_dvb_unregister(&dev->ports[ SAA7164_PORT_TS1 ]);
443c1228
ST
961
962 if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB)
7615e434
ST
963 saa7164_dvb_unregister(&dev->ports[ SAA7164_PORT_TS2 ]);
964
965 if (saa7164_boards[dev->board].portc == SAA7164_MPEG_ENCODER)
966 saa7164_encoder_unregister(&dev->ports[ SAA7164_PORT_ENC1 ]);
967
968 if (saa7164_boards[dev->board].portd == SAA7164_MPEG_ENCODER)
969 saa7164_encoder_unregister(&dev->ports[ SAA7164_PORT_ENC2 ]);
443c1228
ST
970
971 saa7164_i2c_unregister(&dev->i2c_bus[0]);
972 saa7164_i2c_unregister(&dev->i2c_bus[1]);
973 saa7164_i2c_unregister(&dev->i2c_bus[2]);
974
975 pci_disable_device(pci_dev);
976
977 /* unregister stuff */
978 free_irq(pci_dev->irq, dev);
979 pci_set_drvdata(pci_dev, NULL);
980
981 mutex_lock(&devlist);
982 list_del(&dev->devlist);
983 mutex_unlock(&devlist);
984
985 saa7164_dev_unregister(dev);
986 kfree(dev);
987}
988
989static struct pci_device_id saa7164_pci_tbl[] = {
990 {
991 /* SAA7164 */
992 .vendor = 0x1131,
993 .device = 0x7164,
994 .subvendor = PCI_ANY_ID,
995 .subdevice = PCI_ANY_ID,
996 }, {
997 /* --- end of list --- */
998 }
999};
1000MODULE_DEVICE_TABLE(pci, saa7164_pci_tbl);
1001
1002static struct pci_driver saa7164_pci_driver = {
1003 .name = "saa7164",
1004 .id_table = saa7164_pci_tbl,
1005 .probe = saa7164_initdev,
1006 .remove = __devexit_p(saa7164_finidev),
1007 /* TODO */
1008 .suspend = NULL,
1009 .resume = NULL,
1010};
1011
9d440a08 1012static int __init saa7164_init(void)
443c1228
ST
1013{
1014 printk(KERN_INFO "saa7164 driver loaded\n");
1015 return pci_register_driver(&saa7164_pci_driver);
1016}
1017
9d440a08 1018static void __exit saa7164_fini(void)
443c1228
ST
1019{
1020 pci_unregister_driver(&saa7164_pci_driver);
1021}
1022
1023module_init(saa7164_init);
1024module_exit(saa7164_fini);
1025
This page took 0.163851 seconds and 5 git commands to generate.