[media] saa7164: allow the number of encoder buffers to be user configurable
[deliverable/linux.git] / drivers / media / video / saa7164 / saa7164-core.c
1 /*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
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
35 MODULE_DESCRIPTION("Driver for NXP SAA7164 based TV cards");
36 MODULE_AUTHOR("Steven Toth <stoth@kernellabs.com>");
37 MODULE_LICENSE("GPL");
38
39 /*
40 1 Basic
41 2
42 4 i2c
43 8 api
44 16 cmd
45 32 bus
46 */
47
48 unsigned int saa_debug;
49 module_param_named(debug, saa_debug, int, 0644);
50 MODULE_PARM_DESC(debug, "enable debug messages");
51
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");
55
56 unsigned int waitsecs = 10;
57 module_param(waitsecs, int, 0644);
58 MODULE_PARM_DESC(waitsecs, "timeout on firmware messages");
59
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");
63
64 unsigned int print_histogram = 64;
65 module_param(print_histogram, int, 0644);
66 MODULE_PARM_DESC(print_histogram, "print histogram values once");
67
68 static unsigned int saa7164_devcount;
69
70 static DEFINE_MUTEX(devlist);
71 LIST_HEAD(saa7164_devlist);
72
73 #define INT_SIZE 16
74
75 static void saa7164_ts_verifier(struct saa7164_buffer *buf)
76 {
77 struct saa7164_port *port = buf->port;
78 struct saa7164_dev *dev = port->dev;
79 u32 i;
80 u8 tmp, cc, a;
81 u8 *bufcpu = (u8 *)buf->cpu;
82
83 port->sync_errors = 0;
84 port->v_cc_errors = 0;
85 port->a_cc_errors = 0;
86
87 for (i = 0; i < buf->actual_size; i += 188) {
88 if (*(bufcpu + i) != 0x47)
89 port->sync_errors++;
90
91 /* Query pid lower 8 bits */
92 tmp = *(bufcpu + i + 2);
93 cc = *(bufcpu + i + 3) & 0x0f;
94
95 if (tmp == 0xf1) {
96 a = ((port->last_v_cc + 1) & 0x0f);
97 if (a != cc) {
98 printk(KERN_ERR "video cc last = %x current = %x i = %d\n", port->last_v_cc, cc, i);
99 port->v_cc_errors++;
100 }
101
102 port->last_v_cc = cc;
103 } else
104 if (tmp == 0xf2) {
105 a = ((port->last_a_cc + 1) & 0x0f);
106 if (a != cc) {
107 printk(KERN_ERR "audio cc last = %x current = %x i = %d\n", port->last_a_cc, cc, i);
108 port->a_cc_errors++;
109 }
110
111 port->last_a_cc = cc;
112 }
113
114 }
115
116 if (port->v_cc_errors)
117 printk(KERN_ERR "video pid cc, %d errors\n", port->v_cc_errors);
118
119 if (port->a_cc_errors)
120 printk(KERN_ERR "audio pid cc, %d errors\n", port->a_cc_errors);
121
122 if (port->sync_errors)
123 printk(KERN_ERR "sync_errors = %d\n", port->sync_errors);
124 }
125
126 static void saa7164_histogram_reset(struct saa7164_histogram *hg, char *name)
127 {
128 int i;
129
130 memset(hg, 0, sizeof(struct saa7164_histogram));
131 strcpy(hg->name, name);
132
133 /* First 30ms x 1ms */
134 for (i = 0; i < 30; i++) {
135 hg->counter1[0 + i].val = i;
136 }
137
138 /* 30 - 200ms x 10ms */
139 for (i = 0; i < 18; i++) {
140 hg->counter1[30 + i].val = 30 + (i * 10);
141 }
142
143 /* 200 - 2000ms x 100ms */
144 for (i = 0; i < 15; i++) {
145 hg->counter1[48 + i].val = 200 + (i * 100);
146 }
147
148 /* Catch all massive value (1hr) */
149 hg->counter1[63].val = 3600000;
150 }
151
152 static void saa7164_histogram_update(struct saa7164_histogram *hg, u32 val)
153 {
154 int i;
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;
159 break;
160 }
161 }
162 }
163
164 static void saa7164_histogram_print(struct saa7164_port *port,
165 struct saa7164_histogram *hg)
166 {
167 struct saa7164_dev *dev = port->dev;
168 u32 entries = 0;
169 int i;
170
171 printk(KERN_ERR "Histogram named %s\n", hg->name);
172 for (i = 0; i < 64; i++ ) {
173 if (hg->counter1[i].count == 0)
174 continue;
175
176 printk(KERN_ERR " %4d %12d %Ld\n",
177 hg->counter1[i].val,
178 hg->counter1[i].count,
179 hg->counter1[i].update_time);
180
181 entries++;
182 }
183 printk(KERN_ERR "Total: %d\n", entries);
184 }
185
186 static void saa7164_work_enchandler(struct work_struct *w)
187 {
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;
194 int wp, rp, i = 0;
195
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;
202
203
204 port->last_svc_msecs_diff = port->last_svc_msecs -
205 port->last_svc_msecs_diff;
206
207 saa7164_histogram_update(&port->svc_interval,
208 port->last_svc_msecs_diff);
209
210 port->last_irq_svc_msecs_diff = port->last_svc_msecs -
211 port->last_irq_msecs;
212
213 saa7164_histogram_update(&port->irq_svc_interval,
214 port->last_irq_svc_msecs_diff);
215
216 dprintk(DBGLVL_IRQ,
217 "%s() %Ldms elapsed irq->deferred %Ldms wp: %d rp: %d\n",
218 __func__,
219 port->last_svc_msecs_diff,
220 port->last_irq_svc_msecs_diff,
221 port->last_svc_wp,
222 port->last_svc_rp
223 );
224
225 if ((rp < 0) || (rp > 7)) {
226 printk(KERN_ERR "%s() illegal rp count %d\n", __func__, rp);
227 return;
228 }
229
230 mutex_lock(&port->dmaqueue_lock);
231
232 list_for_each_safe(c, n, &port->dmaqueue.list) {
233
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",
237 __func__, i);
238 break;
239 }
240
241 if (buf->idx == rp) {
242 /* Found the buffer, deal with it */
243 dprintk(DBGLVL_IRQ, "%s() wp: %d processing: %d\n",
244 __func__, wp, rp);
245
246 /* Validate the incoming buffer content */
247 if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
248 saa7164_ts_verifier(buf);
249
250 /* find a free user buffer and clone to it */
251 if (!list_empty(&port->list_buf_free.list)) {
252
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);
256
257 if (ubuf->actual_size == buf->actual_size)
258 memcpy(ubuf->data, buf->cpu,
259 ubuf->actual_size);
260
261 /* Requeue the buffer on the free list */
262 ubuf->pos = 0;
263
264 list_move_tail(&ubuf->list,
265 &port->list_buf_used.list);
266
267 /* Flag any userland waiters */
268 wake_up_interruptible(&port->wait_read);
269
270 } else
271 printk(KERN_ERR "encirq no free buffers, increase param encoder_buffers\n");
272
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);
277
278 break;
279 }
280
281 }
282 mutex_unlock(&port->dmaqueue_lock);
283
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;
289 }
290 }
291
292 static void saa7164_work_cmdhandler(struct work_struct *w)
293 {
294 struct saa7164_dev *dev = container_of(w, struct saa7164_dev, workcmd);
295
296 /* Wake up any complete commands */
297 saa7164_irq_dequeue(dev);
298 }
299
300 static void saa7164_buffer_deliver(struct saa7164_buffer *buf)
301 {
302 struct saa7164_port *port = buf->port;
303
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);
307
308 }
309
310 static irqreturn_t saa7164_irq_encoder(struct saa7164_port *port)
311 {
312 struct saa7164_dev *dev = port->dev;
313 int wp, rp;
314
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);
319 return 0;
320 }
321
322 /* Find the previous buffer to the current write point */
323 if (wp == 0)
324 rp = 7;
325 else
326 rp = wp - 1;
327
328 if ((rp < 0) || (rp > 7)) {
329 printk(KERN_ERR "%s() illegal rp count %d\n", __func__, rp);
330 return 0;
331 }
332
333 if (rp != ((port->last_irq_rp + 1) % 8)) {
334 printk(KERN_ERR "%s() Multiple bufs on interrupt, port %p\n",
335 __func__, port);
336 }
337
338 /* Store old time */
339 port->last_irq_msecs_diff = port->last_irq_msecs;
340
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;
345
346 /* Calculate stats */
347 port->last_irq_msecs_diff = port->last_irq_msecs -
348 port->last_irq_msecs_diff;
349
350 saa7164_histogram_update(&port->irq_interval,
351 port->last_irq_msecs_diff);
352
353 dprintk(DBGLVL_IRQ, "%s() %Ldms elapsed wp: %d rp: %d\n",
354 __func__,
355 port->last_irq_msecs_diff,
356 port->last_irq_wp,
357 port->last_irq_rp
358 );
359
360 schedule_work(&port->workenc);
361
362 return 0;
363 }
364
365 static irqreturn_t saa7164_irq_ts(struct saa7164_port *port)
366 {
367 struct saa7164_dev *dev = port->dev;
368 struct saa7164_buffer *buf;
369 struct list_head *c, *n;
370 int wp, i = 0, rp;
371
372 /* Find the current write point from the hardware */
373 wp = saa7164_readl(port->bufcounter);
374 if (wp > (port->hwcfg.buffercount - 1))
375 BUG();
376
377 /* Find the previous buffer to the current write point */
378 if (wp == 0)
379 rp = 7;
380 else
381 rp = wp - 1;
382
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)
388 BUG();
389
390 if (buf->idx == rp) {
391 /* Found the buffer, deal with it */
392 dprintk(DBGLVL_IRQ, "%s() wp: %d processing: %d\n",
393 __func__, wp, rp);
394 saa7164_buffer_deliver(buf);
395 break;
396 }
397
398 }
399 return 0;
400 }
401
402 /* Primary IRQ handler and dispatch mechanism */
403 static irqreturn_t saa7164_irq(int irq, void *dev_id)
404 {
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 ];
410
411 u32 intid, intstat[INT_SIZE/4];
412 int i, handled = 0, bit;
413
414 if (dev == 0) {
415 printk(KERN_ERR "%s() No device specified\n", __func__);
416 handled = 0;
417 goto out;
418 }
419
420 /* Check that the hardware is accessable. If the status bytes are
421 * 0xFF then the device is not accessable, the the IRQ belongs
422 * to another driver.
423 * 4 x u32 interrupt registers.
424 */
425 for (i = 0; i < INT_SIZE/4; i++) {
426
427 /* TODO: Convert into saa7164_readl() */
428 /* Read the 4 hardware interrupt registers */
429 intstat[i] = saa7164_readl(dev->int_status + (i * 4));
430
431 if (intstat[i])
432 handled = 1;
433 }
434 if (handled == 0)
435 goto out;
436
437 /* For each of the HW interrupt registers */
438 for (i = 0; i < INT_SIZE/4; i++) {
439
440 if (intstat[i]) {
441 /* Each function of the board has it's own interruptid.
442 * Find the function that triggered then call
443 * it's handler.
444 */
445 for (bit = 0; bit < 32; bit++) {
446
447 if (((intstat[i] >> bit) & 0x00000001) == 0)
448 continue;
449
450 /* Calculate the interrupt id (0x00 to 0x7f) */
451
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) {
457
458 /* Transport path 1 */
459 saa7164_irq_ts(porta);
460
461 } else if (intid == portb->hwcfg.interruptid) {
462
463 /* Transport path 2 */
464 saa7164_irq_ts(portb);
465
466 } else if (intid == portc->hwcfg.interruptid) {
467
468 /* Encoder path 1 */
469 saa7164_irq_encoder(portc);
470
471 } else if (intid == portd->hwcfg.interruptid) {
472
473 /* Encoder path 1 */
474 saa7164_irq_encoder(portd);
475
476 } else {
477 /* Find the function */
478 dprintk(DBGLVL_IRQ,
479 "%s() unhandled interrupt "
480 "reg 0x%x bit 0x%x "
481 "intid = 0x%x\n",
482 __func__, i, bit, intid);
483 }
484 }
485
486 /* Ack it */
487 saa7164_writel(dev->int_ack + (i * 4), intstat[i]);
488
489 }
490 }
491 out:
492 return IRQ_RETVAL(handled);
493 }
494
495 void saa7164_getfirmwarestatus(struct saa7164_dev *dev)
496 {
497 struct saa7164_fw_status *s = &dev->fw_status;
498
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);
506
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);
514 }
515
516 u32 saa7164_getcurrentfirmwareversion(struct saa7164_dev *dev)
517 {
518 u32 reg;
519
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,
524 (reg & 0x0000001f),
525 (reg & 0xffff0000) >> 16,
526 reg);
527
528 return reg;
529 }
530
531 /* TODO: Debugging func, remove */
532 void saa7164_dumphex16(struct saa7164_dev *dev, u8 *buf, int len)
533 {
534 int i;
535
536 printk(KERN_INFO "--------------------> "
537 "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
538
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));
547 }
548
549 /* TODO: Debugging func, remove */
550 void saa7164_dumpregs(struct saa7164_dev *dev, u32 addr)
551 {
552 int i;
553
554 dprintk(1, "--------------------> "
555 "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
556
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)
577 );
578 }
579
580 static void saa7164_dump_hwdesc(struct saa7164_dev *dev)
581 {
582 dprintk(1, "@0x%p hwdesc sizeof(tmComResHWDescr_t) = %d bytes\n",
583 &dev->hwdesc, (u32)sizeof(tmComResHWDescr_t));
584
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);
589
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);
596
597 dprintk(1, " .dwHostMemoryRegion = 0x%x\n",
598 dev->hwdesc.dwHostMemoryRegion);
599
600 dprintk(1, " .dwHostMemoryRegionSize = 0x%x\n",
601 dev->hwdesc.dwHostMemoryRegionSize);
602
603 dprintk(1, " .dwHostHibernatMemRegion = 0x%x\n",
604 dev->hwdesc.dwHostHibernatMemRegion);
605
606 dprintk(1, " .dwHostHibernatMemRegionSize = 0x%x\n",
607 dev->hwdesc.dwHostHibernatMemRegionSize);
608 }
609
610 static void saa7164_dump_intfdesc(struct saa7164_dev *dev)
611 {
612 dprintk(1, "@0x%p intfdesc "
613 "sizeof(tmComResInterfaceDescr_t) = %d bytes\n",
614 &dev->intfdesc, (u32)sizeof(tmComResInterfaceDescr_t));
615
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);
620
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);
628
629 dprintk(1, " .BARLocation = 0x%x\n", dev->intfdesc.BARLocation);
630 }
631
632 static void saa7164_dump_busdesc(struct saa7164_dev *dev)
633 {
634 dprintk(1, "@0x%p busdesc sizeof(tmComResBusDescr_t) = %d bytes\n",
635 &dev->busdesc, (u32)sizeof(tmComResBusDescr_t));
636
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);
643 }
644
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
648 * from PCI space.
649 */
650 static void saa7164_get_descriptors(struct saa7164_dev *dev)
651 {
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));
657
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));
662 } else
663 saa7164_dump_hwdesc(dev);
664
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));
669 } else
670 saa7164_dump_intfdesc(dev);
671
672 saa7164_dump_busdesc(dev);
673 }
674
675 static int saa7164_pci_quirks(struct saa7164_dev *dev)
676 {
677 return 0;
678 }
679
680 static int get_resources(struct saa7164_dev *dev)
681 {
682 if (request_mem_region(pci_resource_start(dev->pci, 0),
683 pci_resource_len(dev->pci, 0), dev->name)) {
684
685 if (request_mem_region(pci_resource_start(dev->pci, 2),
686 pci_resource_len(dev->pci, 2), dev->name))
687 return 0;
688 }
689
690 printk(KERN_ERR "%s: can't get MMIO memory @ 0x%llx or 0x%llx\n",
691 dev->name,
692 (u64)pci_resource_start(dev->pci, 0),
693 (u64)pci_resource_start(dev->pci, 2));
694
695 return -EBUSY;
696 }
697
698 static int saa7164_port_init(struct saa7164_dev *dev, int portnr)
699 {
700 struct saa7164_port *port = 0;
701
702 if ((portnr < 0) || (portnr >= SAA7164_MAX_PORTS))
703 BUG();
704
705 port = &dev->ports[ portnr ];
706
707 port->dev = dev;
708 port->nr = portnr;
709
710 if ((portnr == SAA7164_PORT_TS1) || (portnr == SAA7164_PORT_TS2))
711 port->type = SAA7164_MPEG_DVB;
712 else
713 if ((portnr == SAA7164_PORT_ENC1) || (portnr == SAA7164_PORT_ENC2))
714 port->type = SAA7164_MPEG_ENCODER;
715 else
716 BUG();
717
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);
722
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);
726
727 /* We need a deferred interrupt handler for cmd handling */
728 INIT_WORK(&port->workenc, saa7164_work_enchandler);
729
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");
734
735 return 0;
736 }
737
738 static int saa7164_dev_setup(struct saa7164_dev *dev)
739 {
740 int i;
741
742 mutex_init(&dev->lock);
743 atomic_inc(&dev->refcount);
744 dev->nr = saa7164_devcount++;
745
746 sprintf(dev->name, "saa7164[%d]", dev->nr);
747
748 mutex_lock(&devlist);
749 list_add_tail(&dev->devlist, &saa7164_devlist);
750 mutex_unlock(&devlist);
751
752 /* board config */
753 dev->board = UNSET;
754 if (card[dev->nr] < saa7164_bcount)
755 dev->board = card[dev->nr];
756
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;
762
763 if (UNSET == dev->board) {
764 dev->board = SAA7164_BOARD_UNKNOWN;
765 saa7164_card_list(dev);
766 }
767
768 dev->pci_bus = dev->pci->bus->number;
769 dev->pci_slot = PCI_SLOT(dev->pci->devfn);
770
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;
778
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);
784
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);
790
791 saa7164_devcount--;
792 return -ENODEV;
793 }
794
795 /* PCI/e allocations */
796 dev->lmmio = ioremap(pci_resource_start(dev->pci, 0),
797 pci_resource_len(dev->pci, 0));
798
799 dev->lmmio2 = ioremap(pci_resource_start(dev->pci, 2),
800 pci_resource_len(dev->pci, 2));
801
802 dev->bmmio = (u8 __iomem *)dev->lmmio;
803 dev->bmmio2 = (u8 __iomem *)dev->lmmio2;
804
805 /* Inerrupt and ack register locations offset of bmmio */
806 dev->int_status = 0x183000 + 0xf80;
807 dev->int_ack = 0x183000 + 0xf90;
808
809 printk(KERN_INFO
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");
815
816 saa7164_pci_quirks(dev);
817
818 return 0;
819 }
820
821 static void saa7164_dev_unregister(struct saa7164_dev *dev)
822 {
823 dprintk(1, "%s()\n", __func__);
824
825 release_mem_region(pci_resource_start(dev->pci, 0),
826 pci_resource_len(dev->pci, 0));
827
828 release_mem_region(pci_resource_start(dev->pci, 2),
829 pci_resource_len(dev->pci, 2));
830
831 if (!atomic_dec_and_test(&dev->refcount))
832 return;
833
834 iounmap(dev->lmmio);
835 iounmap(dev->lmmio2);
836
837 return;
838 }
839
840 static int __devinit saa7164_initdev(struct pci_dev *pci_dev,
841 const struct pci_device_id *pci_id)
842 {
843 struct saa7164_dev *dev;
844 int err, i;
845 u32 version;
846
847 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
848 if (NULL == dev)
849 return -ENOMEM;
850
851 /* pci init */
852 dev->pci = pci_dev;
853 if (pci_enable_device(pci_dev)) {
854 err = -EIO;
855 goto fail_free;
856 }
857
858 if (saa7164_dev_setup(dev) < 0) {
859 err = -EINVAL;
860 goto fail_free;
861 }
862
863 /* print pci info */
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,
869 dev->pci_lat,
870 (unsigned long long)pci_resource_start(pci_dev, 0));
871
872 pci_set_master(pci_dev);
873 /* TODO */
874 if (!pci_dma_supported(pci_dev, 0xffffffff)) {
875 printk("%s/0: Oops: no 32bit PCI DMA ???\n", dev->name);
876 err = -EIO;
877 goto fail_irq;
878 }
879
880 err = request_irq(pci_dev->irq, saa7164_irq,
881 IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
882 if (err < 0) {
883 printk(KERN_ERR "%s: can't get IRQ %d\n", dev->name,
884 pci_dev->irq);
885 err = -EIO;
886 goto fail_irq;
887 }
888
889 pci_set_drvdata(pci_dev, dev);
890
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);
897 }
898
899 /* We need a deferred interrupt handler for cmd handling */
900 INIT_WORK(&dev->workcmd, saa7164_work_cmdhandler);
901
902 /* Only load the firmware if we know the board */
903 if (dev->board != SAA7164_BOARD_UNKNOWN) {
904
905 err = saa7164_downloadfirmware(dev);
906 if (err < 0) {
907 printk(KERN_ERR
908 "Failed to boot firmware, no features "
909 "registered\n");
910 goto fail_fw;
911 }
912
913 saa7164_get_descriptors(dev);
914 saa7164_dumpregs(dev, 0);
915 saa7164_getcurrentfirmwareversion(dev);
916 saa7164_getfirmwarestatus(dev);
917 err = saa7164_bus_setup(dev);
918 if (err < 0)
919 printk(KERN_ERR
920 "Failed to setup the bus, will continue\n");
921 saa7164_bus_dump(dev);
922
923 /* Ping the running firmware via the command bus and get the
924 * firmware version, this checks the bus is running OK.
925 */
926 version = 0;
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,
934 version);
935 else
936 printk(KERN_ERR
937 "Failed to communicate with the firmware\n");
938
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);
945
946
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.
952 */
953 saa7164_api_enum_subdevs(dev);
954
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",
960 __func__);
961 }
962 }
963
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",
968 __func__);
969 }
970 }
971
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__);
976 }
977 }
978
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__);
983 }
984 }
985
986 } /* != BOARD_UNKNOWN */
987 else
988 printk(KERN_ERR "%s() Unsupported board detected, "
989 "registering without firmware\n", __func__);
990
991 dprintk(1, "%s() parameter debug = %d\n", __func__, saa_debug);
992 dprintk(1, "%s() parameter waitsecs = %d\n", __func__, waitsecs);
993
994 fail_fw:
995 return 0;
996
997 fail_irq:
998 saa7164_dev_unregister(dev);
999 fail_free:
1000 kfree(dev);
1001 return err;
1002 }
1003
1004 static void saa7164_shutdown(struct saa7164_dev *dev)
1005 {
1006 dprintk(1, "%s()\n", __func__);
1007 }
1008
1009 static void __devexit saa7164_finidev(struct pci_dev *pci_dev)
1010 {
1011 struct saa7164_dev *dev = pci_get_drvdata(pci_dev);
1012
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);
1019
1020 saa7164_shutdown(dev);
1021
1022 if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB)
1023 saa7164_dvb_unregister(&dev->ports[ SAA7164_PORT_TS1 ]);
1024
1025 if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB)
1026 saa7164_dvb_unregister(&dev->ports[ SAA7164_PORT_TS2 ]);
1027
1028 if (saa7164_boards[dev->board].portc == SAA7164_MPEG_ENCODER)
1029 saa7164_encoder_unregister(&dev->ports[ SAA7164_PORT_ENC1 ]);
1030
1031 if (saa7164_boards[dev->board].portd == SAA7164_MPEG_ENCODER)
1032 saa7164_encoder_unregister(&dev->ports[ SAA7164_PORT_ENC2 ]);
1033
1034 saa7164_i2c_unregister(&dev->i2c_bus[0]);
1035 saa7164_i2c_unregister(&dev->i2c_bus[1]);
1036 saa7164_i2c_unregister(&dev->i2c_bus[2]);
1037
1038 pci_disable_device(pci_dev);
1039
1040 /* unregister stuff */
1041 free_irq(pci_dev->irq, dev);
1042 pci_set_drvdata(pci_dev, NULL);
1043
1044 mutex_lock(&devlist);
1045 list_del(&dev->devlist);
1046 mutex_unlock(&devlist);
1047
1048 saa7164_dev_unregister(dev);
1049 kfree(dev);
1050 }
1051
1052 static struct pci_device_id saa7164_pci_tbl[] = {
1053 {
1054 /* SAA7164 */
1055 .vendor = 0x1131,
1056 .device = 0x7164,
1057 .subvendor = PCI_ANY_ID,
1058 .subdevice = PCI_ANY_ID,
1059 }, {
1060 /* --- end of list --- */
1061 }
1062 };
1063 MODULE_DEVICE_TABLE(pci, saa7164_pci_tbl);
1064
1065 static struct pci_driver saa7164_pci_driver = {
1066 .name = "saa7164",
1067 .id_table = saa7164_pci_tbl,
1068 .probe = saa7164_initdev,
1069 .remove = __devexit_p(saa7164_finidev),
1070 /* TODO */
1071 .suspend = NULL,
1072 .resume = NULL,
1073 };
1074
1075 static int __init saa7164_init(void)
1076 {
1077 printk(KERN_INFO "saa7164 driver loaded\n");
1078 return pci_register_driver(&saa7164_pci_driver);
1079 }
1080
1081 static void __exit saa7164_fini(void)
1082 {
1083 pci_unregister_driver(&saa7164_pci_driver);
1084 }
1085
1086 module_init(saa7164_init);
1087 module_exit(saa7164_fini);
1088
This page took 0.254989 seconds and 6 git commands to generate.