linux/drivers/media/pci/saa7134/saa7134-core.c
<<
>>
Prefs
   1/*
   2 *
   3 * device driver for philips saa7134 based TV cards
   4 * driver core
   5 *
   6 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
   7 *
   8 *  This program is free software; you can redistribute it and/or modify
   9 *  it under the terms of the GNU General Public License as published by
  10 *  the Free Software Foundation; either version 2 of the License, or
  11 *  (at your option) any later version.
  12 *
  13 *  This program is distributed in the hope that it will be useful,
  14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 *  GNU General Public License for more details.
  17 */
  18
  19#include "saa7134.h"
  20#include "saa7134-reg.h"
  21
  22#include <linux/init.h>
  23#include <linux/list.h>
  24#include <linux/module.h>
  25#include <linux/kernel.h>
  26#include <linux/slab.h>
  27#include <linux/kmod.h>
  28#include <linux/sound.h>
  29#include <linux/interrupt.h>
  30#include <linux/delay.h>
  31#include <linux/mutex.h>
  32#include <linux/dma-mapping.h>
  33#include <linux/pm.h>
  34
  35MODULE_DESCRIPTION("v4l2 driver module for saa7130/34 based TV cards");
  36MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  37MODULE_LICENSE("GPL");
  38MODULE_VERSION(SAA7134_VERSION);
  39
  40
  41/* ------------------------------------------------------------------ */
  42
  43static unsigned int irq_debug;
  44module_param(irq_debug, int, 0644);
  45MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
  46
  47static unsigned int core_debug;
  48module_param(core_debug, int, 0644);
  49MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
  50
  51static unsigned int gpio_tracking;
  52module_param(gpio_tracking, int, 0644);
  53MODULE_PARM_DESC(gpio_tracking,"enable debug messages [gpio]");
  54
  55static unsigned int alsa = 1;
  56module_param(alsa, int, 0644);
  57MODULE_PARM_DESC(alsa,"enable/disable ALSA DMA sound [dmasound]");
  58
  59static unsigned int latency = UNSET;
  60module_param(latency, int, 0444);
  61MODULE_PARM_DESC(latency,"pci latency timer");
  62
  63int saa7134_no_overlay=-1;
  64module_param_named(no_overlay, saa7134_no_overlay, int, 0444);
  65MODULE_PARM_DESC(no_overlay, "allow override overlay default (0 disables, 1 enables) [some VIA/SIS chipsets are known to have problem with overlay]");
  66
  67bool saa7134_userptr;
  68module_param(saa7134_userptr, bool, 0644);
  69MODULE_PARM_DESC(saa7134_userptr, "enable page-aligned userptr support");
  70
  71static unsigned int video_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
  72static unsigned int vbi_nr[]   = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
  73static unsigned int radio_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
  74static unsigned int tuner[]    = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
  75static unsigned int card[]     = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
  76
  77
  78module_param_array(video_nr, int, NULL, 0444);
  79module_param_array(vbi_nr,   int, NULL, 0444);
  80module_param_array(radio_nr, int, NULL, 0444);
  81module_param_array(tuner,    int, NULL, 0444);
  82module_param_array(card,     int, NULL, 0444);
  83
  84MODULE_PARM_DESC(video_nr, "video device number");
  85MODULE_PARM_DESC(vbi_nr,   "vbi device number");
  86MODULE_PARM_DESC(radio_nr, "radio device number");
  87MODULE_PARM_DESC(tuner,    "tuner type");
  88MODULE_PARM_DESC(card,     "card type");
  89
  90DEFINE_MUTEX(saa7134_devlist_lock);
  91EXPORT_SYMBOL(saa7134_devlist_lock);
  92LIST_HEAD(saa7134_devlist);
  93EXPORT_SYMBOL(saa7134_devlist);
  94static LIST_HEAD(mops_list);
  95static unsigned int saa7134_devcount;
  96
  97int (*saa7134_dmasound_init)(struct saa7134_dev *dev);
  98int (*saa7134_dmasound_exit)(struct saa7134_dev *dev);
  99
 100#define core_dbg(fmt, arg...) do { \
 101        if (core_debug) \
 102                printk(KERN_DEBUG pr_fmt("core: " fmt), ## arg); \
 103        } while (0)
 104
 105#define irq_dbg(level, fmt, arg...)  do {\
 106        if (irq_debug > level) \
 107                printk(KERN_DEBUG pr_fmt("irq: " fmt), ## arg); \
 108        } while (0)
 109
 110void saa7134_track_gpio(struct saa7134_dev *dev, const char *msg)
 111{
 112        unsigned long mode,status;
 113
 114        if (!gpio_tracking)
 115                return;
 116        /* rising SAA7134_GPIO_GPRESCAN reads the status */
 117        saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,0);
 118        saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,SAA7134_GPIO_GPRESCAN);
 119        mode   = saa_readl(SAA7134_GPIO_GPMODE0   >> 2) & 0xfffffff;
 120        status = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & 0xfffffff;
 121        core_dbg("%s: gpio: mode=0x%07lx in=0x%07lx out=0x%07lx [%s]\n",
 122               dev->name, mode, (~mode) & status, mode & status, msg);
 123}
 124
 125void saa7134_set_gpio(struct saa7134_dev *dev, int bit_no, int value)
 126{
 127        u32 index, bitval;
 128
 129        index = 1 << bit_no;
 130        switch (value) {
 131        case 0: /* static value */
 132        case 1:
 133                core_dbg("setting GPIO%d to static %d\n", bit_no, value);
 134                /* turn sync mode off if necessary */
 135                if (index & 0x00c00000)
 136                        saa_andorb(SAA7134_VIDEO_PORT_CTRL6, 0x0f, 0x00);
 137                if (value)
 138                        bitval = index;
 139                else
 140                        bitval = 0;
 141                saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, index);
 142                saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, index, bitval);
 143                break;
 144        case 3: /* tristate */
 145                core_dbg("setting GPIO%d to tristate\n", bit_no);
 146                saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, 0);
 147                break;
 148        }
 149}
 150
 151/* ------------------------------------------------------------------ */
 152
 153
 154/* ----------------------------------------------------------- */
 155/* delayed request_module                                      */
 156
 157#if defined(CONFIG_MODULES) && defined(MODULE)
 158
 159static void request_module_async(struct work_struct *work){
 160        struct saa7134_dev* dev = container_of(work, struct saa7134_dev, request_module_wk);
 161        if (card_is_empress(dev))
 162                request_module("saa7134-empress");
 163        if (card_is_dvb(dev))
 164                request_module("saa7134-dvb");
 165        if (card_is_go7007(dev))
 166                request_module("saa7134-go7007");
 167        if (alsa) {
 168                if (dev->pci->device != PCI_DEVICE_ID_PHILIPS_SAA7130)
 169                        request_module("saa7134-alsa");
 170        }
 171}
 172
 173static void request_submodules(struct saa7134_dev *dev)
 174{
 175        INIT_WORK(&dev->request_module_wk, request_module_async);
 176        schedule_work(&dev->request_module_wk);
 177}
 178
 179static void flush_request_submodules(struct saa7134_dev *dev)
 180{
 181        flush_work(&dev->request_module_wk);
 182}
 183
 184#else
 185#define request_submodules(dev)
 186#define flush_request_submodules(dev)
 187#endif /* CONFIG_MODULES */
 188
 189/* ------------------------------------------------------------------ */
 190
 191/* nr of (saa7134-)pages for the given buffer size */
 192static int saa7134_buffer_pages(int size)
 193{
 194        size  = PAGE_ALIGN(size);
 195        size += PAGE_SIZE; /* for non-page-aligned buffers */
 196        size /= 4096;
 197        return size;
 198}
 199
 200/* calc max # of buffers from size (must not exceed the 4MB virtual
 201 * address space per DMA channel) */
 202int saa7134_buffer_count(unsigned int size, unsigned int count)
 203{
 204        unsigned int maxcount;
 205
 206        maxcount = 1024 / saa7134_buffer_pages(size);
 207        if (count > maxcount)
 208                count = maxcount;
 209        return count;
 210}
 211
 212int saa7134_buffer_startpage(struct saa7134_buf *buf)
 213{
 214        return saa7134_buffer_pages(vb2_plane_size(&buf->vb2.vb2_buf, 0))
 215                        * buf->vb2.vb2_buf.index;
 216}
 217
 218unsigned long saa7134_buffer_base(struct saa7134_buf *buf)
 219{
 220        unsigned long base;
 221        struct sg_table *dma = vb2_dma_sg_plane_desc(&buf->vb2.vb2_buf, 0);
 222
 223        base  = saa7134_buffer_startpage(buf) * 4096;
 224        base += dma->sgl[0].offset;
 225        return base;
 226}
 227
 228/* ------------------------------------------------------------------ */
 229
 230int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt)
 231{
 232        __le32       *cpu;
 233        dma_addr_t   dma_addr = 0;
 234
 235        cpu = pci_alloc_consistent(pci, SAA7134_PGTABLE_SIZE, &dma_addr);
 236        if (NULL == cpu)
 237                return -ENOMEM;
 238        pt->size = SAA7134_PGTABLE_SIZE;
 239        pt->cpu  = cpu;
 240        pt->dma  = dma_addr;
 241        return 0;
 242}
 243
 244int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
 245                          struct scatterlist *list, unsigned int length,
 246                          unsigned int startpage)
 247{
 248        __le32        *ptr;
 249        unsigned int  i, p;
 250
 251        BUG_ON(NULL == pt || NULL == pt->cpu);
 252
 253        ptr = pt->cpu + startpage;
 254        for (i = 0; i < length; i++, list = sg_next(list)) {
 255                for (p = 0; p * 4096 < list->length; p++, ptr++)
 256                        *ptr = cpu_to_le32(sg_dma_address(list) +
 257                                                list->offset + p * 4096);
 258        }
 259        return 0;
 260}
 261
 262void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt)
 263{
 264        if (NULL == pt->cpu)
 265                return;
 266        pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
 267        pt->cpu = NULL;
 268}
 269
 270/* ------------------------------------------------------------------ */
 271
 272int saa7134_buffer_queue(struct saa7134_dev *dev,
 273                         struct saa7134_dmaqueue *q,
 274                         struct saa7134_buf *buf)
 275{
 276        struct saa7134_buf *next = NULL;
 277        unsigned long flags;
 278
 279        spin_lock_irqsave(&dev->slock, flags);
 280        core_dbg("buffer_queue %p\n", buf);
 281        if (NULL == q->curr) {
 282                if (!q->need_two) {
 283                        q->curr = buf;
 284                        buf->activate(dev, buf, NULL);
 285                } else if (list_empty(&q->queue)) {
 286                        list_add_tail(&buf->entry, &q->queue);
 287                } else {
 288                        next = list_entry(q->queue.next, struct saa7134_buf,
 289                                          entry);
 290                        q->curr = buf;
 291                        buf->activate(dev, buf, next);
 292                }
 293        } else {
 294                list_add_tail(&buf->entry, &q->queue);
 295        }
 296        spin_unlock_irqrestore(&dev->slock, flags);
 297        return 0;
 298}
 299
 300void saa7134_buffer_finish(struct saa7134_dev *dev,
 301                           struct saa7134_dmaqueue *q,
 302                           unsigned int state)
 303{
 304        core_dbg("buffer_finish %p\n", q->curr);
 305
 306        /* finish current buffer */
 307        q->curr->vb2.vb2_buf.timestamp = ktime_get_ns();
 308        q->curr->vb2.sequence = q->seq_nr++;
 309        vb2_buffer_done(&q->curr->vb2.vb2_buf, state);
 310        q->curr = NULL;
 311}
 312
 313void saa7134_buffer_next(struct saa7134_dev *dev,
 314                         struct saa7134_dmaqueue *q)
 315{
 316        struct saa7134_buf *buf,*next = NULL;
 317
 318        assert_spin_locked(&dev->slock);
 319        BUG_ON(NULL != q->curr);
 320
 321        if (!list_empty(&q->queue)) {
 322                /* activate next one from queue */
 323                buf = list_entry(q->queue.next, struct saa7134_buf, entry);
 324                core_dbg("buffer_next %p [prev=%p/next=%p]\n",
 325                        buf, q->queue.prev, q->queue.next);
 326                list_del(&buf->entry);
 327                if (!list_empty(&q->queue))
 328                        next = list_entry(q->queue.next, struct saa7134_buf, entry);
 329                q->curr = buf;
 330                buf->activate(dev, buf, next);
 331                core_dbg("buffer_next #2 prev=%p/next=%p\n",
 332                        q->queue.prev, q->queue.next);
 333        } else {
 334                /* nothing to do -- just stop DMA */
 335                core_dbg("buffer_next %p\n", NULL);
 336                saa7134_set_dmabits(dev);
 337                del_timer(&q->timeout);
 338        }
 339}
 340
 341void saa7134_buffer_timeout(struct timer_list *t)
 342{
 343        struct saa7134_dmaqueue *q = from_timer(q, t, timeout);
 344        struct saa7134_dev *dev = q->dev;
 345        unsigned long flags;
 346
 347        spin_lock_irqsave(&dev->slock, flags);
 348
 349        /* try to reset the hardware (SWRST) */
 350        saa_writeb(SAA7134_REGION_ENABLE, 0x00);
 351        saa_writeb(SAA7134_REGION_ENABLE, 0x80);
 352        saa_writeb(SAA7134_REGION_ENABLE, 0x00);
 353
 354        /* flag current buffer as failed,
 355           try to start over with the next one. */
 356        if (q->curr) {
 357                core_dbg("timeout on %p\n", q->curr);
 358                saa7134_buffer_finish(dev, q, VB2_BUF_STATE_ERROR);
 359        }
 360        saa7134_buffer_next(dev, q);
 361        spin_unlock_irqrestore(&dev->slock, flags);
 362}
 363
 364void saa7134_stop_streaming(struct saa7134_dev *dev, struct saa7134_dmaqueue *q)
 365{
 366        unsigned long flags;
 367        struct list_head *pos, *n;
 368        struct saa7134_buf *tmp;
 369
 370        spin_lock_irqsave(&dev->slock, flags);
 371        if (!list_empty(&q->queue)) {
 372                list_for_each_safe(pos, n, &q->queue) {
 373                         tmp = list_entry(pos, struct saa7134_buf, entry);
 374                         vb2_buffer_done(&tmp->vb2.vb2_buf,
 375                                         VB2_BUF_STATE_ERROR);
 376                         list_del(pos);
 377                         tmp = NULL;
 378                }
 379        }
 380        spin_unlock_irqrestore(&dev->slock, flags);
 381        saa7134_buffer_timeout(&q->timeout); /* also calls del_timer(&q->timeout) */
 382}
 383EXPORT_SYMBOL_GPL(saa7134_stop_streaming);
 384
 385/* ------------------------------------------------------------------ */
 386
 387int saa7134_set_dmabits(struct saa7134_dev *dev)
 388{
 389        u32 split, task=0, ctrl=0, irq=0;
 390        enum v4l2_field cap = V4L2_FIELD_ANY;
 391        enum v4l2_field ov  = V4L2_FIELD_ANY;
 392
 393        assert_spin_locked(&dev->slock);
 394
 395        if (dev->insuspend)
 396                return 0;
 397
 398        /* video capture -- dma 0 + video task A */
 399        if (dev->video_q.curr) {
 400                task |= 0x01;
 401                ctrl |= SAA7134_MAIN_CTRL_TE0;
 402                irq  |= SAA7134_IRQ1_INTE_RA0_1 |
 403                        SAA7134_IRQ1_INTE_RA0_0;
 404                cap = dev->field;
 405        }
 406
 407        /* video capture -- dma 1+2 (planar modes) */
 408        if (dev->video_q.curr && dev->fmt->planar) {
 409                ctrl |= SAA7134_MAIN_CTRL_TE4 |
 410                        SAA7134_MAIN_CTRL_TE5;
 411        }
 412
 413        /* screen overlay -- dma 0 + video task B */
 414        if (dev->ovenable) {
 415                task |= 0x10;
 416                ctrl |= SAA7134_MAIN_CTRL_TE1;
 417                ov = dev->ovfield;
 418        }
 419
 420        /* vbi capture -- dma 0 + vbi task A+B */
 421        if (dev->vbi_q.curr) {
 422                task |= 0x22;
 423                ctrl |= SAA7134_MAIN_CTRL_TE2 |
 424                        SAA7134_MAIN_CTRL_TE3;
 425                irq  |= SAA7134_IRQ1_INTE_RA0_7 |
 426                        SAA7134_IRQ1_INTE_RA0_6 |
 427                        SAA7134_IRQ1_INTE_RA0_5 |
 428                        SAA7134_IRQ1_INTE_RA0_4;
 429        }
 430
 431        /* audio capture -- dma 3 */
 432        if (dev->dmasound.dma_running) {
 433                ctrl |= SAA7134_MAIN_CTRL_TE6;
 434                irq  |= SAA7134_IRQ1_INTE_RA3_1 |
 435                        SAA7134_IRQ1_INTE_RA3_0;
 436        }
 437
 438        /* TS capture -- dma 5 */
 439        if (dev->ts_q.curr) {
 440                ctrl |= SAA7134_MAIN_CTRL_TE5;
 441                irq  |= SAA7134_IRQ1_INTE_RA2_1 |
 442                        SAA7134_IRQ1_INTE_RA2_0;
 443        }
 444
 445        /* set task conditions + field handling */
 446        if (V4L2_FIELD_HAS_BOTH(cap) || V4L2_FIELD_HAS_BOTH(ov) || cap == ov) {
 447                /* default config -- use full frames */
 448                saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
 449                saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
 450                saa_writeb(SAA7134_FIELD_HANDLING(TASK_A),  0x02);
 451                saa_writeb(SAA7134_FIELD_HANDLING(TASK_B),  0x02);
 452                split = 0;
 453        } else {
 454                /* split fields between tasks */
 455                if (V4L2_FIELD_TOP == cap) {
 456                        /* odd A, even B, repeat */
 457                        saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
 458                        saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0e);
 459                } else {
 460                        /* odd B, even A, repeat */
 461                        saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0e);
 462                        saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
 463                }
 464                saa_writeb(SAA7134_FIELD_HANDLING(TASK_A),  0x01);
 465                saa_writeb(SAA7134_FIELD_HANDLING(TASK_B),  0x01);
 466                split = 1;
 467        }
 468
 469        /* irqs */
 470        saa_writeb(SAA7134_REGION_ENABLE, task);
 471        saa_writel(SAA7134_IRQ1,          irq);
 472        saa_andorl(SAA7134_MAIN_CTRL,
 473                   SAA7134_MAIN_CTRL_TE0 |
 474                   SAA7134_MAIN_CTRL_TE1 |
 475                   SAA7134_MAIN_CTRL_TE2 |
 476                   SAA7134_MAIN_CTRL_TE3 |
 477                   SAA7134_MAIN_CTRL_TE4 |
 478                   SAA7134_MAIN_CTRL_TE5 |
 479                   SAA7134_MAIN_CTRL_TE6,
 480                   ctrl);
 481        core_dbg("dmabits: task=0x%02x ctrl=0x%02x irq=0x%x split=%s\n",
 482                task, ctrl, irq, split ? "no" : "yes");
 483
 484        return 0;
 485}
 486
 487/* ------------------------------------------------------------------ */
 488/* IRQ handler + helpers                                              */
 489
 490static char *irqbits[] = {
 491        "DONE_RA0", "DONE_RA1", "DONE_RA2", "DONE_RA3",
 492        "AR", "PE", "PWR_ON", "RDCAP", "INTL", "FIDT", "MMC",
 493        "TRIG_ERR", "CONF_ERR", "LOAD_ERR",
 494        "GPIO16", "GPIO18", "GPIO22", "GPIO23"
 495};
 496#define IRQBITS ARRAY_SIZE(irqbits)
 497
 498static void print_irqstatus(struct saa7134_dev *dev, int loop,
 499                            unsigned long report, unsigned long status)
 500{
 501        unsigned int i;
 502
 503        irq_dbg(1, "[%d,%ld]: r=0x%lx s=0x%02lx",
 504                loop, jiffies, report, status);
 505        for (i = 0; i < IRQBITS; i++) {
 506                if (!(report & (1 << i)))
 507                        continue;
 508                pr_cont(" %s", irqbits[i]);
 509        }
 510        if (report & SAA7134_IRQ_REPORT_DONE_RA0) {
 511                pr_cont(" | RA0=%s,%s,%s,%ld",
 512                        (status & 0x40) ? "vbi"  : "video",
 513                        (status & 0x20) ? "b"    : "a",
 514                        (status & 0x10) ? "odd"  : "even",
 515                        (status & 0x0f));
 516        }
 517        pr_cont("\n");
 518}
 519
 520static irqreturn_t saa7134_irq(int irq, void *dev_id)
 521{
 522        struct saa7134_dev *dev = (struct saa7134_dev*) dev_id;
 523        unsigned long report,status;
 524        int loop, handled = 0;
 525
 526        if (dev->insuspend)
 527                goto out;
 528
 529        for (loop = 0; loop < 10; loop++) {
 530                report = saa_readl(SAA7134_IRQ_REPORT);
 531                status = saa_readl(SAA7134_IRQ_STATUS);
 532
 533                /* If dmasound support is active and we get a sound report,
 534                 * mask out the report and let the saa7134-alsa module deal
 535                 * with it */
 536                if ((report & SAA7134_IRQ_REPORT_DONE_RA3) &&
 537                        (dev->dmasound.priv_data != NULL) )
 538                {
 539                        irq_dbg(2, "preserving DMA sound interrupt\n");
 540                        report &= ~SAA7134_IRQ_REPORT_DONE_RA3;
 541                }
 542
 543                if (0 == report) {
 544                        irq_dbg(2, "no (more) work\n");
 545                        goto out;
 546                }
 547
 548                handled = 1;
 549                saa_writel(SAA7134_IRQ_REPORT,report);
 550                if (irq_debug)
 551                        print_irqstatus(dev,loop,report,status);
 552
 553
 554                if ((report & SAA7134_IRQ_REPORT_RDCAP) ||
 555                        (report & SAA7134_IRQ_REPORT_INTL))
 556                                saa7134_irq_video_signalchange(dev);
 557
 558
 559                if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
 560                    (status & 0x60) == 0)
 561                        saa7134_irq_video_done(dev,status);
 562
 563                if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
 564                    (status & 0x40) == 0x40)
 565                        saa7134_irq_vbi_done(dev,status);
 566
 567                if ((report & SAA7134_IRQ_REPORT_DONE_RA2) &&
 568                    card_has_mpeg(dev)) {
 569                        if (dev->mops->irq_ts_done != NULL)
 570                                dev->mops->irq_ts_done(dev, status);
 571                        else
 572                                saa7134_irq_ts_done(dev, status);
 573                }
 574
 575                if (report & SAA7134_IRQ_REPORT_GPIO16) {
 576                        switch (dev->has_remote) {
 577                                case SAA7134_REMOTE_GPIO:
 578                                        if (!dev->remote)
 579                                                break;
 580                                        if  (dev->remote->mask_keydown & 0x10000) {
 581                                                saa7134_input_irq(dev);
 582                                        }
 583                                        break;
 584
 585                                case SAA7134_REMOTE_I2C:
 586                                        break;                  /* FIXME: invoke I2C get_key() */
 587
 588                                default:                        /* GPIO16 not used by IR remote */
 589                                        break;
 590                        }
 591                }
 592
 593                if (report & SAA7134_IRQ_REPORT_GPIO18) {
 594                        switch (dev->has_remote) {
 595                                case SAA7134_REMOTE_GPIO:
 596                                        if (!dev->remote)
 597                                                break;
 598                                        if ((dev->remote->mask_keydown & 0x40000) ||
 599                                            (dev->remote->mask_keyup & 0x40000)) {
 600                                                saa7134_input_irq(dev);
 601                                        }
 602                                        break;
 603
 604                                case SAA7134_REMOTE_I2C:
 605                                        break;                  /* FIXME: invoke I2C get_key() */
 606
 607                                default:                        /* GPIO18 not used by IR remote */
 608                                        break;
 609                        }
 610                }
 611        }
 612
 613        if (10 == loop) {
 614                print_irqstatus(dev,loop,report,status);
 615                if (report & SAA7134_IRQ_REPORT_PE) {
 616                        /* disable all parity error */
 617                        pr_warn("%s/irq: looping -- clearing PE (parity error!) enable bit\n",
 618                                dev->name);
 619                        saa_clearl(SAA7134_IRQ2,SAA7134_IRQ2_INTE_PE);
 620                } else if (report & SAA7134_IRQ_REPORT_GPIO16) {
 621                        /* disable gpio16 IRQ */
 622                        pr_warn("%s/irq: looping -- clearing GPIO16 enable bit\n",
 623                                dev->name);
 624                        saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO16_P);
 625                        saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO16_N);
 626                } else if (report & SAA7134_IRQ_REPORT_GPIO18) {
 627                        /* disable gpio18 IRQs */
 628                        pr_warn("%s/irq: looping -- clearing GPIO18 enable bit\n",
 629                                dev->name);
 630                        saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18_P);
 631                        saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18_N);
 632                } else {
 633                        /* disable all irqs */
 634                        pr_warn("%s/irq: looping -- clearing all enable bits\n",
 635                                dev->name);
 636                        saa_writel(SAA7134_IRQ1,0);
 637                        saa_writel(SAA7134_IRQ2,0);
 638                }
 639        }
 640
 641 out:
 642        return IRQ_RETVAL(handled);
 643}
 644
 645/* ------------------------------------------------------------------ */
 646
 647/* early init (no i2c, no irq) */
 648
 649static int saa7134_hw_enable1(struct saa7134_dev *dev)
 650{
 651        /* RAM FIFO config */
 652        saa_writel(SAA7134_FIFO_SIZE, 0x08070503);
 653        saa_writel(SAA7134_THRESHOULD, 0x02020202);
 654
 655        /* enable audio + video processing */
 656        saa_writel(SAA7134_MAIN_CTRL,
 657                        SAA7134_MAIN_CTRL_VPLLE |
 658                        SAA7134_MAIN_CTRL_APLLE |
 659                        SAA7134_MAIN_CTRL_EXOSC |
 660                        SAA7134_MAIN_CTRL_EVFE1 |
 661                        SAA7134_MAIN_CTRL_EVFE2 |
 662                        SAA7134_MAIN_CTRL_ESFE  |
 663                        SAA7134_MAIN_CTRL_EBDAC);
 664
 665        /*
 666        * Initialize OSS _after_ enabling audio clock PLL and audio processing.
 667        * OSS initialization writes to registers via the audio DSP; these
 668        * writes will fail unless the audio clock has been started.  At worst,
 669        * audio will not work.
 670        */
 671
 672        /* enable peripheral devices */
 673        saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
 674
 675        /* set vertical line numbering start (vbi needs this) */
 676        saa_writeb(SAA7134_SOURCE_TIMING2, 0x20);
 677
 678        return 0;
 679}
 680
 681static int saa7134_hwinit1(struct saa7134_dev *dev)
 682{
 683        core_dbg("hwinit1\n");
 684
 685        saa_writel(SAA7134_IRQ1, 0);
 686        saa_writel(SAA7134_IRQ2, 0);
 687
 688        /* Clear any stale IRQ reports */
 689        saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
 690
 691        mutex_init(&dev->lock);
 692        spin_lock_init(&dev->slock);
 693
 694        saa7134_track_gpio(dev,"pre-init");
 695        saa7134_video_init1(dev);
 696        saa7134_vbi_init1(dev);
 697        if (card_has_mpeg(dev))
 698                saa7134_ts_init1(dev);
 699        saa7134_input_init1(dev);
 700
 701        saa7134_hw_enable1(dev);
 702
 703        return 0;
 704}
 705
 706/* late init (with i2c + irq) */
 707static int saa7134_hw_enable2(struct saa7134_dev *dev)
 708{
 709
 710        unsigned int irq2_mask;
 711
 712        /* enable IRQ's */
 713        irq2_mask =
 714                SAA7134_IRQ2_INTE_DEC3    |
 715                SAA7134_IRQ2_INTE_DEC2    |
 716                SAA7134_IRQ2_INTE_DEC1    |
 717                SAA7134_IRQ2_INTE_DEC0    |
 718                SAA7134_IRQ2_INTE_PE      |
 719                SAA7134_IRQ2_INTE_AR;
 720
 721        if (dev->has_remote == SAA7134_REMOTE_GPIO && dev->remote) {
 722                if (dev->remote->mask_keydown & 0x10000)
 723                        irq2_mask |= SAA7134_IRQ2_INTE_GPIO16_N;
 724                else {          /* Allow enabling both IRQ edge triggers */
 725                        if (dev->remote->mask_keydown & 0x40000)
 726                                irq2_mask |= SAA7134_IRQ2_INTE_GPIO18_P;
 727                        if (dev->remote->mask_keyup & 0x40000)
 728                                irq2_mask |= SAA7134_IRQ2_INTE_GPIO18_N;
 729                }
 730        }
 731
 732        if (dev->has_remote == SAA7134_REMOTE_I2C) {
 733                request_module("ir-kbd-i2c");
 734        }
 735
 736        saa_writel(SAA7134_IRQ1, 0);
 737        saa_writel(SAA7134_IRQ2, irq2_mask);
 738
 739        return 0;
 740}
 741
 742static int saa7134_hwinit2(struct saa7134_dev *dev)
 743{
 744
 745        core_dbg("hwinit2\n");
 746
 747        saa7134_video_init2(dev);
 748        saa7134_tvaudio_init2(dev);
 749
 750        saa7134_hw_enable2(dev);
 751
 752        return 0;
 753}
 754
 755
 756/* shutdown */
 757static int saa7134_hwfini(struct saa7134_dev *dev)
 758{
 759        core_dbg("hwfini\n");
 760
 761        if (card_has_mpeg(dev))
 762                saa7134_ts_fini(dev);
 763        saa7134_input_fini(dev);
 764        saa7134_vbi_fini(dev);
 765        saa7134_tvaudio_fini(dev);
 766        saa7134_video_fini(dev);
 767        return 0;
 768}
 769
 770static void must_configure_manually(int has_eeprom)
 771{
 772        unsigned int i,p;
 773
 774        if (!has_eeprom)
 775                pr_warn("saa7134: <rant>\n"
 776                        "saa7134:  Congratulations!  Your TV card vendor saved a few\n"
 777                        "saa7134:  cents for a eeprom, thus your pci board has no\n"
 778                        "saa7134:  subsystem ID and I can't identify it automatically\n"
 779                        "saa7134: </rant>\n"
 780                        "saa7134: I feel better now.  Ok, here are the good news:\n"
 781                        "saa7134: You can use the card=<nr> insmod option to specify\n"
 782                        "saa7134: which board do you have.  The list:\n");
 783        else
 784                pr_warn("saa7134: Board is currently unknown. You might try to use the card=<nr>\n"
 785                        "saa7134: insmod option to specify which board do you have, but this is\n"
 786                        "saa7134: somewhat risky, as might damage your card. It is better to ask\n"
 787                        "saa7134: for support at linux-media@vger.kernel.org.\n"
 788                        "saa7134: The supported cards are:\n");
 789
 790        for (i = 0; i < saa7134_bcount; i++) {
 791                pr_warn("saa7134:   card=%d -> %-40.40s",
 792                       i,saa7134_boards[i].name);
 793                for (p = 0; saa7134_pci_tbl[p].driver_data; p++) {
 794                        if (saa7134_pci_tbl[p].driver_data != i)
 795                                continue;
 796                        pr_cont(" %04x:%04x",
 797                               saa7134_pci_tbl[p].subvendor,
 798                               saa7134_pci_tbl[p].subdevice);
 799                }
 800                pr_cont("\n");
 801        }
 802}
 803
 804static void saa7134_unregister_media_device(struct saa7134_dev *dev)
 805{
 806
 807#ifdef CONFIG_MEDIA_CONTROLLER
 808        if (!dev->media_dev)
 809                return;
 810        media_device_unregister(dev->media_dev);
 811        media_device_cleanup(dev->media_dev);
 812        kfree(dev->media_dev);
 813        dev->media_dev = NULL;
 814#endif
 815}
 816
 817static void saa7134_media_release(struct saa7134_dev *dev)
 818{
 819#ifdef CONFIG_MEDIA_CONTROLLER
 820        int i;
 821
 822        for (i = 0; i < SAA7134_INPUT_MAX + 1; i++)
 823                media_device_unregister_entity(&dev->input_ent[i]);
 824#endif
 825}
 826
 827#if defined(CONFIG_MEDIA_CONTROLLER)
 828static void saa7134_create_entities(struct saa7134_dev *dev)
 829{
 830        int ret, i;
 831        struct media_entity *entity;
 832        struct media_entity *decoder = NULL;
 833
 834        /* Check if it is using an external analog TV demod */
 835        media_device_for_each_entity(entity, dev->media_dev) {
 836                if (entity->function == MEDIA_ENT_F_ATV_DECODER) {
 837                        decoder = entity;
 838                        break;
 839                }
 840        }
 841
 842        /*
 843         * saa713x is not using an external ATV demod.
 844         * Register the internal one
 845         */
 846        if (!decoder) {
 847                dev->demod.name = "saa713x";
 848                dev->demod_pad[SAA7134_PAD_IF_INPUT].flags = MEDIA_PAD_FL_SINK;
 849                dev->demod_pad[SAA7134_PAD_IF_INPUT].sig_type = PAD_SIGNAL_ANALOG;
 850                dev->demod_pad[SAA7134_PAD_VID_OUT].flags = MEDIA_PAD_FL_SOURCE;
 851                dev->demod_pad[SAA7134_PAD_VID_OUT].sig_type = PAD_SIGNAL_DV;
 852                dev->demod.function = MEDIA_ENT_F_ATV_DECODER;
 853
 854                ret = media_entity_pads_init(&dev->demod, SAA7134_NUM_PADS,
 855                                             dev->demod_pad);
 856                if (ret < 0)
 857                        pr_err("failed to initialize demod pad!\n");
 858
 859                ret = media_device_register_entity(dev->media_dev, &dev->demod);
 860                if (ret < 0)
 861                        pr_err("failed to register demod entity!\n");
 862
 863                dev->decoder = &dev->demod;
 864        } else {
 865                dev->decoder = decoder;
 866        }
 867
 868        /* Initialize Video, VBI and Radio pads */
 869        dev->video_pad.flags = MEDIA_PAD_FL_SINK;
 870        ret = media_entity_pads_init(&dev->video_dev->entity, 1,
 871                                     &dev->video_pad);
 872        if (ret < 0)
 873                pr_err("failed to initialize video media entity!\n");
 874
 875        dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
 876        ret = media_entity_pads_init(&dev->vbi_dev->entity, 1,
 877                                        &dev->vbi_pad);
 878        if (ret < 0)
 879                pr_err("failed to initialize vbi media entity!\n");
 880
 881        /* Create entities for each input connector */
 882        for (i = 0; i < SAA7134_INPUT_MAX; i++) {
 883                struct media_entity *ent = &dev->input_ent[i];
 884                struct saa7134_input *in = &card_in(dev, i);
 885
 886                if (in->type == SAA7134_NO_INPUT)
 887                        break;
 888
 889                /* This input uses the S-Video connector */
 890                if (in->type == SAA7134_INPUT_COMPOSITE_OVER_SVIDEO)
 891                        continue;
 892
 893                ent->name = saa7134_input_name[in->type];
 894                ent->flags = MEDIA_ENT_FL_CONNECTOR;
 895                dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
 896
 897                switch (in->type) {
 898                case SAA7134_INPUT_COMPOSITE:
 899                case SAA7134_INPUT_COMPOSITE0:
 900                case SAA7134_INPUT_COMPOSITE1:
 901                case SAA7134_INPUT_COMPOSITE2:
 902                case SAA7134_INPUT_COMPOSITE3:
 903                case SAA7134_INPUT_COMPOSITE4:
 904                        ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
 905                        break;
 906                case SAA7134_INPUT_SVIDEO:
 907                case SAA7134_INPUT_SVIDEO0:
 908                case SAA7134_INPUT_SVIDEO1:
 909                        ent->function = MEDIA_ENT_F_CONN_SVIDEO;
 910                        break;
 911                default:
 912                        /*
 913                         * SAA7134_INPUT_TV and SAA7134_INPUT_TV_MONO.
 914                         *
 915                         * Please notice that neither SAA7134_INPUT_MUTE or
 916                         * SAA7134_INPUT_RADIO are defined at
 917                         * saa7134_board.input.
 918                         */
 919                        ent->function = MEDIA_ENT_F_CONN_RF;
 920                        break;
 921                }
 922
 923                ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
 924                if (ret < 0)
 925                        pr_err("failed to initialize input pad[%d]!\n", i);
 926
 927                ret = media_device_register_entity(dev->media_dev, ent);
 928                if (ret < 0)
 929                        pr_err("failed to register input entity %d!\n", i);
 930        }
 931
 932        /* Create input for Radio RF connector */
 933        if (card_has_radio(dev)) {
 934                struct saa7134_input *in = &saa7134_boards[dev->board].radio;
 935                struct media_entity *ent = &dev->input_ent[i];
 936
 937                ent->name = saa7134_input_name[in->type];
 938                ent->flags = MEDIA_ENT_FL_CONNECTOR;
 939                dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
 940                ent->function = MEDIA_ENT_F_CONN_RF;
 941
 942                ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
 943                if (ret < 0)
 944                        pr_err("failed to initialize input pad[%d]!\n", i);
 945
 946                ret = media_device_register_entity(dev->media_dev, ent);
 947                if (ret < 0)
 948                        pr_err("failed to register input entity %d!\n", i);
 949        }
 950}
 951#endif
 952
 953static struct video_device *vdev_init(struct saa7134_dev *dev,
 954                                      struct video_device *template,
 955                                      char *type)
 956{
 957        struct video_device *vfd;
 958
 959        vfd = video_device_alloc();
 960        if (NULL == vfd)
 961                return NULL;
 962        *vfd = *template;
 963        vfd->v4l2_dev  = &dev->v4l2_dev;
 964        vfd->release = video_device_release;
 965        snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
 966                 dev->name, type, saa7134_boards[dev->board].name);
 967        video_set_drvdata(vfd, dev);
 968        return vfd;
 969}
 970
 971static void saa7134_unregister_video(struct saa7134_dev *dev)
 972{
 973        saa7134_media_release(dev);
 974
 975        if (dev->video_dev) {
 976                if (video_is_registered(dev->video_dev))
 977                        video_unregister_device(dev->video_dev);
 978                else
 979                        video_device_release(dev->video_dev);
 980                dev->video_dev = NULL;
 981        }
 982        if (dev->vbi_dev) {
 983                if (video_is_registered(dev->vbi_dev))
 984                        video_unregister_device(dev->vbi_dev);
 985                else
 986                        video_device_release(dev->vbi_dev);
 987                dev->vbi_dev = NULL;
 988        }
 989        if (dev->radio_dev) {
 990                if (video_is_registered(dev->radio_dev))
 991                        video_unregister_device(dev->radio_dev);
 992                else
 993                        video_device_release(dev->radio_dev);
 994                dev->radio_dev = NULL;
 995        }
 996}
 997
 998static void mpeg_ops_attach(struct saa7134_mpeg_ops *ops,
 999                            struct saa7134_dev *dev)
