root/drivers/media/pci/saa7134/saa7134-core.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. saa7134_track_gpio
  2. saa7134_set_gpio
  3. request_module_async
  4. request_submodules
  5. flush_request_submodules
  6. saa7134_buffer_pages
  7. saa7134_buffer_count
  8. saa7134_buffer_startpage
  9. saa7134_buffer_base
  10. saa7134_pgtable_alloc
  11. saa7134_pgtable_build
  12. saa7134_pgtable_free
  13. saa7134_buffer_queue
  14. saa7134_buffer_finish
  15. saa7134_buffer_next
  16. saa7134_buffer_timeout
  17. saa7134_stop_streaming
  18. saa7134_set_dmabits
  19. print_irqstatus
  20. saa7134_irq
  21. saa7134_hw_enable1
  22. saa7134_hwinit1
  23. saa7134_hw_enable2
  24. saa7134_hwinit2
  25. saa7134_hwfini
  26. must_configure_manually
  27. saa7134_unregister_media_device
  28. saa7134_media_release
  29. saa7134_create_entities
  30. vdev_init
  31. saa7134_unregister_video
  32. mpeg_ops_attach
  33. mpeg_ops_detach
  34. saa7134_initdev
  35. saa7134_finidev
  36. saa7134_buffer_requeue
  37. saa7134_suspend
  38. saa7134_resume
  39. saa7134_ts_register
  40. saa7134_ts_unregister
  41. saa7134_init
  42. saa7134_fini

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

/* [<][>][^][v][top][bottom][index][help] */