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