1000{
1001        int err;
1002
1003        if (NULL != dev->mops)
1004                return;
1005        if (saa7134_boards[dev->board].mpeg != ops->type)
1006                return;
1007        err = ops->init(dev);
1008        if (0 != err)
1009                return;
1010        dev->mops = ops;
1011}
1012
1013static void mpeg_ops_detach(struct saa7134_mpeg_ops *ops,
1014                            struct saa7134_dev *dev)
1015{
1016        if (NULL == dev->mops)
1017                return;
1018        if (dev->mops != ops)
1019                return;
1020        dev->mops->fini(dev);
1021        dev->mops = NULL;
1022}
1023
1024static int saa7134_initdev(struct pci_dev *pci_dev,
1025                           const struct pci_device_id *pci_id)
1026{
1027        struct saa7134_dev *dev;
1028        struct saa7134_mpeg_ops *mops;
1029        int err;
1030
1031        if (saa7134_devcount == SAA7134_MAXBOARDS)
1032                return -ENOMEM;
1033
1034        dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1035        if (NULL == dev)
1036                return -ENOMEM;
1037
1038        dev->nr = saa7134_devcount;
1039        sprintf(dev->name, "saa%x[%d]", pci_dev->device, dev->nr);
1040
1041#ifdef CONFIG_MEDIA_CONTROLLER
1042        dev->media_dev = kzalloc(sizeof(*dev->media_dev), GFP_KERNEL);
1043        if (!dev->media_dev) {
1044                err = -ENOMEM;
1045                goto fail0;
1046        }
1047        media_device_pci_init(dev->media_dev, pci_dev, dev->name);
1048        dev->v4l2_dev.mdev = dev->media_dev;
1049#endif
1050
1051        err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
1052        if (err)
1053                goto fail0;
1054
1055        /* pci init */
1056        dev->pci = pci_dev;
1057        if (pci_enable_device(pci_dev)) {
1058                err = -EIO;
1059                goto fail1;
1060        }
1061
1062        /* pci quirks */
1063        if (pci_pci_problems) {
1064                if (pci_pci_problems & PCIPCI_TRITON)
1065                        pr_info("%s: quirk: PCIPCI_TRITON\n", dev->name);
1066                if (pci_pci_problems & PCIPCI_NATOMA)
1067                        pr_info("%s: quirk: PCIPCI_NATOMA\n", dev->name);
1068                if (pci_pci_problems & PCIPCI_VIAETBF)
1069                        pr_info("%s: quirk: PCIPCI_VIAETBF\n", dev->name);
1070                if (pci_pci_problems & PCIPCI_VSFX)
1071                        pr_info("%s: quirk: PCIPCI_VSFX\n", dev->name);
1072#ifdef PCIPCI_ALIMAGIK
1073                if (pci_pci_problems & PCIPCI_ALIMAGIK) {
1074                        pr_info("%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
1075                               dev->name);
1076                        latency = 0x0A;
1077                }
1078#endif
1079                if (pci_pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL)) {
1080                        pr_info("%s: quirk: this driver and your chipset may not work together in overlay mode.\n",
1081                                dev->name);
1082                        if (!saa7134_no_overlay) {
1083                                pr_info("%s: quirk: overlay mode will be disabled.\n",
1084                                                dev->name);
1085                                saa7134_no_overlay = 1;
1086                        } else {
1087                                pr_info("%s: quirk: overlay mode will be forced. Use this option at your own risk.\n",
1088                                                dev->name);
1089                        }
1090                }
1091        }
1092        if (UNSET != latency) {
1093                pr_info("%s: setting pci latency timer to %d\n",
1094                       dev->name,latency);
1095                pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
1096        }
1097
1098        /* print pci info */
1099        dev->pci_rev = pci_dev->revision;
1100        pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER,  &dev->pci_lat);
1101        pr_info("%s: found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n",
1102                dev->name, pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1103                dev->pci_lat,
1104                (unsigned long long)pci_resource_start(pci_dev, 0));
1105        pci_set_master(pci_dev);
1106        err = pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32));
1107        if (err) {
1108                pr_warn("%s: Oops: no 32bit PCI DMA ???\n", dev->name);
1109                goto fail1;
1110        }
1111
1112        /* board config */
1113        dev->board = pci_id->driver_data;
1114        if ((unsigned)card[dev->nr] < saa7134_bcount)
1115                dev->board = card[dev->nr];
1116        if (SAA7134_BOARD_UNKNOWN == dev->board)
1117                must_configure_manually(0);
1118        else if (SAA7134_BOARD_NOAUTO == dev->board) {
1119                must_configure_manually(1);
1120                dev->board = SAA7134_BOARD_UNKNOWN;
1121        }
1122        dev->autodetected = card[dev->nr] != dev->board;
1123        dev->tuner_type = saa7134_boards[dev->board].tuner_type;
1124        dev->tuner_addr = saa7134_boards[dev->board].tuner_addr;
1125        dev->radio_type = saa7134_boards[dev->board].radio_type;
1126        dev->radio_addr = saa7134_boards[dev->board].radio_addr;
1127        dev->tda9887_conf = saa7134_boards[dev->board].tda9887_conf;
1128        if (UNSET != tuner[dev->nr])
1129                dev->tuner_type = tuner[dev->nr];
1130        pr_info("%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
1131                dev->name,pci_dev->subsystem_vendor,
1132                pci_dev->subsystem_device,saa7134_boards[dev->board].name,
1133                dev->board, dev->autodetected ?
1134                "autodetected" : "insmod option");
1135
1136        /* get mmio */
1137        if (!request_mem_region(pci_resource_start(pci_dev,0),
1138                                pci_resource_len(pci_dev,0),
1139                                dev->name)) {
1140                err = -EBUSY;
1141                pr_err("%s: can't get MMIO memory @ 0x%llx\n",
1142                       dev->name,(unsigned long long)pci_resource_start(pci_dev,0));
1143                goto fail1;
1144        }
1145        dev->lmmio = ioremap(pci_resource_start(pci_dev, 0),
1146                             pci_resource_len(pci_dev, 0));
1147        dev->bmmio = (__u8 __iomem *)dev->lmmio;
1148        if (NULL == dev->lmmio) {
1149                err = -EIO;
1150                pr_err("%s: can't ioremap() MMIO memory\n",
1151                       dev->name);
1152                goto fail2;
1153        }
1154
1155        /* initialize hardware #1 */
1156        saa7134_board_init1(dev);
1157        saa7134_hwinit1(dev);
1158
1159        /* get irq */
1160        err = request_irq(pci_dev->irq, saa7134_irq,
1161                          IRQF_SHARED, dev->name, dev);
1162        if (err < 0) {
1163                pr_err("%s: can't get IRQ %d\n",
1164                       dev->name,pci_dev->irq);
1165                goto fail3;
1166        }
1167
1168        /* wait a bit, register i2c bus */
1169        msleep(100);
1170        saa7134_i2c_register(dev);
1171        saa7134_board_init2(dev);
1172
1173        saa7134_hwinit2(dev);
1174
1175        /* load i2c helpers */
1176        if (card_is_empress(dev)) {
1177                dev->empress_sd =
1178                        v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
1179                                "saa6752hs",
1180                                saa7134_boards[dev->board].empress_addr, NULL);
1181
1182                if (dev->empress_sd)
1183                        dev->empress_sd->grp_id = GRP_EMPRESS;
1184        }
1185
1186        if (saa7134_boards[dev->board].rds_addr) {
1187                struct v4l2_subdev *sd;
1188
1189                sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1190                                &dev->i2c_adap, "saa6588",
1191                                0, I2C_ADDRS(saa7134_boards[dev->board].rds_addr));
1192                if (sd) {
1193                        pr_info("%s: found RDS decoder\n", dev->name);
1194                        dev->has_rds = 1;
1195                }
1196        }
1197
1198        mutex_lock(&saa7134_devlist_lock);
1199        list_for_each_entry(mops, &mops_list, next)
1200                mpeg_ops_attach(mops, dev);
1201        list_add_tail(&dev->devlist, &saa7134_devlist);
1202        mutex_unlock(&saa7134_devlist_lock);
1203
1204        /* check for signal */
1205        saa7134_irq_video_signalchange(dev);
1206
1207        if (TUNER_ABSENT != dev->tuner_type)
1208                saa_call_all(dev, core, s_power, 0);
1209
1210        /* register v4l devices */
1211        if (saa7134_no_overlay > 0)
1212                pr_info("%s: Overlay support disabled.\n", dev->name);
1213
1214        dev->video_dev = vdev_init(dev,&saa7134_video_template,"video");
1215        dev->video_dev->ctrl_handler = &dev->ctrl_handler;
1216        dev->video_dev->lock = &dev->lock;
1217        dev->video_dev->queue = &dev->video_vbq;
1218        err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1219                                    video_nr[dev->nr]);
1220        if (err < 0) {
1221                pr_info("%s: can't register video device\n",
1222                       dev->name);
1223                goto fail4;
1224        }
1225        pr_info("%s: registered device %s [v4l2]\n",
1226               dev->name, video_device_node_name(dev->video_dev));
1227
1228        dev->vbi_dev = vdev_init(dev, &saa7134_video_template, "vbi");
1229        dev->vbi_dev->ctrl_handler = &dev->ctrl_handler;
1230        dev->vbi_dev->lock = &dev->lock;
1231        dev->vbi_dev->queue = &dev->vbi_vbq;
1232
1233        err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1234                                    vbi_nr[dev->nr]);
1235        if (err < 0)
1236                goto fail4;
1237        pr_info("%s: registered device %s\n",
1238               dev->name, video_device_node_name(dev->vbi_dev));
1239
1240        if (card_has_radio(dev)) {
1241                dev->radio_dev = vdev_init(dev,&saa7134_radio_template,"radio");
1242                dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
1243                dev->radio_dev->lock = &dev->lock;
1244                err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1245                                            radio_nr[dev->nr]);
1246                if (err < 0)
1247                        goto fail4;
1248                pr_info("%s: registered device %s\n",
1249                       dev->name, video_device_node_name(dev->radio_dev));
1250        }
1251
1252#ifdef CONFIG_MEDIA_CONTROLLER
1253        saa7134_create_entities(dev);
1254
1255        err = v4l2_mc_create_media_graph(dev->media_dev);
1256        if (err) {
1257                pr_err("failed to create media graph\n");
1258                goto fail4;
1259        }
1260#endif
1261        /* everything worked */
1262        saa7134_devcount++;
1263
1264        if (saa7134_dmasound_init && !dev->dmasound.priv_data)
1265                saa7134_dmasound_init(dev);
1266
1267        request_submodules(dev);
1268
1269        /*
1270         * Do it at the end, to reduce dynamic configuration changes during
1271         * the device init. Yet, as request_modules() can be async, the
1272         * topology will likely change after load the saa7134 subdrivers.
1273         */
1274#ifdef CONFIG_MEDIA_CONTROLLER
1275        err = media_device_register(dev->media_dev);
1276        if (err)
1277                goto fail4;
1278#endif
1279
1280        return 0;
1281
1282 fail4:
1283        saa7134_unregister_video(dev);
1284        saa7134_i2c_unregister(dev);
1285        free_irq(pci_dev->irq, dev);
1286 fail3:
1287        saa7134_hwfini(dev);
1288        iounmap(dev->lmmio);
1289 fail2:
1290        release_mem_region(pci_resource_start(pci_dev,0),
1291                           pci_resource_len(pci_dev,0));
1292 fail1:
1293        v4l2_device_unregister(&dev->v4l2_dev);
1294 fail0:
1295#ifdef CONFIG_MEDIA_CONTROLLER
1296        kfree(dev->media_dev);
1297#endif
1298        kfree(dev);
1299        return err;
1300}
1301
1302static void saa7134_finidev(struct pci_dev *pci_dev)
1303{
1304        struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1305        struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1306        struct saa7134_mpeg_ops *mops;
1307
1308        flush_request_submodules(dev);
1309
1310        /* Release DMA sound modules if present */
1311        if (saa7134_dmasound_exit && dev->dmasound.priv_data) {
1312                saa7134_dmasound_exit(dev);
1313        }
1314
1315        /* debugging ... */
1316        if (irq_debug) {
1317                u32 report = saa_readl(SAA7134_IRQ_REPORT);
1318                u32 status = saa_readl(SAA7134_IRQ_STATUS);
1319                print_irqstatus(dev,42,report,status);
1320        }
1321
1322        /* disable peripheral devices */
1323        saa_writeb(SAA7134_SPECIAL_MODE,0);
1324
1325        /* shutdown hardware */
1326        saa_writel(SAA7134_IRQ1,0);
1327        saa_writel(SAA7134_IRQ2,0);
1328        saa_writel(SAA7134_MAIN_CTRL,0);
1329
1330        /* shutdown subsystems */
1331        saa7134_hwfini(dev);
1332
1333        /* unregister */
1334        mutex_lock(&saa7134_devlist_lock);
1335        list_del(&dev->devlist);
1336        list_for_each_entry(mops, &mops_list, next)
1337                mpeg_ops_detach(mops, dev);
1338        mutex_unlock(&saa7134_devlist_lock);
1339        saa7134_devcount--;
1340
1341        saa7134_i2c_unregister(dev);
1342        saa7134_unregister_video(dev);
1343
1344
1345        /* the DMA sound modules should be unloaded before reaching
1346           this, but just in case they are still present... */
1347        if (dev->dmasound.priv_data != NULL) {
1348                free_irq(pci_dev->irq, &dev->dmasound);
1349                dev->dmasound.priv_data = NULL;
1350        }
1351
1352
1353        /* release resources */
1354        free_irq(pci_dev->irq, dev);
1355        iounmap(dev->lmmio);
1356        release_mem_region(pci_resource_start(pci_dev,0),
1357                           pci_resource_len(pci_dev,0));
1358
1359        v4l2_device_unregister(&dev->v4l2_dev);
1360
1361        saa7134_unregister_media_device(dev);
1362
1363        /* free memory */
1364        kfree(dev);
1365}
1366
1367#ifdef CONFIG_PM
1368
1369/* resends a current buffer in queue after resume */
1370static int saa7134_buffer_requeue(struct saa7134_dev *dev,
1371                                  struct saa7134_dmaqueue *q)
1372{
1373        struct saa7134_buf *buf, *next;
1374
1375        assert_spin_locked(&dev->slock);
1376
1377        buf  = q->curr;
1378        next = buf;
1379        core_dbg("buffer_requeue\n");
1380
1381        if (!buf)
1382                return 0;
1383
1384        core_dbg("buffer_requeue : resending active buffer\n");
1385
1386        if (!list_empty(&q->queue))
1387                next = list_entry(q->queue.next, struct saa7134_buf,
1388                                          entry);
1389        buf->activate(dev, buf, next);
1390
1391        return 0;
1392}
1393
1394static int saa7134_suspend(struct pci_dev *pci_dev , pm_message_t state)
1395{
1396        struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1397        struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1398
1399        /* disable overlay - apps should enable it explicitly on resume*/
1400        dev->ovenable = 0;
1401
1402        /* Disable interrupts, DMA, and rest of the chip*/
1403        saa_writel(SAA7134_IRQ1, 0);
1404        saa_writel(SAA7134_IRQ2, 0);
1405        saa_writel(SAA7134_MAIN_CTRL, 0);
1406
1407        dev->insuspend = 1;
1408        synchronize_irq(pci_dev->irq);
1409
1410        /* ACK interrupts once more, just in case,
1411                since the IRQ handler won't ack them anymore*/
1412
1413        saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
1414
1415        /* Disable timeout timers - if we have active buffers, we will
1416           fill them on resume*/
1417
1418        del_timer(&dev->video_q.timeout);
1419        del_timer(&dev->vbi_q.timeout);
1420        del_timer(&dev->ts_q.timeout);
1421
1422        if (dev->remote)
1423                saa7134_ir_stop(dev);
1424
1425        pci_save_state(pci_dev);
1426        pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
1427
1428        return 0;
1429}
1430
1431static int saa7134_resume(struct pci_dev *pci_dev)
1432{
1433        struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1434        struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1435        unsigned long flags;
1436
1437        pci_set_power_state(pci_dev, PCI_D0);
1438        pci_restore_state(pci_dev);
1439
1440        /* Do things that are done in saa7134_initdev ,
1441                except of initializing memory structures.*/
1442
1443        saa7134_board_init1(dev);
1444
1445        /* saa7134_hwinit1 */
1446        if (saa7134_boards[dev->board].video_out)
1447                saa7134_videoport_init(dev);
1448        if (card_has_mpeg(dev))
1449                saa7134_ts_init_hw(dev);
1450        if (dev->remote)
1451                saa7134_ir_start(dev);
1452        saa7134_hw_enable1(dev);
1453
1454        msleep(100);
1455
1456        saa7134_board_init2(dev);
1457
1458        /*saa7134_hwinit2*/
1459        saa7134_set_tvnorm_hw(dev);
1460        saa7134_tvaudio_setmute(dev);
1461        saa7134_tvaudio_setvolume(dev, dev->ctl_volume);
1462        saa7134_tvaudio_init(dev);
1463        saa7134_enable_i2s(dev);
1464        saa7134_hw_enable2(dev);
1465
1466        saa7134_irq_video_signalchange(dev);
1467
1468        /*resume unfinished buffer(s)*/
1469        spin_lock_irqsave(&dev->slock, flags);
1470        saa7134_buffer_requeue(dev, &dev->video_q);
1471        saa7134_buffer_requeue(dev, &dev->vbi_q);
1472        saa7134_buffer_requeue(dev, &dev->ts_q);
1473
1474        /* FIXME: Disable DMA audio sound - temporary till proper support
1475                  is implemented*/
1476
1477        dev->dmasound.dma_running = 0;
1478
1479        /* start DMA now*/
1480        dev->insuspend = 0;
1481        smp_wmb();
1482        saa7134_set_dmabits(dev);
1483        spin_unlock_irqrestore(&dev->slock, flags);
1484
1485        return 0;
1486}
1487#endif
1488
1489/* ----------------------------------------------------------- */
1490
1491int saa7134_ts_register(struct saa7134_mpeg_ops *ops)
1492{
1493        struct saa7134_dev *dev;
1494
1495        mutex_lock(&saa7134_devlist_lock);
1496        list_for_each_entry(dev, &saa7134_devlist, devlist)
1497                mpeg_ops_attach(ops, dev);
1498        list_add_tail(&ops->next,&mops_list);
1499        mutex_unlock(&saa7134_devlist_lock);
1500        return 0;
1501}
1502
1503void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops)
1504{
1505        struct saa7134_dev *dev;
1506
1507        mutex_lock(&saa7134_devlist_lock);
1508        list_del(&ops->next);
1509        list_for_each_entry(dev, &saa7134_devlist, devlist)
1510                mpeg_ops_detach(ops, dev);
1511        mutex_unlock(&saa7134_devlist_lock);
1512}
1513
1514EXPORT_SYMBOL(saa7134_ts_register);
1515EXPORT_SYMBOL(saa7134_ts_unregister);
1516
1517/* ----------------------------------------------------------- */
1518
1519static struct pci_driver saa7134_pci_driver = {
1520        .name     = "saa7134",
1521        .id_table = saa7134_pci_tbl,
1522        .probe    = saa7134_initdev,
1523        .remove   = saa7134_finidev,
1524#ifdef CONFIG_PM
1525        .suspend  = saa7134_suspend,
1526        .resume   = saa7134_resume
1527#endif
1528};
1529
1530static int __init saa7134_init(void)
1531{
1532        INIT_LIST_HEAD(&saa7134_devlist);
1533        pr_info("saa7130/34: v4l2 driver version %s loaded\n",
1534               SAA7134_VERSION);
1535        return pci_register_driver(&saa7134_pci_driver);
1536}
1537
1538static void __exit saa7134_fini(void)
1539{
1540        pci_unregister_driver(&saa7134_pci_driver);
1541}
1542
1543module_init(saa7134_init);
1544module_exit(saa7134_fini);
1545
1546/* ----------------------------------------------------------- */
1547
1548EXPORT_SYMBOL(saa7134_set_gpio);
1549EXPORT_SYMBOL(saa7134_boards);
1550
1551/* ----------------- for the DMA sound modules --------------- */
1552
1553EXPORT_SYMBOL(saa7134_dmasound_init);
1554EXPORT_SYMBOL(saa7134_dmasound_exit);
1555EXPORT_SYMBOL(saa7134_pgtable_free);
1556EXPORT_SYMBOL(saa7134_pgtable_build);
1557EXPORT_SYMBOL(saa7134_pgtable_alloc);
1558EXPORT_SYMBOL(saa7134_set_dmabits);
1559