aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'drivers')
-rw-r--r--drivers/media/pci/saa7134/Kconfig4
-rw-r--r--drivers/media/pci/saa7134/saa7134-core.c86
-rw-r--r--drivers/media/pci/saa7134/saa7134-dvb.c43
-rw-r--r--drivers/media/pci/saa7134/saa7134-empress.c175
-rw-r--r--drivers/media/pci/saa7134/saa7134-ts.c171
-rw-r--r--drivers/media/pci/saa7134/saa7134-vbi.c108
-rw-r--r--drivers/media/pci/saa7134/saa7134-video.c590
-rw-r--r--drivers/media/pci/saa7134/saa7134.h94
8 files changed, 542 insertions, 729 deletions
diff --git a/drivers/media/pci/saa7134/Kconfig b/drivers/media/pci/saa7134/Kconfig
index 7883393571e5..18ae75546302 100644
--- a/drivers/media/pci/saa7134/Kconfig
+++ b/drivers/media/pci/saa7134/Kconfig
@@ -1,7 +1,7 @@
1config VIDEO_SAA7134 1config VIDEO_SAA7134
2 tristate "Philips SAA7134 support" 2 tristate "Philips SAA7134 support"
3 depends on VIDEO_DEV && PCI && I2C 3 depends on VIDEO_DEV && PCI && I2C
4 select VIDEOBUF_DMA_SG 4 select VIDEOBUF2_DMA_SG
5 select VIDEO_TUNER 5 select VIDEO_TUNER
6 select VIDEO_TVEEPROM 6 select VIDEO_TVEEPROM
7 select CRC32 7 select CRC32
@@ -37,7 +37,7 @@ config VIDEO_SAA7134_RC
37config VIDEO_SAA7134_DVB 37config VIDEO_SAA7134_DVB
38 tristate "DVB/ATSC Support for saa7134 based TV cards" 38 tristate "DVB/ATSC Support for saa7134 based TV cards"
39 depends on VIDEO_SAA7134 && DVB_CORE 39 depends on VIDEO_SAA7134 && DVB_CORE
40 select VIDEOBUF_DVB 40 select VIDEOBUF2_DVB
41 select DVB_PLL if MEDIA_SUBDRV_AUTOSELECT 41 select DVB_PLL if MEDIA_SUBDRV_AUTOSELECT
42 select DVB_MT352 if MEDIA_SUBDRV_AUTOSELECT 42 select DVB_MT352 if MEDIA_SUBDRV_AUTOSELECT
43 select DVB_TDA1004X if MEDIA_SUBDRV_AUTOSELECT 43 select DVB_TDA1004X if MEDIA_SUBDRV_AUTOSELECT
diff --git a/drivers/media/pci/saa7134/saa7134-core.c b/drivers/media/pci/saa7134/saa7134-core.c
index f6cfbb450613..f4ea0ecec91e 100644
--- a/drivers/media/pci/saa7134/saa7134-core.c
+++ b/drivers/media/pci/saa7134/saa7134-core.c
@@ -203,16 +203,16 @@ int saa7134_buffer_count(unsigned int size, unsigned int count)
203 203
204int saa7134_buffer_startpage(struct saa7134_buf *buf) 204int saa7134_buffer_startpage(struct saa7134_buf *buf)
205{ 205{
206 return saa7134_buffer_pages(buf->vb.bsize) * buf->vb.i; 206 return saa7134_buffer_pages(vb2_plane_size(&buf->vb2, 0)) * buf->vb2.v4l2_buf.index;
207} 207}
208 208
209unsigned long saa7134_buffer_base(struct saa7134_buf *buf) 209unsigned long saa7134_buffer_base(struct saa7134_buf *buf)
210{ 210{
211 unsigned long base; 211 unsigned long base;
212 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb); 212 struct sg_table *dma = vb2_dma_sg_plane_desc(&buf->vb2, 0);
213 213
214 base = saa7134_buffer_startpage(buf) * 4096; 214 base = saa7134_buffer_startpage(buf) * 4096;
215 base += dma->sglist[0].offset; 215 base += dma->sgl[0].offset;
216 return base; 216 return base;
217} 217}
218 218
@@ -242,9 +242,11 @@ int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
242 BUG_ON(NULL == pt || NULL == pt->cpu); 242 BUG_ON(NULL == pt || NULL == pt->cpu);
243 243
244 ptr = pt->cpu + startpage; 244 ptr = pt->cpu + startpage;
245 for (i = 0; i < length; i++, list++) 245 for (i = 0; i < length; i++, list = sg_next(list)) {
246 for (p = 0; p * 4096 < list->length; p++, ptr++) 246 for (p = 0; p * 4096 < list->length; p++, ptr++)
247 *ptr = cpu_to_le32(sg_dma_address(list) - list->offset); 247 *ptr = cpu_to_le32(sg_dma_address(list) +
248 list->offset + p * 4096);
249 }
248 return 0; 250 return 0;
249} 251}
250 252
@@ -258,44 +260,31 @@ void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt)
258 260
259/* ------------------------------------------------------------------ */ 261/* ------------------------------------------------------------------ */
260 262
261void saa7134_dma_free(struct videobuf_queue *q,struct saa7134_buf *buf)
262{
263 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
264 BUG_ON(in_interrupt());
265
266 videobuf_waiton(q, &buf->vb, 0, 0);
267 videobuf_dma_unmap(q->dev, dma);
268 videobuf_dma_free(dma);
269 buf->vb.state = VIDEOBUF_NEEDS_INIT;
270}
271
272/* ------------------------------------------------------------------ */
273
274int saa7134_buffer_queue(struct saa7134_dev *dev, 263int saa7134_buffer_queue(struct saa7134_dev *dev,
275 struct saa7134_dmaqueue *q, 264 struct saa7134_dmaqueue *q,
276 struct saa7134_buf *buf) 265 struct saa7134_buf *buf)
277{ 266{
278 struct saa7134_buf *next = NULL; 267 struct saa7134_buf *next = NULL;
268 unsigned long flags;
279 269
280 assert_spin_locked(&dev->slock); 270 spin_lock_irqsave(&dev->slock, flags);
281 dprintk("buffer_queue %p\n", buf); 271 dprintk("buffer_queue %p\n", buf);
282 if (NULL == q->curr) { 272 if (NULL == q->curr) {
283 if (!q->need_two) { 273 if (!q->need_two) {
284 q->curr = buf; 274 q->curr = buf;
285 buf->activate(dev, buf, NULL); 275 buf->activate(dev, buf, NULL);
286 } else if (list_empty(&q->queue)) { 276 } else if (list_empty(&q->queue)) {
287 list_add_tail(&buf->vb.queue,&q->queue); 277 list_add_tail(&buf->entry, &q->queue);
288 buf->vb.state = VIDEOBUF_QUEUED;
289 } else { 278 } else {
290 next = list_entry(q->queue.next, struct saa7134_buf, 279 next = list_entry(q->queue.next, struct saa7134_buf,
291 vb.queue); 280 entry);
292 q->curr = buf; 281 q->curr = buf;
293 buf->activate(dev, buf, next); 282 buf->activate(dev, buf, next);
294 } 283 }
295 } else { 284 } else {
296 list_add_tail(&buf->vb.queue, &q->queue); 285 list_add_tail(&buf->entry, &q->queue);
297 buf->vb.state = VIDEOBUF_QUEUED;
298 } 286 }
287 spin_unlock_irqrestore(&dev->slock, flags);
299 return 0; 288 return 0;
300} 289}
301 290
@@ -303,13 +292,12 @@ void saa7134_buffer_finish(struct saa7134_dev *dev,
303 struct saa7134_dmaqueue *q, 292 struct saa7134_dmaqueue *q,
304 unsigned int state) 293 unsigned int state)
305{ 294{
306 assert_spin_locked(&dev->slock);
307 dprintk("buffer_finish %p\n", q->curr); 295 dprintk("buffer_finish %p\n", q->curr);
308 296
309 /* finish current buffer */ 297 /* finish current buffer */
310 q->curr->vb.state = state; 298 v4l2_get_timestamp(&q->curr->vb2.v4l2_buf.timestamp);
311 v4l2_get_timestamp(&q->curr->vb.ts); 299 q->curr->vb2.v4l2_buf.sequence = q->seq_nr++;
312 wake_up(&q->curr->vb.done); 300 vb2_buffer_done(&q->curr->vb2, state);
313 q->curr = NULL; 301 q->curr = NULL;
314} 302}
315 303
@@ -323,13 +311,12 @@ void saa7134_buffer_next(struct saa7134_dev *dev,
323 311
324 if (!list_empty(&q->queue)) { 312 if (!list_empty(&q->queue)) {
325 /* activate next one from queue */ 313 /* activate next one from queue */
326 buf = list_entry(q->queue.next, struct saa7134_buf, vb.queue); 314 buf = list_entry(q->queue.next, struct saa7134_buf, entry);
327 dprintk("buffer_next %p [prev=%p/next=%p]\n", 315 dprintk("buffer_next %p [prev=%p/next=%p]\n",
328 buf, q->queue.prev, q->queue.next); 316 buf, q->queue.prev, q->queue.next);
329 list_del(&buf->vb.queue); 317 list_del(&buf->entry);
330 if (!list_empty(&q->queue)) 318 if (!list_empty(&q->queue))
331 next = list_entry(q->queue.next, struct saa7134_buf, 319 next = list_entry(q->queue.next, struct saa7134_buf, entry);
332 vb.queue);
333 q->curr = buf; 320 q->curr = buf;
334 buf->activate(dev, buf, next); 321 buf->activate(dev, buf, next);
335 dprintk("buffer_next #2 prev=%p/next=%p\n", 322 dprintk("buffer_next #2 prev=%p/next=%p\n",
@@ -339,10 +326,6 @@ void saa7134_buffer_next(struct saa7134_dev *dev,
339 dprintk("buffer_next %p\n", NULL); 326 dprintk("buffer_next %p\n", NULL);
340 saa7134_set_dmabits(dev); 327 saa7134_set_dmabits(dev);
341 del_timer(&q->timeout); 328 del_timer(&q->timeout);
342
343 if (card_has_mpeg(dev))
344 if (dev->ts_started)
345 saa7134_ts_stop(dev);
346 } 329 }
347} 330}
348 331
@@ -363,12 +346,32 @@ void saa7134_buffer_timeout(unsigned long data)
363 try to start over with the next one. */ 346 try to start over with the next one. */
364 if (q->curr) { 347 if (q->curr) {
365 dprintk("timeout on %p\n", q->curr); 348 dprintk("timeout on %p\n", q->curr);
366 saa7134_buffer_finish(dev, q, VIDEOBUF_ERROR); 349 saa7134_buffer_finish(dev, q, VB2_BUF_STATE_ERROR);
367 } 350 }
368 saa7134_buffer_next(dev, q); 351 saa7134_buffer_next(dev, q);
369 spin_unlock_irqrestore(&dev->slock, flags); 352 spin_unlock_irqrestore(&dev->slock, flags);
370} 353}
371 354
355void 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_STATE_ERROR);
366 list_del(pos);
367 tmp = NULL;
368 }
369 }
370 spin_unlock_irqrestore(&dev->slock, flags);
371 saa7134_buffer_timeout((unsigned long)q); /* also calls del_timer(&q->timeout) */
372}
373EXPORT_SYMBOL_GPL(saa7134_stop_streaming);
374
372/* ------------------------------------------------------------------ */ 375/* ------------------------------------------------------------------ */
373 376
374int saa7134_set_dmabits(struct saa7134_dev *dev) 377int saa7134_set_dmabits(struct saa7134_dev *dev)
@@ -388,7 +391,7 @@ int saa7134_set_dmabits(struct saa7134_dev *dev)
388 ctrl |= SAA7134_MAIN_CTRL_TE0; 391 ctrl |= SAA7134_MAIN_CTRL_TE0;
389 irq |= SAA7134_IRQ1_INTE_RA0_1 | 392 irq |= SAA7134_IRQ1_INTE_RA0_1 |
390 SAA7134_IRQ1_INTE_RA0_0; 393 SAA7134_IRQ1_INTE_RA0_0;
391 cap = dev->video_q.curr->vb.field; 394 cap = dev->field;
392 } 395 }
393 396
394 /* video capture -- dma 1+2 (planar modes) */ 397 /* video capture -- dma 1+2 (planar modes) */
@@ -1046,6 +1049,8 @@ static int saa7134_initdev(struct pci_dev *pci_dev,
1046 1049
1047 dev->video_dev = vdev_init(dev,&saa7134_video_template,"video"); 1050 dev->video_dev = vdev_init(dev,&saa7134_video_template,"video");
1048 dev->video_dev->ctrl_handler = &dev->ctrl_handler; 1051 dev->video_dev->ctrl_handler = &dev->ctrl_handler;
1052 dev->video_dev->lock = &dev->lock;
1053 dev->video_dev->queue = &dev->video_vbq;
1049 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER, 1054 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1050 video_nr[dev->nr]); 1055 video_nr[dev->nr]);
1051 if (err < 0) { 1056 if (err < 0) {
@@ -1058,6 +1063,8 @@ static int saa7134_initdev(struct pci_dev *pci_dev,
1058 1063
1059 dev->vbi_dev = vdev_init(dev, &saa7134_video_template, "vbi"); 1064 dev->vbi_dev = vdev_init(dev, &saa7134_video_template, "vbi");
1060 dev->vbi_dev->ctrl_handler = &dev->ctrl_handler; 1065 dev->vbi_dev->ctrl_handler = &dev->ctrl_handler;
1066 dev->vbi_dev->lock = &dev->lock;
1067 dev->vbi_dev->queue = &dev->vbi_vbq;
1061 1068
1062 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI, 1069 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1063 vbi_nr[dev->nr]); 1070 vbi_nr[dev->nr]);
@@ -1069,6 +1076,7 @@ static int saa7134_initdev(struct pci_dev *pci_dev,
1069 if (card_has_radio(dev)) { 1076 if (card_has_radio(dev)) {
1070 dev->radio_dev = vdev_init(dev,&saa7134_radio_template,"radio"); 1077 dev->radio_dev = vdev_init(dev,&saa7134_radio_template,"radio");
1071 dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler; 1078 dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
1079 dev->radio_dev->lock = &dev->lock;
1072 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO, 1080 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1073 radio_nr[dev->nr]); 1081 radio_nr[dev->nr]);
1074 if (err < 0) 1082 if (err < 0)
@@ -1188,7 +1196,7 @@ static int saa7134_buffer_requeue(struct saa7134_dev *dev,
1188 1196
1189 if (!list_empty(&q->queue)) 1197 if (!list_empty(&q->queue))
1190 next = list_entry(q->queue.next, struct saa7134_buf, 1198 next = list_entry(q->queue.next, struct saa7134_buf,
1191 vb.queue); 1199 entry);
1192 buf->activate(dev, buf, next); 1200 buf->activate(dev, buf, next);
1193 1201
1194 return 0; 1202 return 0;
diff --git a/drivers/media/pci/saa7134/saa7134-dvb.c b/drivers/media/pci/saa7134/saa7134-dvb.c
index eb48b1f097fb..73ffbabf831c 100644
--- a/drivers/media/pci/saa7134/saa7134-dvb.c
+++ b/drivers/media/pci/saa7134/saa7134-dvb.c
@@ -602,10 +602,10 @@ static int configure_tda827x_fe(struct saa7134_dev *dev,
602 struct tda1004x_config *cdec_conf, 602 struct tda1004x_config *cdec_conf,
603 struct tda827x_config *tuner_conf) 603 struct tda827x_config *tuner_conf)
604{ 604{
605 struct videobuf_dvb_frontend *fe0; 605 struct vb2_dvb_frontend *fe0;
606 606
607 /* Get the first frontend */ 607 /* Get the first frontend */
608 fe0 = videobuf_dvb_get_frontend(&dev->frontends, 1); 608 fe0 = vb2_dvb_get_frontend(&dev->frontends, 1);
609 609
610 if (!fe0) 610 if (!fe0)
611 return -EINVAL; 611 return -EINVAL;
@@ -1215,29 +1215,38 @@ static int dvb_init(struct saa7134_dev *dev)
1215{ 1215{
1216 int ret; 1216 int ret;
1217 int attach_xc3028 = 0; 1217 int attach_xc3028 = 0;
1218 struct videobuf_dvb_frontend *fe0; 1218 struct vb2_dvb_frontend *fe0;
1219 struct vb2_queue *q;
1219 1220
1220 /* FIXME: add support for multi-frontend */ 1221 /* FIXME: add support for multi-frontend */
1221 mutex_init(&dev->frontends.lock); 1222 mutex_init(&dev->frontends.lock);
1222 INIT_LIST_HEAD(&dev->frontends.felist); 1223 INIT_LIST_HEAD(&dev->frontends.felist);
1223 1224
1224 printk(KERN_INFO "%s() allocating 1 frontend\n", __func__); 1225 printk(KERN_INFO "%s() allocating 1 frontend\n", __func__);
1225 fe0 = videobuf_dvb_alloc_frontend(&dev->frontends, 1); 1226 fe0 = vb2_dvb_alloc_frontend(&dev->frontends, 1);
1226 if (!fe0) { 1227 if (!fe0) {
1227 printk(KERN_ERR "%s() failed to alloc\n", __func__); 1228 printk(KERN_ERR "%s() failed to alloc\n", __func__);
1228 return -ENOMEM; 1229 return -ENOMEM;
1229 } 1230 }
1230 1231
1231 /* init struct videobuf_dvb */ 1232 /* init struct vb2_dvb */
1232 dev->ts.nr_bufs = 32; 1233 dev->ts.nr_bufs = 32;
1233 dev->ts.nr_packets = 32*4; 1234 dev->ts.nr_packets = 32*4;
1234 fe0->dvb.name = dev->name; 1235 fe0->dvb.name = dev->name;
1235 videobuf_queue_sg_init(&fe0->dvb.dvbq, &saa7134_ts_qops, 1236 q = &fe0->dvb.dvbq;
1236 &dev->pci->dev, &dev->slock, 1237 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1237 V4L2_BUF_TYPE_VIDEO_CAPTURE, 1238 q->io_modes = VB2_MMAP | VB2_READ;
1238 V4L2_FIELD_ALTERNATE, 1239 q->drv_priv = &dev->ts_q;
1239 sizeof(struct saa7134_buf), 1240 q->ops = &saa7134_ts_qops;
1240 &dev->ts_q, NULL); 1241 q->mem_ops = &vb2_dma_sg_memops;
1242 q->buf_struct_size = sizeof(struct saa7134_buf);
1243 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1244 q->lock = &dev->lock;
1245 ret = vb2_queue_init(q);
1246 if (ret) {
1247 vb2_dvb_dealloc_frontends(&dev->frontends);
1248 return ret;
1249 }
1241 1250
1242 switch (dev->board) { 1251 switch (dev->board) {
1243 case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL: 1252 case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
@@ -1876,7 +1885,7 @@ static int dvb_init(struct saa7134_dev *dev)
1876 fe0->dvb.frontend->callback = saa7134_tuner_callback; 1885 fe0->dvb.frontend->callback = saa7134_tuner_callback;
1877 1886
1878 /* register everything else */ 1887 /* register everything else */
1879 ret = videobuf_dvb_register_bus(&dev->frontends, THIS_MODULE, dev, 1888 ret = vb2_dvb_register_bus(&dev->frontends, THIS_MODULE, dev,
1880 &dev->pci->dev, adapter_nr, 0); 1889 &dev->pci->dev, adapter_nr, 0);
1881 1890
1882 /* this sequence is necessary to make the tda1004x load its firmware 1891 /* this sequence is necessary to make the tda1004x load its firmware
@@ -1893,16 +1902,17 @@ static int dvb_init(struct saa7134_dev *dev)
1893 return ret; 1902 return ret;
1894 1903
1895detach_frontend: 1904detach_frontend:
1896 videobuf_dvb_dealloc_frontends(&dev->frontends); 1905 vb2_dvb_dealloc_frontends(&dev->frontends);
1906 vb2_queue_release(&fe0->dvb.dvbq);
1897 return -EINVAL; 1907 return -EINVAL;
1898} 1908}
1899 1909
1900static int dvb_fini(struct saa7134_dev *dev) 1910static int dvb_fini(struct saa7134_dev *dev)
1901{ 1911{
1902 struct videobuf_dvb_frontend *fe0; 1912 struct vb2_dvb_frontend *fe0;
1903 1913
1904 /* Get the first frontend */ 1914 /* Get the first frontend */
1905 fe0 = videobuf_dvb_get_frontend(&dev->frontends, 1); 1915 fe0 = vb2_dvb_get_frontend(&dev->frontends, 1);
1906 if (!fe0) 1916 if (!fe0)
1907 return -EINVAL; 1917 return -EINVAL;
1908 1918
@@ -1933,7 +1943,8 @@ static int dvb_fini(struct saa7134_dev *dev)
1933 } 1943 }
1934 } 1944 }
1935 } 1945 }
1936 videobuf_dvb_unregister_bus(&dev->frontends); 1946 vb2_dvb_unregister_bus(&dev->frontends);
1947 vb2_queue_release(&fe0->dvb.dvbq);
1937 return 0; 1948 return 0;
1938} 1949}
1939 1950
diff --git a/drivers/media/pci/saa7134/saa7134-empress.c b/drivers/media/pci/saa7134/saa7134-empress.c
index c84dece3342a..e65c760e4e8b 100644
--- a/drivers/media/pci/saa7134/saa7134-empress.c
+++ b/drivers/media/pci/saa7134/saa7134-empress.c
@@ -48,11 +48,16 @@ MODULE_PARM_DESC(debug,"enable debug messages");
48 48
49/* ------------------------------------------------------------------ */ 49/* ------------------------------------------------------------------ */
50 50
51static void ts_reset_encoder(struct saa7134_dev* dev); 51static int start_streaming(struct vb2_queue *vq, unsigned int count)
52
53static int ts_init_encoder(struct saa7134_dev* dev)
54{ 52{
53 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
54 struct saa7134_dev *dev = dmaq->dev;
55 u32 leading_null_bytes = 0; 55 u32 leading_null_bytes = 0;
56 int err;
57
58 err = saa7134_ts_start_streaming(vq, count);
59 if (err)
60 return err;
56 61
57 /* If more cards start to need this, then this 62 /* If more cards start to need this, then this
58 should probably be added to the card definitions. */ 63 should probably be added to the card definitions. */
@@ -63,109 +68,43 @@ static int ts_init_encoder(struct saa7134_dev* dev)
63 leading_null_bytes = 1; 68 leading_null_bytes = 1;
64 break; 69 break;
65 } 70 }
66 ts_reset_encoder(dev);
67 saa_call_all(dev, core, init, leading_null_bytes); 71 saa_call_all(dev, core, init, leading_null_bytes);
72 /* Unmute audio */
73 saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
74 saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6));
68 dev->empress_started = 1; 75 dev->empress_started = 1;
69 return 0; 76 return 0;
70} 77}
71 78
72static void ts_reset_encoder(struct saa7134_dev* dev) 79static void stop_streaming(struct vb2_queue *vq)
73{ 80{
74 if (!dev->empress_started) 81 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
75 return; 82 struct saa7134_dev *dev = dmaq->dev;
76 83
84 saa7134_ts_stop_streaming(vq);
77 saa_writeb(SAA7134_SPECIAL_MODE, 0x00); 85 saa_writeb(SAA7134_SPECIAL_MODE, 0x00);
78 msleep(10); 86 msleep(20);
79 saa_writeb(SAA7134_SPECIAL_MODE, 0x01); 87 saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
80 msleep(100); 88 msleep(100);
81 dev->empress_started = 0; 89 /* Mute audio */
82}
83
84/* ------------------------------------------------------------------ */
85
86static int ts_open(struct file *file)
87{
88 struct video_device *vdev = video_devdata(file);
89 struct saa7134_dev *dev = video_drvdata(file);
90 struct saa7134_fh *fh;
91
92 /* allocate + initialize per filehandle data */
93 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
94 if (NULL == fh)
95 return -ENOMEM;
96
97 v4l2_fh_init(&fh->fh, vdev);
98 file->private_data = fh;
99 fh->is_empress = true;
100 v4l2_fh_add(&fh->fh);
101
102 /* Unmute audio */
103 saa_writeb(SAA7134_AUDIO_MUTE_CTRL, 90 saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
104 saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6)); 91 saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6));
105 92 dev->empress_started = 0;
106 return 0;
107}
108
109static int ts_release(struct file *file)
110{
111 struct saa7134_dev *dev = video_drvdata(file);
112 struct saa7134_fh *fh = file->private_data;
113
114 if (res_check(fh, RESOURCE_EMPRESS)) {
115 videobuf_stop(&dev->empress_vbq);
116 videobuf_mmap_free(&dev->empress_vbq);
117
118 /* stop the encoder */
119 ts_reset_encoder(dev);
120
121 /* Mute audio */
122 saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
123 saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6));
124 }
125
126 v4l2_fh_del(&fh->fh);
127 v4l2_fh_exit(&fh->fh);
128 return 0;
129}
130
131static ssize_t
132ts_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
133{
134 struct saa7134_dev *dev = video_drvdata(file);
135
136 if (res_locked(dev, RESOURCE_EMPRESS))
137 return -EBUSY;
138 if (!dev->empress_started)
139 ts_init_encoder(dev);
140
141 return videobuf_read_stream(&dev->empress_vbq,
142 data, count, ppos, 0,
143 file->f_flags & O_NONBLOCK);
144}
145
146static unsigned int
147ts_poll(struct file *file, struct poll_table_struct *wait)
148{
149 unsigned long req_events = poll_requested_events(wait);
150 struct saa7134_dev *dev = video_drvdata(file);
151 struct saa7134_fh *fh = file->private_data;
152 unsigned int rc = 0;
153
154 if (v4l2_event_pending(&fh->fh))
155 rc = POLLPRI;
156 else if (req_events & POLLPRI)
157 poll_wait(file, &fh->fh.wait, wait);
158 return rc | videobuf_poll_stream(file, &dev->empress_vbq, wait);
159} 93}
160 94
95static struct vb2_ops saa7134_empress_qops = {
96 .queue_setup = saa7134_ts_queue_setup,
97 .buf_init = saa7134_ts_buffer_init,
98 .buf_prepare = saa7134_ts_buffer_prepare,
99 .buf_finish = saa7134_ts_buffer_finish,
100 .buf_queue = saa7134_vb2_buffer_queue,
101 .wait_prepare = vb2_ops_wait_prepare,
102 .wait_finish = vb2_ops_wait_finish,
103 .start_streaming = start_streaming,
104 .stop_streaming = stop_streaming,
105};
161 106
162static int 107/* ------------------------------------------------------------------ */
163ts_mmap(struct file *file, struct vm_area_struct * vma)
164{
165 struct saa7134_dev *dev = video_drvdata(file);
166
167 return videobuf_mmap_mapper(&dev->empress_vbq, vma);
168}
169 108
170static int empress_enum_fmt_vid_cap(struct file *file, void *priv, 109static int empress_enum_fmt_vid_cap(struct file *file, void *priv,
171 struct v4l2_fmtdesc *f) 110 struct v4l2_fmtdesc *f)
@@ -235,11 +174,11 @@ static int empress_try_fmt_vid_cap(struct file *file, void *priv,
235static const struct v4l2_file_operations ts_fops = 174static const struct v4l2_file_operations ts_fops =
236{ 175{
237 .owner = THIS_MODULE, 176 .owner = THIS_MODULE,
238 .open = ts_open, 177 .open = v4l2_fh_open,
239 .release = ts_release, 178 .release = vb2_fop_release,
240 .read = ts_read, 179 .read = vb2_fop_read,
241 .poll = ts_poll, 180 .poll = vb2_fop_poll,
242 .mmap = ts_mmap, 181 .mmap = vb2_fop_mmap,
243 .ioctl = video_ioctl2, 182 .ioctl = video_ioctl2,
244}; 183};
245 184
@@ -249,12 +188,12 @@ static const struct v4l2_ioctl_ops ts_ioctl_ops = {
249 .vidioc_try_fmt_vid_cap = empress_try_fmt_vid_cap, 188 .vidioc_try_fmt_vid_cap = empress_try_fmt_vid_cap,
250 .vidioc_s_fmt_vid_cap = empress_s_fmt_vid_cap, 189 .vidioc_s_fmt_vid_cap = empress_s_fmt_vid_cap,
251 .vidioc_g_fmt_vid_cap = empress_g_fmt_vid_cap, 190 .vidioc_g_fmt_vid_cap = empress_g_fmt_vid_cap,
252 .vidioc_reqbufs = saa7134_reqbufs, 191 .vidioc_reqbufs = vb2_ioctl_reqbufs,
253 .vidioc_querybuf = saa7134_querybuf, 192 .vidioc_querybuf = vb2_ioctl_querybuf,
254 .vidioc_qbuf = saa7134_qbuf, 193 .vidioc_qbuf = vb2_ioctl_qbuf,
255 .vidioc_dqbuf = saa7134_dqbuf, 194 .vidioc_dqbuf = vb2_ioctl_dqbuf,
256 .vidioc_streamon = saa7134_streamon, 195 .vidioc_streamon = vb2_ioctl_streamon,
257 .vidioc_streamoff = saa7134_streamoff, 196 .vidioc_streamoff = vb2_ioctl_streamoff,
258 .vidioc_g_frequency = saa7134_g_frequency, 197 .vidioc_g_frequency = saa7134_g_frequency,
259 .vidioc_s_frequency = saa7134_s_frequency, 198 .vidioc_s_frequency = saa7134_s_frequency,
260 .vidioc_g_tuner = saa7134_g_tuner, 199 .vidioc_g_tuner = saa7134_g_tuner,
@@ -317,6 +256,7 @@ static bool empress_ctrl_filter(const struct v4l2_ctrl *ctrl)
317static int empress_init(struct saa7134_dev *dev) 256static int empress_init(struct saa7134_dev *dev)
318{ 257{
319 struct v4l2_ctrl_handler *hdl = &dev->empress_ctrl_handler; 258 struct v4l2_ctrl_handler *hdl = &dev->empress_ctrl_handler;
259 struct vb2_queue *q;
320 int err; 260 int err;
321 261
322 dprintk("%s: %s\n",dev->name,__func__); 262 dprintk("%s: %s\n",dev->name,__func__);
@@ -326,6 +266,7 @@ static int empress_init(struct saa7134_dev *dev)
326 *(dev->empress_dev) = saa7134_empress_template; 266 *(dev->empress_dev) = saa7134_empress_template;
327 dev->empress_dev->v4l2_dev = &dev->v4l2_dev; 267 dev->empress_dev->v4l2_dev = &dev->v4l2_dev;
328 dev->empress_dev->release = video_device_release; 268 dev->empress_dev->release = video_device_release;
269 dev->empress_dev->lock = &dev->lock;
329 snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name), 270 snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
330 "%s empress (%s)", dev->name, 271 "%s empress (%s)", dev->name,
331 saa7134_boards[dev->board].name); 272 saa7134_boards[dev->board].name);
@@ -342,6 +283,26 @@ static int empress_init(struct saa7134_dev *dev)
342 283
343 INIT_WORK(&dev->empress_workqueue, empress_signal_update); 284 INIT_WORK(&dev->empress_workqueue, empress_signal_update);
344 285
286 q = &dev->empress_vbq;
287 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
288 /*
289 * Do not add VB2_USERPTR: the saa7134 DMA engine cannot handle
290 * transfers that do not start at the beginning of a page. A USERPTR
291 * can start anywhere in a page, so USERPTR support is a no-go.
292 */
293 q->io_modes = VB2_MMAP | VB2_READ;
294 q->drv_priv = &dev->ts_q;
295 q->ops = &saa7134_empress_qops;
296 q->gfp_flags = GFP_DMA32;
297 q->mem_ops = &vb2_dma_sg_memops;
298 q->buf_struct_size = sizeof(struct saa7134_buf);
299 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
300 q->lock = &dev->lock;
301 err = vb2_queue_init(q);
302 if (err)
303 return err;
304 dev->empress_dev->queue = q;
305
345 video_set_drvdata(dev->empress_dev, dev); 306 video_set_drvdata(dev->empress_dev, dev);
346 err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER, 307 err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
347 empress_nr[dev->nr]); 308 empress_nr[dev->nr]);
@@ -355,13 +316,6 @@ static int empress_init(struct saa7134_dev *dev)
355 printk(KERN_INFO "%s: registered device %s [mpeg]\n", 316 printk(KERN_INFO "%s: registered device %s [mpeg]\n",
356 dev->name, video_device_node_name(dev->empress_dev)); 317 dev->name, video_device_node_name(dev->empress_dev));
357 318
358 videobuf_queue_sg_init(&dev->empress_vbq, &saa7134_ts_qops,
359 &dev->pci->dev, &dev->slock,
360 V4L2_BUF_TYPE_VIDEO_CAPTURE,
361 V4L2_FIELD_ALTERNATE,
362 sizeof(struct saa7134_buf),
363 &dev->ts_q, NULL);
364
365 empress_signal_update(&dev->empress_workqueue); 319 empress_signal_update(&dev->empress_workqueue);
366 return 0; 320 return 0;
367} 321}
@@ -374,6 +328,7 @@ static int empress_fini(struct saa7134_dev *dev)
374 return 0; 328 return 0;
375 flush_work(&dev->empress_workqueue); 329 flush_work(&dev->empress_workqueue);
376 video_unregister_device(dev->empress_dev); 330 video_unregister_device(dev->empress_dev);
331 vb2_queue_release(&dev->empress_vbq);
377 v4l2_ctrl_handler_free(&dev->empress_ctrl_handler); 332 v4l2_ctrl_handler_free(&dev->empress_ctrl_handler);
378 dev->empress_dev = NULL; 333 dev->empress_dev = NULL;
379 return 0; 334 return 0;
diff --git a/drivers/media/pci/saa7134/saa7134-ts.c b/drivers/media/pci/saa7134/saa7134-ts.c
index 8ac4fdac6f97..bd25323bd947 100644
--- a/drivers/media/pci/saa7134/saa7134-ts.c
+++ b/drivers/media/pci/saa7134/saa7134-ts.c
@@ -39,26 +39,29 @@ MODULE_PARM_DESC(ts_debug,"enable debug messages [ts]");
39 printk(KERN_DEBUG "%s/ts: " fmt, dev->name , ## arg) 39 printk(KERN_DEBUG "%s/ts: " fmt, dev->name , ## arg)
40 40
41/* ------------------------------------------------------------------ */ 41/* ------------------------------------------------------------------ */
42
43static int buffer_activate(struct saa7134_dev *dev, 42static int buffer_activate(struct saa7134_dev *dev,
44 struct saa7134_buf *buf, 43 struct saa7134_buf *buf,
45 struct saa7134_buf *next) 44 struct saa7134_buf *next)
46{ 45{
47 46
48 dprintk("buffer_activate [%p]",buf); 47 dprintk("buffer_activate [%p]",buf);
49 buf->vb.state = VIDEOBUF_ACTIVE;
50 buf->top_seen = 0; 48 buf->top_seen = 0;
51 49
50 if (!dev->ts_started)
51 dev->ts_field = V4L2_FIELD_TOP;
52
52 if (NULL == next) 53 if (NULL == next)
53 next = buf; 54 next = buf;
54 if (V4L2_FIELD_TOP == buf->vb.field) { 55 if (V4L2_FIELD_TOP == dev->ts_field) {
55 dprintk("- [top] buf=%p next=%p\n",buf,next); 56 dprintk("- [top] buf=%p next=%p\n",buf,next);
56 saa_writel(SAA7134_RS_BA1(5),saa7134_buffer_base(buf)); 57 saa_writel(SAA7134_RS_BA1(5),saa7134_buffer_base(buf));
57 saa_writel(SAA7134_RS_BA2(5),saa7134_buffer_base(next)); 58 saa_writel(SAA7134_RS_BA2(5),saa7134_buffer_base(next));
59 dev->ts_field = V4L2_FIELD_BOTTOM;
58 } else { 60 } else {
59 dprintk("- [bottom] buf=%p next=%p\n",buf,next); 61 dprintk("- [bottom] buf=%p next=%p\n",buf,next);
60 saa_writel(SAA7134_RS_BA1(5),saa7134_buffer_base(next)); 62 saa_writel(SAA7134_RS_BA1(5),saa7134_buffer_base(next));
61 saa_writel(SAA7134_RS_BA2(5),saa7134_buffer_base(buf)); 63 saa_writel(SAA7134_RS_BA2(5),saa7134_buffer_base(buf));
64 dev->ts_field = V4L2_FIELD_TOP;
62 } 65 }
63 66
64 /* start DMA */ 67 /* start DMA */
@@ -72,99 +75,123 @@ static int buffer_activate(struct saa7134_dev *dev,
72 return 0; 75 return 0;
73} 76}
74 77
75static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb, 78int saa7134_ts_buffer_init(struct vb2_buffer *vb2)
76 enum v4l2_field field) 79{
80 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
81 struct saa7134_buf *buf = container_of(vb2, struct saa7134_buf, vb2);
82
83 dmaq->curr = NULL;
84 buf->activate = buffer_activate;
85
86 return 0;
87}
88EXPORT_SYMBOL_GPL(saa7134_ts_buffer_init);
89
90int saa7134_ts_buffer_prepare(struct vb2_buffer *vb2)
77{ 91{
78 struct saa7134_dmaqueue *dmaq = q->priv_data; 92 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
79 struct saa7134_dev *dev = dmaq->dev; 93 struct saa7134_dev *dev = dmaq->dev;
80 struct saa7134_buf *buf = container_of(vb, struct saa7134_buf, vb); 94 struct saa7134_buf *buf = container_of(vb2, struct saa7134_buf, vb2);
95 struct sg_table *dma = vb2_dma_sg_plane_desc(vb2, 0);
81 unsigned int lines, llength, size; 96 unsigned int lines, llength, size;
82 int err; 97 int ret;
83 98
84 dprintk("buffer_prepare [%p,%s]\n", buf, v4l2_field_names[field]); 99 dprintk("buffer_prepare [%p]\n", buf);
85 100
86 llength = TS_PACKET_SIZE; 101 llength = TS_PACKET_SIZE;
87 lines = dev->ts.nr_packets; 102 lines = dev->ts.nr_packets;
88 103
89 size = lines * llength; 104 size = lines * llength;
90 if (0 != buf->vb.baddr && buf->vb.bsize < size) 105 if (vb2_plane_size(vb2, 0) < size)
91 return -EINVAL; 106 return -EINVAL;
92 107
93 if (buf->vb.size != size) { 108 vb2_set_plane_payload(vb2, 0, size);
94 saa7134_dma_free(q,buf); 109 vb2->v4l2_buf.field = dev->field;
95 }
96
97 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
98 110
99 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb); 111 ret = dma_map_sg(&dev->pci->dev, dma->sgl, dma->nents, DMA_FROM_DEVICE);
100 112 if (!ret)
101 dprintk("buffer_prepare: needs_init\n"); 113 return -EIO;
102 114 return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents,
103 buf->vb.width = llength; 115 saa7134_buffer_startpage(buf));
104 buf->vb.height = lines;
105 buf->vb.size = size;
106
107 err = videobuf_iolock(q,&buf->vb,NULL);
108 if (err)
109 goto oops;
110 err = saa7134_pgtable_build(dev->pci, &dmaq->pt,
111 dma->sglist,
112 dma->sglen,
113 saa7134_buffer_startpage(buf));
114 if (err)
115 goto oops;
116 }
117
118 buf->vb.state = VIDEOBUF_PREPARED;
119 buf->activate = buffer_activate;
120 buf->vb.field = field;
121 return 0;
122
123 oops:
124 saa7134_dma_free(q,buf);
125 return err;
126} 116}
117EXPORT_SYMBOL_GPL(saa7134_ts_buffer_prepare);
127 118
128static int 119void saa7134_ts_buffer_finish(struct vb2_buffer *vb2)
129buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
130{ 120{
131 struct saa7134_dmaqueue *dmaq = q->priv_data; 121 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
132 struct saa7134_dev *dev = dmaq->dev; 122 struct saa7134_dev *dev = dmaq->dev;
123 struct saa7134_buf *buf = container_of(vb2, struct saa7134_buf, vb2);
124 struct sg_table *dma = vb2_dma_sg_plane_desc(&buf->vb2, 0);
133 125
134 *size = TS_PACKET_SIZE * dev->ts.nr_packets; 126 dma_unmap_sg(&dev->pci->dev, dma->sgl, dma->nents, DMA_FROM_DEVICE);
135 if (0 == *count) 127}
136 *count = dev->ts.nr_bufs; 128EXPORT_SYMBOL_GPL(saa7134_ts_buffer_finish);
137 *count = saa7134_buffer_count(*size,*count);
138 129
130int saa7134_ts_queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
131 unsigned int *nbuffers, unsigned int *nplanes,
132 unsigned int sizes[], void *alloc_ctxs[])
133{
134 struct saa7134_dmaqueue *dmaq = q->drv_priv;
135 struct saa7134_dev *dev = dmaq->dev;
136 int size = TS_PACKET_SIZE * dev->ts.nr_packets;
137
138 if (0 == *nbuffers)
139 *nbuffers = dev->ts.nr_bufs;
140 *nbuffers = saa7134_buffer_count(size, *nbuffers);
141 if (*nbuffers < 3)
142 *nbuffers = 3;
143 *nplanes = 1;
144 sizes[0] = size;
139 return 0; 145 return 0;
140} 146}
147EXPORT_SYMBOL_GPL(saa7134_ts_queue_setup);
141 148
142static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb) 149int saa7134_ts_start_streaming(struct vb2_queue *vq, unsigned int count)
143{ 150{
144 struct saa7134_dmaqueue *dmaq = q->priv_data; 151 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
145 struct saa7134_dev *dev = dmaq->dev; 152 struct saa7134_dev *dev = dmaq->dev;
146 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
147 153
148 saa7134_buffer_queue(dev,&dev->ts_q,buf); 154 /*
155 * Planar video capture and TS share the same DMA channel,
156 * so only one can be active at a time.
157 */
158 if (vb2_is_busy(&dev->video_vbq) && dev->fmt->planar) {
159 struct saa7134_buf *buf, *tmp;
160
161 list_for_each_entry_safe(buf, tmp, &dmaq->queue, entry) {
162 list_del(&buf->entry);
163 vb2_buffer_done(&buf->vb2, VB2_BUF_STATE_QUEUED);
164 }
165 if (dmaq->curr) {
166 vb2_buffer_done(&dmaq->curr->vb2, VB2_BUF_STATE_QUEUED);
167 dmaq->curr = NULL;
168 }
169 return -EBUSY;
170 }
171 dmaq->seq_nr = 0;
172 return 0;
149} 173}
174EXPORT_SYMBOL_GPL(saa7134_ts_start_streaming);
150 175
151static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb) 176void saa7134_ts_stop_streaming(struct vb2_queue *vq)
152{ 177{
153 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb); 178 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
154 struct saa7134_dmaqueue *dmaq = q->priv_data;
155 struct saa7134_dev *dev = dmaq->dev; 179 struct saa7134_dev *dev = dmaq->dev;
156 180
157 if (dev->ts_started) 181 saa7134_ts_stop(dev);
158 saa7134_ts_stop(dev); 182 saa7134_stop_streaming(dev, dmaq);
159
160 saa7134_dma_free(q,buf);
161} 183}
162 184EXPORT_SYMBOL_GPL(saa7134_ts_stop_streaming);
163struct videobuf_queue_ops saa7134_ts_qops = { 185
164 .buf_setup = buffer_setup, 186struct vb2_ops saa7134_ts_qops = {
165 .buf_prepare = buffer_prepare, 187 .queue_setup = saa7134_ts_queue_setup,
166 .buf_queue = buffer_queue, 188 .buf_init = saa7134_ts_buffer_init,
167 .buf_release = buffer_release, 189 .buf_prepare = saa7134_ts_buffer_prepare,
190 .buf_finish = saa7134_ts_buffer_finish,
191 .buf_queue = saa7134_vb2_buffer_queue,
192 .wait_prepare = vb2_ops_wait_prepare,
193 .wait_finish = vb2_ops_wait_finish,
194 .stop_streaming = saa7134_ts_stop_streaming,
168}; 195};
169EXPORT_SYMBOL_GPL(saa7134_ts_qops); 196EXPORT_SYMBOL_GPL(saa7134_ts_qops);
170 197
@@ -229,7 +256,8 @@ int saa7134_ts_stop(struct saa7134_dev *dev)
229{ 256{
230 dprintk("TS stop\n"); 257 dprintk("TS stop\n");
231 258
232 BUG_ON(!dev->ts_started); 259 if (!dev->ts_started)
260 return 0;
233 261
234 /* Stop TS stream */ 262 /* Stop TS stream */
235 switch (saa7134_boards[dev->board].ts_type) { 263 switch (saa7134_boards[dev->board].ts_type) {
@@ -250,7 +278,8 @@ int saa7134_ts_start(struct saa7134_dev *dev)
250{ 278{
251 dprintk("TS start\n"); 279 dprintk("TS start\n");
252 280
253 BUG_ON(dev->ts_started); 281 if (WARN_ON(dev->ts_started))
282 return 0;
254 283
255 /* dma: setup channel 5 (= TS) */ 284 /* dma: setup channel 5 (= TS) */
256 saa_writeb(SAA7134_TS_DMA0, (dev->ts.nr_packets - 1) & 0xff); 285 saa_writeb(SAA7134_TS_DMA0, (dev->ts.nr_packets - 1) & 0xff);
@@ -306,15 +335,15 @@ void saa7134_irq_ts_done(struct saa7134_dev *dev, unsigned long status)
306 335
307 spin_lock(&dev->slock); 336 spin_lock(&dev->slock);
308 if (dev->ts_q.curr) { 337 if (dev->ts_q.curr) {
309 field = dev->ts_q.curr->vb.field; 338 field = dev->ts_field;
310 if (field == V4L2_FIELD_TOP) { 339 if (field != V4L2_FIELD_TOP) {
311 if ((status & 0x100000) != 0x000000) 340 if ((status & 0x100000) != 0x000000)
312 goto done; 341 goto done;
313 } else { 342 } else {
314 if ((status & 0x100000) != 0x100000) 343 if ((status & 0x100000) != 0x100000)
315 goto done; 344 goto done;
316 } 345 }
317 saa7134_buffer_finish(dev, &dev->ts_q, VIDEOBUF_DONE); 346 saa7134_buffer_finish(dev, &dev->ts_q, VB2_BUF_STATE_DONE);
318 } 347 }
319 saa7134_buffer_next(dev,&dev->ts_q); 348 saa7134_buffer_next(dev,&dev->ts_q);
320 349
diff --git a/drivers/media/pci/saa7134/saa7134-vbi.c b/drivers/media/pci/saa7134/saa7134-vbi.c
index 4954a5483299..4479af5e4b9c 100644
--- a/drivers/media/pci/saa7134/saa7134-vbi.c
+++ b/drivers/media/pci/saa7134/saa7134-vbi.c
@@ -81,10 +81,10 @@ static int buffer_activate(struct saa7134_dev *dev,
81 struct saa7134_buf *buf, 81 struct saa7134_buf *buf,
82 struct saa7134_buf *next) 82 struct saa7134_buf *next)
83{ 83{
84 struct saa7134_dmaqueue *dmaq = buf->vb2.vb2_queue->drv_priv;
84 unsigned long control, base; 85 unsigned long control, base;
85 86
86 dprintk("buffer_activate [%p]\n", buf); 87 dprintk("buffer_activate [%p]\n", buf);
87 buf->vb.state = VIDEOBUF_ACTIVE;
88 buf->top_seen = 0; 88 buf->top_seen = 0;
89 89
90 task_init(dev, buf, TASK_A); 90 task_init(dev, buf, TASK_A);
@@ -96,7 +96,7 @@ static int buffer_activate(struct saa7134_dev *dev,
96 base = saa7134_buffer_base(buf); 96 base = saa7134_buffer_base(buf);
97 control = SAA7134_RS_CONTROL_BURST_16 | 97 control = SAA7134_RS_CONTROL_BURST_16 |
98 SAA7134_RS_CONTROL_ME | 98 SAA7134_RS_CONTROL_ME |
99 (dev->vbi_q.pt.dma >> 12); 99 (dmaq->pt.dma >> 12);
100 saa_writel(SAA7134_RS_BA1(2), base); 100 saa_writel(SAA7134_RS_BA1(2), base);
101 saa_writel(SAA7134_RS_BA2(2), base + dev->vbi_hlen * dev->vbi_vlen); 101 saa_writel(SAA7134_RS_BA2(2), base + dev->vbi_hlen * dev->vbi_vlen);
102 saa_writel(SAA7134_RS_PITCH(2), dev->vbi_hlen); 102 saa_writel(SAA7134_RS_PITCH(2), dev->vbi_hlen);
@@ -108,93 +108,83 @@ static int buffer_activate(struct saa7134_dev *dev,
108 108
109 /* start DMA */ 109 /* start DMA */
110 saa7134_set_dmabits(dev); 110 saa7134_set_dmabits(dev);
111 mod_timer(&dev->vbi_q.timeout, jiffies + BUFFER_TIMEOUT); 111 mod_timer(&dmaq->timeout, jiffies + BUFFER_TIMEOUT);
112 112
113 return 0; 113 return 0;
114} 114}
115 115
116static int buffer_prepare(struct videobuf_queue *q, 116static int buffer_prepare(struct vb2_buffer *vb2)
117 struct videobuf_buffer *vb,
118 enum v4l2_field field)
119{ 117{
120 struct saa7134_dmaqueue *dmaq = q->priv_data; 118 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
121 struct saa7134_dev *dev = dmaq->dev; 119 struct saa7134_dev *dev = dmaq->dev;
122 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb); 120 struct saa7134_buf *buf = container_of(vb2, struct saa7134_buf, vb2);
121 struct sg_table *dma = vb2_dma_sg_plane_desc(&buf->vb2, 0);
123 unsigned int size; 122 unsigned int size;
124 int err; 123 int ret;
125 124
126 size = dev->vbi_hlen * dev->vbi_vlen * 2; 125 size = dev->vbi_hlen * dev->vbi_vlen * 2;
127 if (0 != buf->vb.baddr && buf->vb.bsize < size) 126 if (vb2_plane_size(vb2, 0) < size)
128 return -EINVAL; 127 return -EINVAL;
129 128
130 if (buf->vb.size != size) 129 vb2_set_plane_payload(vb2, 0, size);
131 saa7134_dma_free(q,buf);
132
133 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
134 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
135
136 buf->vb.width = dev->vbi_hlen;
137 buf->vb.height = dev->vbi_vlen;
138 buf->vb.size = size;
139
140 err = videobuf_iolock(q,&buf->vb,NULL);
141 if (err)
142 goto oops;
143 err = saa7134_pgtable_build(dev->pci, &dmaq->pt,
144 dma->sglist,
145 dma->sglen,
146 saa7134_buffer_startpage(buf));
147 if (err)
148 goto oops;
149 }
150 buf->vb.state = VIDEOBUF_PREPARED;
151 buf->activate = buffer_activate;
152 buf->vb.field = field;
153 return 0;
154 130
155 oops: 131 ret = dma_map_sg(&dev->pci->dev, dma->sgl, dma->nents, DMA_FROM_DEVICE);
156 saa7134_dma_free(q,buf); 132 if (!ret)
157 return err; 133 return -EIO;
134 return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents,
135 saa7134_buffer_startpage(buf));
158} 136}
159 137
160static int 138static int queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
161buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size) 139 unsigned int *nbuffers, unsigned int *nplanes,
140 unsigned int sizes[], void *alloc_ctxs[])
162{ 141{
163 struct saa7134_dmaqueue *dmaq = q->priv_data; 142 struct saa7134_dmaqueue *dmaq = q->drv_priv;
164 struct saa7134_dev *dev = dmaq->dev; 143 struct saa7134_dev *dev = dmaq->dev;
144 unsigned int size;
165 145
166 dev->vbi_vlen = dev->tvnorm->vbi_v_stop_0 - dev->tvnorm->vbi_v_start_0 + 1; 146 dev->vbi_vlen = dev->tvnorm->vbi_v_stop_0 - dev->tvnorm->vbi_v_start_0 + 1;
167 if (dev->vbi_vlen > VBI_LINE_COUNT) 147 if (dev->vbi_vlen > VBI_LINE_COUNT)
168 dev->vbi_vlen = VBI_LINE_COUNT; 148 dev->vbi_vlen = VBI_LINE_COUNT;
169 dev->vbi_hlen = VBI_LINE_LENGTH; 149 dev->vbi_hlen = VBI_LINE_LENGTH;
170 *size = dev->vbi_hlen * dev->vbi_vlen * 2; 150 size = dev->vbi_hlen * dev->vbi_vlen * 2;
171 if (0 == *count) 151
172 *count = vbibufs; 152 *nbuffers = saa7134_buffer_count(size, *nbuffers);
173 *count = saa7134_buffer_count(*size,*count); 153 *nplanes = 1;
154 sizes[0] = size;
174 return 0; 155 return 0;
175} 156}
176 157
177static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb) 158static int buffer_init(struct vb2_buffer *vb2)
178{ 159{
179 struct saa7134_dmaqueue *dmaq = q->priv_data; 160 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
180 struct saa7134_dev *dev = dmaq->dev; 161 struct saa7134_buf *buf = container_of(vb2, struct saa7134_buf, vb2);
181 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
182 162
183 saa7134_buffer_queue(dev,&dev->vbi_q,buf); 163 dmaq->curr = NULL;
164 buf->activate = buffer_activate;
165 return 0;
184} 166}
185 167
186static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb) 168static void buffer_finish(struct vb2_buffer *vb2)
187{ 169{
188 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb); 170 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
171 struct saa7134_dev *dev = dmaq->dev;
172 struct saa7134_buf *buf = container_of(vb2, struct saa7134_buf, vb2);
173 struct sg_table *dma = vb2_dma_sg_plane_desc(&buf->vb2, 0);
189 174
190 saa7134_dma_free(q,buf); 175 dma_unmap_sg(&dev->pci->dev, dma->sgl, dma->nents, DMA_FROM_DEVICE);
191} 176}
192 177
193struct videobuf_queue_ops saa7134_vbi_qops = { 178struct vb2_ops saa7134_vbi_qops = {
194 .buf_setup = buffer_setup, 179 .queue_setup = queue_setup,
195 .buf_prepare = buffer_prepare, 180 .buf_init = buffer_init,
196 .buf_queue = buffer_queue, 181 .buf_prepare = buffer_prepare,
197 .buf_release = buffer_release, 182 .buf_finish = buffer_finish,
183 .buf_queue = saa7134_vb2_buffer_queue,
184 .wait_prepare = vb2_ops_wait_prepare,
185 .wait_finish = vb2_ops_wait_finish,
186 .start_streaming = saa7134_vb2_start_streaming,
187 .stop_streaming = saa7134_vb2_stop_streaming,
198}; 188};
199 189
200/* ------------------------------------------------------------------ */ 190/* ------------------------------------------------------------------ */
@@ -224,7 +214,6 @@ void saa7134_irq_vbi_done(struct saa7134_dev *dev, unsigned long status)
224{ 214{
225 spin_lock(&dev->slock); 215 spin_lock(&dev->slock);
226 if (dev->vbi_q.curr) { 216 if (dev->vbi_q.curr) {
227 dev->vbi_fieldcount++;
228 /* make sure we have seen both fields */ 217 /* make sure we have seen both fields */
229 if ((status & 0x10) == 0x00) { 218 if ((status & 0x10) == 0x00) {
230 dev->vbi_q.curr->top_seen = 1; 219 dev->vbi_q.curr->top_seen = 1;
@@ -233,8 +222,7 @@ void saa7134_irq_vbi_done(struct saa7134_dev *dev, unsigned long status)
233 if (!dev->vbi_q.curr->top_seen) 222 if (!dev->vbi_q.curr->top_seen)
234 goto done; 223 goto done;
235 224
236 dev->vbi_q.curr->vb.field_count = dev->vbi_fieldcount; 225 saa7134_buffer_finish(dev, &dev->vbi_q, VB2_BUF_STATE_DONE);
237 saa7134_buffer_finish(dev, &dev->vbi_q, VIDEOBUF_DONE);
238 } 226 }
239 saa7134_buffer_next(dev, &dev->vbi_q); 227 saa7134_buffer_next(dev, &dev->vbi_q);
240 228
diff --git a/drivers/media/pci/saa7134/saa7134-video.c b/drivers/media/pci/saa7134/saa7134-video.c
index 26cb7a56635d..1f63f95c9a82 100644
--- a/drivers/media/pci/saa7134/saa7134-video.c
+++ b/drivers/media/pci/saa7134/saa7134-video.c
@@ -381,42 +381,6 @@ static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
381 return NULL; 381 return NULL;
382} 382}
383 383
384/* ----------------------------------------------------------------------- */
385/* resource management */
386
387static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
388{
389 if (fh->resources & bit)
390 /* have it already allocated */
391 return 1;
392
393 /* is it free? */
394 mutex_lock(&dev->lock);
395 if (dev->resources & bit) {
396 /* no, someone else uses it */
397 mutex_unlock(&dev->lock);
398 return 0;
399 }
400 /* it's free, grab it */
401 fh->resources |= bit;
402 dev->resources |= bit;
403 dprintk("res: get %d\n",bit);
404 mutex_unlock(&dev->lock);
405 return 1;
406}
407
408static
409void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
410{
411 BUG_ON((fh->resources & bits) != bits);
412
413 mutex_lock(&dev->lock);
414 fh->resources &= ~bits;
415 dev->resources &= ~bits;
416 dprintk("res: put %d\n",bits);
417 mutex_unlock(&dev->lock);
418}
419
420/* ------------------------------------------------------------------ */ 384/* ------------------------------------------------------------------ */
421 385
422static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm) 386static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
@@ -824,15 +788,15 @@ static int buffer_activate(struct saa7134_dev *dev,
824 struct saa7134_buf *buf, 788 struct saa7134_buf *buf,
825 struct saa7134_buf *next) 789 struct saa7134_buf *next)
826{ 790{
791 struct saa7134_dmaqueue *dmaq = buf->vb2.vb2_queue->drv_priv;
827 unsigned long base,control,bpl; 792 unsigned long base,control,bpl;
828 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */ 793 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
829 794
830 dprintk("buffer_activate buf=%p\n",buf); 795 dprintk("buffer_activate buf=%p\n",buf);
831 buf->vb.state = VIDEOBUF_ACTIVE;
832 buf->top_seen = 0; 796 buf->top_seen = 0;
833 797
834 set_size(dev, TASK_A, buf->vb.width, buf->vb.height, 798 set_size(dev, TASK_A, dev->width, dev->height,
835 V4L2_FIELD_HAS_BOTH(buf->vb.field)); 799 V4L2_FIELD_HAS_BOTH(dev->field));
836 if (dev->fmt->yuv) 800 if (dev->fmt->yuv)
837 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03); 801 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
838 else 802 else
@@ -842,17 +806,17 @@ static int buffer_activate(struct saa7134_dev *dev,
842 /* DMA: setup channel 0 (= Video Task A0) */ 806 /* DMA: setup channel 0 (= Video Task A0) */
843 base = saa7134_buffer_base(buf); 807 base = saa7134_buffer_base(buf);
844 if (dev->fmt->planar) 808 if (dev->fmt->planar)
845 bpl = buf->vb.width; 809 bpl = dev->width;
846 else 810 else
847 bpl = (buf->vb.width * dev->fmt->depth) / 8; 811 bpl = (dev->width * dev->fmt->depth) / 8;
848 control = SAA7134_RS_CONTROL_BURST_16 | 812 control = SAA7134_RS_CONTROL_BURST_16 |
849 SAA7134_RS_CONTROL_ME | 813 SAA7134_RS_CONTROL_ME |
850 (dev->video_q.pt.dma >> 12); 814 (dmaq->pt.dma >> 12);
851 if (dev->fmt->bswap) 815 if (dev->fmt->bswap)
852 control |= SAA7134_RS_CONTROL_BSWAP; 816 control |= SAA7134_RS_CONTROL_BSWAP;
853 if (dev->fmt->wswap) 817 if (dev->fmt->wswap)
854 control |= SAA7134_RS_CONTROL_WSWAP; 818 control |= SAA7134_RS_CONTROL_WSWAP;
855 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) { 819 if (V4L2_FIELD_HAS_BOTH(dev->field)) {
856 /* interlaced */ 820 /* interlaced */
857 saa_writel(SAA7134_RS_BA1(0),base); 821 saa_writel(SAA7134_RS_BA1(0),base);
858 saa_writel(SAA7134_RS_BA2(0),base+bpl); 822 saa_writel(SAA7134_RS_BA2(0),base+bpl);
@@ -868,14 +832,14 @@ static int buffer_activate(struct saa7134_dev *dev,
868 if (dev->fmt->planar) { 832 if (dev->fmt->planar) {
869 /* DMA: setup channel 4+5 (= planar task A) */ 833 /* DMA: setup channel 4+5 (= planar task A) */
870 bpl_uv = bpl >> dev->fmt->hshift; 834 bpl_uv = bpl >> dev->fmt->hshift;
871 lines_uv = buf->vb.height >> dev->fmt->vshift; 835 lines_uv = dev->height >> dev->fmt->vshift;
872 base2 = base + bpl * buf->vb.height; 836 base2 = base + bpl * dev->height;
873 base3 = base2 + bpl_uv * lines_uv; 837 base3 = base2 + bpl_uv * lines_uv;
874 if (dev->fmt->uvswap) 838 if (dev->fmt->uvswap)
875 tmp = base2, base2 = base3, base3 = tmp; 839 tmp = base2, base2 = base3, base3 = tmp;
876 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n", 840 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
877 bpl_uv,lines_uv,base2,base3); 841 bpl_uv,lines_uv,base2,base3);
878 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) { 842 if (V4L2_FIELD_HAS_BOTH(dev->field)) {
879 /* interlaced */ 843 /* interlaced */
880 saa_writel(SAA7134_RS_BA1(4),base2); 844 saa_writel(SAA7134_RS_BA1(4),base2);
881 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv); 845 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
@@ -898,23 +862,61 @@ static int buffer_activate(struct saa7134_dev *dev,
898 862
899 /* start DMA */ 863 /* start DMA */
900 saa7134_set_dmabits(dev); 864 saa7134_set_dmabits(dev);
901 mod_timer(&dev->video_q.timeout, jiffies + BUFFER_TIMEOUT); 865 mod_timer(&dmaq->timeout, jiffies + BUFFER_TIMEOUT);
902 return 0; 866 return 0;
903} 867}
904 868
905static int buffer_prepare(struct videobuf_queue *q, 869static int buffer_init(struct vb2_buffer *vb2)
906 struct videobuf_buffer *vb,
907 enum v4l2_field field)
908{ 870{
909 struct saa7134_dmaqueue *dmaq = q->priv_data; 871 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
872 struct saa7134_buf *buf = container_of(vb2, struct saa7134_buf, vb2);
873
874 dmaq->curr = NULL;
875 buf->activate = buffer_activate;
876 return 0;
877}
878
879static int buffer_prepare(struct vb2_buffer *vb2)
880{
881 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
910 struct saa7134_dev *dev = dmaq->dev; 882 struct saa7134_dev *dev = dmaq->dev;
911 struct saa7134_buf *buf = container_of(vb, struct saa7134_buf, vb); 883 struct saa7134_buf *buf = container_of(vb2, struct saa7134_buf, vb2);
884 struct sg_table *dma = vb2_dma_sg_plane_desc(&buf->vb2, 0);
912 unsigned int size; 885 unsigned int size;
913 int err; 886 int ret;
914 887
915 /* sanity checks */ 888 size = (dev->width * dev->height * dev->fmt->depth) >> 3;
916 if (NULL == dev->fmt) 889 if (vb2_plane_size(vb2, 0) < size)
917 return -EINVAL; 890 return -EINVAL;
891
892 vb2_set_plane_payload(vb2, 0, size);
893 vb2->v4l2_buf.field = dev->field;
894
895 ret = dma_map_sg(&dev->pci->dev, dma->sgl, dma->nents, DMA_FROM_DEVICE);
896 if (!ret)
897 return -EIO;
898 return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents,
899 saa7134_buffer_startpage(buf));
900}
901
902static void buffer_finish(struct vb2_buffer *vb2)
903{
904 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
905 struct saa7134_dev *dev = dmaq->dev;
906 struct saa7134_buf *buf = container_of(vb2, struct saa7134_buf, vb2);
907 struct sg_table *dma = vb2_dma_sg_plane_desc(&buf->vb2, 0);
908
909 dma_unmap_sg(&dev->pci->dev, dma->sgl, dma->nents, DMA_FROM_DEVICE);
910}
911
912static int queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
913 unsigned int *nbuffers, unsigned int *nplanes,
914 unsigned int sizes[], void *alloc_ctxs[])
915{
916 struct saa7134_dmaqueue *dmaq = q->drv_priv;
917 struct saa7134_dev *dev = dmaq->dev;
918 int size = dev->fmt->depth * dev->width * dev->height >> 3;
919
918 if (dev->width < 48 || 920 if (dev->width < 48 ||
919 dev->height < 32 || 921 dev->height < 32 ||
920 dev->width/4 > dev->crop_current.width || 922 dev->width/4 > dev->crop_current.width ||
@@ -922,82 +924,88 @@ static int buffer_prepare(struct videobuf_queue *q,
922 dev->width > dev->crop_bounds.width || 924 dev->width > dev->crop_bounds.width ||
923 dev->height > dev->crop_bounds.height) 925 dev->height > dev->crop_bounds.height)
924 return -EINVAL; 926 return -EINVAL;
925 size = (dev->width * dev->height * dev->fmt->depth) >> 3;
926 if (0 != buf->vb.baddr && buf->vb.bsize < size)
927 return -EINVAL;
928 927
929 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n", 928 *nbuffers = saa7134_buffer_count(size, *nbuffers);
930 vb->i, dev->width, dev->height, size, v4l2_field_names[field], 929 *nplanes = 1;
931 dev->fmt->name); 930 sizes[0] = size;
932 if (buf->vb.width != dev->width ||
933 buf->vb.height != dev->height ||
934 buf->vb.size != size ||
935 buf->vb.field != field) {
936 saa7134_dma_free(q,buf);
937 }
938
939 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
940 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
941
942 buf->vb.width = dev->width;
943 buf->vb.height = dev->height;
944 buf->vb.size = size;
945 buf->vb.field = field;
946 dev->video_q.curr = NULL;
947
948 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
949 if (err)
950 goto oops;
951 err = saa7134_pgtable_build(dev->pci, &dmaq->pt,
952 dma->sglist,
953 dma->sglen,
954 saa7134_buffer_startpage(buf));
955 if (err)
956 goto oops;
957 }
958 buf->vb.state = VIDEOBUF_PREPARED;
959 buf->activate = buffer_activate;
960 return 0; 931 return 0;
961
962 oops:
963 saa7134_dma_free(q,buf);
964 return err;
965} 932}
966 933
967static int 934/*
968buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size) 935 * move buffer to hardware queue
936 */
937void saa7134_vb2_buffer_queue(struct vb2_buffer *vb)
969{ 938{
970 struct saa7134_dmaqueue *dmaq = q->priv_data; 939 struct saa7134_dmaqueue *dmaq = vb->vb2_queue->drv_priv;
971 struct saa7134_dev *dev = dmaq->dev; 940 struct saa7134_dev *dev = dmaq->dev;
941 struct saa7134_buf *buf = container_of(vb, struct saa7134_buf, vb2);
972 942
973 *size = dev->fmt->depth * dev->width * dev->height >> 3; 943 saa7134_buffer_queue(dev, dmaq, buf);
974 if (0 == *count)
975 *count = gbuffers;
976 *count = saa7134_buffer_count(*size,*count);
977 return 0;
978} 944}
945EXPORT_SYMBOL_GPL(saa7134_vb2_buffer_queue);
979 946
980static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb) 947int saa7134_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
981{ 948{
982 struct saa7134_dmaqueue *dmaq = q->priv_data; 949 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
983 struct saa7134_dev *dev = dmaq->dev; 950 struct saa7134_dev *dev = dmaq->dev;
984 struct saa7134_buf *buf = container_of(vb, struct saa7134_buf, vb);
985 951
986 saa7134_buffer_queue(dev, &dev->video_q, buf); 952 /*
953 * Planar video capture and TS share the same DMA channel,
954 * so only one can be active at a time.
955 */
956 if (card_is_empress(dev) && vb2_is_busy(&dev->empress_vbq) &&
957 dmaq == &dev->video_q && dev->fmt->planar) {
958 struct saa7134_buf *buf, *tmp;
959
960 list_for_each_entry_safe(buf, tmp, &dmaq->queue, entry) {
961 list_del(&buf->entry);
962 vb2_buffer_done(&buf->vb2, VB2_BUF_STATE_QUEUED);
963 }
964 if (dmaq->curr) {
965 vb2_buffer_done(&dmaq->curr->vb2, VB2_BUF_STATE_QUEUED);
966 dmaq->curr = NULL;
967 }
968 return -EBUSY;
969 }
970
971 /* The SAA7134 has a 1K FIFO; the datasheet suggests that when
972 * configured conservatively, there's 22 usec of buffering for video.
973 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
974 * margin in case the FIFO is configured differently to the datasheet.
975 * Unfortunately, I lack register-level documentation to check the
976 * Linux FIFO setup and confirm the perfect value.
977 */
978 if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
979 (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
980 pm_qos_add_request(&dev->qos_request,
981 PM_QOS_CPU_DMA_LATENCY, 20);
982 dmaq->seq_nr = 0;
983
984 return 0;
987} 985}
988 986
989static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb) 987void saa7134_vb2_stop_streaming(struct vb2_queue *vq)
990{ 988{
991 struct saa7134_buf *buf = container_of(vb, struct saa7134_buf, vb); 989 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
990 struct saa7134_dev *dev = dmaq->dev;
991
992 saa7134_stop_streaming(dev, dmaq);
992 993
993 saa7134_dma_free(q,buf); 994 if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
995 (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
996 pm_qos_remove_request(&dev->qos_request);
994} 997}
995 998
996static struct videobuf_queue_ops video_qops = { 999static struct vb2_ops vb2_qops = {
997 .buf_setup = buffer_setup, 1000 .queue_setup = queue_setup,
998 .buf_prepare = buffer_prepare, 1001 .buf_init = buffer_init,
999 .buf_queue = buffer_queue, 1002 .buf_prepare = buffer_prepare,
1000 .buf_release = buffer_release, 1003 .buf_finish = buffer_finish,
1004 .buf_queue = saa7134_vb2_buffer_queue,
1005 .wait_prepare = vb2_ops_wait_prepare,
1006 .wait_finish = vb2_ops_wait_finish,
1007 .start_streaming = saa7134_vb2_start_streaming,
1008 .stop_streaming = saa7134_vb2_stop_streaming,
1001}; 1009};
1002 1010
1003/* ------------------------------------------------------------------ */ 1011/* ------------------------------------------------------------------ */
@@ -1075,7 +1083,7 @@ static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
1075 default: 1083 default:
1076 return -EINVAL; 1084 return -EINVAL;
1077 } 1085 }
1078 if (restart_overlay && res_locked(dev, RESOURCE_OVERLAY)) { 1086 if (restart_overlay && dev->overlay_owner) {
1079 spin_lock_irqsave(&dev->slock, flags); 1087 spin_lock_irqsave(&dev->slock, flags);
1080 stop_preview(dev); 1088 stop_preview(dev);
1081 start_preview(dev); 1089 start_preview(dev);
@@ -1086,55 +1094,21 @@ static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
1086 1094
1087/* ------------------------------------------------------------------ */ 1095/* ------------------------------------------------------------------ */
1088 1096
1089static struct videobuf_queue *saa7134_queue(struct file *file) 1097static inline struct vb2_queue *saa7134_queue(struct file *file)
1090{
1091 struct video_device *vdev = video_devdata(file);
1092 struct saa7134_dev *dev = video_drvdata(file);
1093 struct saa7134_fh *fh = file->private_data;
1094 struct videobuf_queue *q = NULL;
1095
1096 switch (vdev->vfl_type) {
1097 case VFL_TYPE_GRABBER:
1098 q = fh->is_empress ? &dev->empress_vbq : &dev->video_vbq;
1099 break;
1100 case VFL_TYPE_VBI:
1101 q = &dev->vbi_vbq;
1102 break;
1103 default:
1104 BUG();
1105 }
1106 return q;
1107}
1108
1109static int saa7134_resource(struct file *file)
1110{ 1098{
1111 struct video_device *vdev = video_devdata(file); 1099 return video_devdata(file)->queue;
1112 struct saa7134_fh *fh = file->private_data;
1113
1114 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1115 return fh->is_empress ? RESOURCE_EMPRESS : RESOURCE_VIDEO;
1116
1117 if (vdev->vfl_type == VFL_TYPE_VBI)
1118 return RESOURCE_VBI;
1119
1120 BUG();
1121 return 0;
1122} 1100}
1123 1101
1124static int video_open(struct file *file) 1102static int video_open(struct file *file)
1125{ 1103{
1126 struct video_device *vdev = video_devdata(file); 1104 struct video_device *vdev = video_devdata(file);
1127 struct saa7134_dev *dev = video_drvdata(file); 1105 struct saa7134_dev *dev = video_drvdata(file);
1128 struct saa7134_fh *fh; 1106 int ret = v4l2_fh_open(file);
1129 1107
1130 /* allocate + initialize per filehandle data */ 1108 if (ret < 0)
1131 fh = kzalloc(sizeof(*fh),GFP_KERNEL); 1109 return ret;
1132 if (NULL == fh)
1133 return -ENOMEM;
1134
1135 v4l2_fh_init(&fh->fh, vdev);
1136 file->private_data = fh;
1137 1110
1111 mutex_lock(&dev->lock);
1138 if (vdev->vfl_type == VFL_TYPE_RADIO) { 1112 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1139 /* switch to radio mode */ 1113 /* switch to radio mode */
1140 saa7134_tvaudio_setinput(dev, &card(dev).radio); 1114 saa7134_tvaudio_setinput(dev, &card(dev).radio);
@@ -1143,127 +1117,34 @@ static int video_open(struct file *file)
1143 /* switch to video/vbi mode */ 1117 /* switch to video/vbi mode */
1144 video_mux(dev, dev->ctl_input); 1118 video_mux(dev, dev->ctl_input);
1145 } 1119 }
1146 v4l2_fh_add(&fh->fh); 1120 mutex_unlock(&dev->lock);
1147 1121
1148 return 0; 1122 return 0;
1149} 1123}
1150 1124
1151static ssize_t
1152video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1153{
1154 struct video_device *vdev = video_devdata(file);
1155 struct saa7134_dev *dev = video_drvdata(file);
1156 struct saa7134_fh *fh = file->private_data;
1157
1158 switch (vdev->vfl_type) {
1159 case VFL_TYPE_GRABBER:
1160 if (res_locked(dev, RESOURCE_VIDEO))
1161 return -EBUSY;
1162 return videobuf_read_one(saa7134_queue(file),
1163 data, count, ppos,
1164 file->f_flags & O_NONBLOCK);
1165 case VFL_TYPE_VBI:
1166 if (!res_get(dev, fh, RESOURCE_VBI))
1167 return -EBUSY;
1168 return videobuf_read_stream(saa7134_queue(file),
1169 data, count, ppos, 1,
1170 file->f_flags & O_NONBLOCK);
1171 break;
1172 default:
1173 BUG();
1174 return 0;
1175 }
1176}
1177
1178static unsigned int
1179video_poll(struct file *file, struct poll_table_struct *wait)
1180{
1181 unsigned long req_events = poll_requested_events(wait);
1182 struct video_device *vdev = video_devdata(file);
1183 struct saa7134_dev *dev = video_drvdata(file);
1184 struct saa7134_fh *fh = file->private_data;
1185 struct videobuf_buffer *buf = NULL;
1186 struct videobuf_queue *q = &dev->video_vbq;
1187 unsigned int rc = 0;
1188
1189 if (v4l2_event_pending(&fh->fh))
1190 rc = POLLPRI;
1191 else if (req_events & POLLPRI)
1192 poll_wait(file, &fh->fh.wait, wait);
1193
1194 if (vdev->vfl_type == VFL_TYPE_VBI)
1195 return rc | videobuf_poll_stream(file, &dev->vbi_vbq, wait);
1196
1197 if (res_check(fh, RESOURCE_VIDEO)) {
1198 mutex_lock(&q->vb_lock);
1199 if (!list_empty(&q->stream))
1200 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
1201 } else {
1202 mutex_lock(&q->vb_lock);
1203 if (UNSET == q->read_off) {
1204 /* need to capture a new frame */
1205 if (res_locked(dev, RESOURCE_VIDEO))
1206 goto err;
1207 if (0 != q->ops->buf_prepare(q, q->read_buf, q->field))
1208 goto err;
1209 q->ops->buf_queue(q, q->read_buf);
1210 q->read_off = 0;
1211 }
1212 buf = q->read_buf;
1213 }
1214
1215 if (!buf)
1216 goto err;
1217
1218 poll_wait(file, &buf->done, wait);
1219 if (buf->state == VIDEOBUF_DONE || buf->state == VIDEOBUF_ERROR)
1220 rc |= POLLIN | POLLRDNORM;
1221 mutex_unlock(&q->vb_lock);
1222 return rc;
1223
1224err:
1225 mutex_unlock(&q->vb_lock);
1226 return rc | POLLERR;
1227}
1228
1229static int video_release(struct file *file) 1125static int video_release(struct file *file)
1230{ 1126{
1231 struct video_device *vdev = video_devdata(file); 1127 struct video_device *vdev = video_devdata(file);
1232 struct saa7134_dev *dev = video_drvdata(file); 1128 struct saa7134_dev *dev = video_drvdata(file);
1233 struct saa7134_fh *fh = file->private_data; 1129 struct v4l2_fh *fh = file->private_data;
1234 struct saa6588_command cmd; 1130 struct saa6588_command cmd;
1235 unsigned long flags; 1131 unsigned long flags;
1236 1132
1133 mutex_lock(&dev->lock);
1237 saa7134_tvaudio_close(dev); 1134 saa7134_tvaudio_close(dev);
1238 1135
1239 /* turn off overlay */ 1136 /* turn off overlay */
1240 if (res_check(fh, RESOURCE_OVERLAY)) { 1137 if (fh == dev->overlay_owner) {
1241 spin_lock_irqsave(&dev->slock,flags); 1138 spin_lock_irqsave(&dev->slock,flags);
1242 stop_preview(dev); 1139 stop_preview(dev);
1243 spin_unlock_irqrestore(&dev->slock,flags); 1140 spin_unlock_irqrestore(&dev->slock,flags);
1244 res_free(dev, fh, RESOURCE_OVERLAY); 1141 dev->overlay_owner = NULL;
1245 }
1246
1247 /* stop video capture */
1248 if (res_check(fh, RESOURCE_VIDEO)) {
1249 pm_qos_remove_request(&dev->qos_request);
1250 videobuf_streamoff(&dev->video_vbq);
1251 res_free(dev, fh, RESOURCE_VIDEO);
1252 videobuf_mmap_free(&dev->video_vbq);
1253 INIT_LIST_HEAD(&dev->video_vbq.stream);
1254 }
1255 if (dev->video_vbq.read_buf) {
1256 buffer_release(&dev->video_vbq, dev->video_vbq.read_buf);
1257 kfree(dev->video_vbq.read_buf);
1258 } 1142 }
1259 1143
1260 /* stop vbi capture */ 1144 if (vdev->vfl_type == VFL_TYPE_RADIO)
1261 if (res_check(fh, RESOURCE_VBI)) { 1145 v4l2_fh_release(file);
1262 videobuf_stop(&dev->vbi_vbq); 1146 else
1263 res_free(dev, fh, RESOURCE_VBI); 1147 _vb2_fop_release(file, NULL);
1264 videobuf_mmap_free(&dev->vbi_vbq);
1265 INIT_LIST_HEAD(&dev->vbi_vbq.stream);
1266 }
1267 1148
1268 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/ 1149 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1269 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0); 1150 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
@@ -1274,19 +1155,11 @@ static int video_release(struct file *file)
1274 saa_call_all(dev, core, s_power, 0); 1155 saa_call_all(dev, core, s_power, 0);
1275 if (vdev->vfl_type == VFL_TYPE_RADIO) 1156 if (vdev->vfl_type == VFL_TYPE_RADIO)
1276 saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd); 1157 saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
1158 mutex_unlock(&dev->lock);
1277 1159
1278 v4l2_fh_del(&fh->fh);
1279 v4l2_fh_exit(&fh->fh);
1280 file->private_data = NULL;
1281 kfree(fh);
1282 return 0; 1160 return 0;
1283} 1161}
1284 1162
1285static int video_mmap(struct file *file, struct vm_area_struct * vma)
1286{
1287 return videobuf_mmap_mapper(saa7134_queue(file), vma);
1288}
1289
1290static ssize_t radio_read(struct file *file, char __user *data, 1163static ssize_t radio_read(struct file *file, char __user *data,
1291 size_t count, loff_t *ppos) 1164 size_t count, loff_t *ppos)
1292{ 1165{
@@ -1299,7 +1172,9 @@ static ssize_t radio_read(struct file *file, char __user *data,
1299 cmd.instance = file; 1172 cmd.instance = file;
1300 cmd.result = -ENODEV; 1173 cmd.result = -ENODEV;
1301 1174
1175 mutex_lock(&dev->lock);
1302 saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd); 1176 saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd);
1177 mutex_unlock(&dev->lock);
1303 1178
1304 return cmd.result; 1179 return cmd.result;
1305} 1180}
@@ -1313,7 +1188,9 @@ static unsigned int radio_poll(struct file *file, poll_table *wait)
1313 cmd.instance = file; 1188 cmd.instance = file;
1314 cmd.event_list = wait; 1189 cmd.event_list = wait;
1315 cmd.result = 0; 1190 cmd.result = 0;
1191 mutex_lock(&dev->lock);
1316 saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd); 1192 saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd);
1193 mutex_unlock(&dev->lock);
1317 1194
1318 return rc | cmd.result; 1195 return rc | cmd.result;
1319} 1196}
@@ -1347,7 +1224,7 @@ static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
1347 1224
1348 f->fmt.pix.width = dev->width; 1225 f->fmt.pix.width = dev->width;
1349 f->fmt.pix.height = dev->height; 1226 f->fmt.pix.height = dev->height;
1350 f->fmt.pix.field = dev->video_vbq.field; 1227 f->fmt.pix.field = dev->field;
1351 f->fmt.pix.pixelformat = dev->fmt->fourcc; 1228 f->fmt.pix.pixelformat = dev->fmt->fourcc;
1352 f->fmt.pix.bytesperline = 1229 f->fmt.pix.bytesperline =
1353 (f->fmt.pix.width * dev->fmt->depth) >> 3; 1230 (f->fmt.pix.width * dev->fmt->depth) >> 3;
@@ -1371,7 +1248,6 @@ static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
1371 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n"); 1248 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1372 return -EINVAL; 1249 return -EINVAL;
1373 } 1250 }
1374 mutex_lock(&dev->lock);
1375 f->fmt.win = dev->win; 1251 f->fmt.win = dev->win;
1376 f->fmt.win.clips = clips; 1252 f->fmt.win.clips = clips;
1377 if (clips == NULL) 1253 if (clips == NULL)
@@ -1385,7 +1261,6 @@ static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
1385 sizeof(struct v4l2_rect))) 1261 sizeof(struct v4l2_rect)))
1386 err = -EFAULT; 1262 err = -EFAULT;
1387 } 1263 }
1388 mutex_unlock(&dev->lock);
1389 1264
1390 return err; 1265 return err;
1391} 1266}
@@ -1469,7 +1344,7 @@ static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
1469 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat); 1344 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1470 dev->width = f->fmt.pix.width; 1345 dev->width = f->fmt.pix.width;
1471 dev->height = f->fmt.pix.height; 1346 dev->height = f->fmt.pix.height;
1472 dev->video_vbq.field = f->fmt.pix.field; 1347 dev->field = f->fmt.pix.field;
1473 return 0; 1348 return 0;
1474} 1349}
1475 1350
@@ -1490,25 +1365,20 @@ static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
1490 if (0 != err) 1365 if (0 != err)
1491 return err; 1366 return err;
1492 1367
1493 mutex_lock(&dev->lock);
1494
1495 dev->win = f->fmt.win; 1368 dev->win = f->fmt.win;
1496 dev->nclips = f->fmt.win.clipcount; 1369 dev->nclips = f->fmt.win.clipcount;
1497 1370
1498 if (copy_from_user(dev->clips, f->fmt.win.clips, 1371 if (copy_from_user(dev->clips, f->fmt.win.clips,
1499 sizeof(struct v4l2_clip) * dev->nclips)) { 1372 sizeof(struct v4l2_clip) * dev->nclips))
1500 mutex_unlock(&dev->lock);
1501 return -EFAULT; 1373 return -EFAULT;
1502 }
1503 1374
1504 if (res_check(priv, RESOURCE_OVERLAY)) { 1375 if (priv == dev->overlay_owner) {
1505 spin_lock_irqsave(&dev->slock, flags); 1376 spin_lock_irqsave(&dev->slock, flags);
1506 stop_preview(dev); 1377 stop_preview(dev);
1507 start_preview(dev); 1378 start_preview(dev);
1508 spin_unlock_irqrestore(&dev->slock, flags); 1379 spin_unlock_irqrestore(&dev->slock, flags);
1509 } 1380 }
1510 1381
1511 mutex_unlock(&dev->lock);
1512 return 0; 1382 return 0;
1513} 1383}
1514 1384
@@ -1560,9 +1430,7 @@ int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1560 return -EINVAL; 1430 return -EINVAL;
1561 if (NULL == card_in(dev, i).name) 1431 if (NULL == card_in(dev, i).name)
1562 return -EINVAL; 1432 return -EINVAL;
1563 mutex_lock(&dev->lock);
1564 video_mux(dev, i); 1433 video_mux(dev, i);
1565 mutex_unlock(&dev->lock);
1566 return 0; 1434 return 0;
1567} 1435}
1568EXPORT_SYMBOL_GPL(saa7134_s_input); 1436EXPORT_SYMBOL_GPL(saa7134_s_input);
@@ -1572,7 +1440,6 @@ int saa7134_querycap(struct file *file, void *priv,
1572{ 1440{
1573 struct saa7134_dev *dev = video_drvdata(file); 1441 struct saa7134_dev *dev = video_drvdata(file);
1574 struct video_device *vdev = video_devdata(file); 1442 struct video_device *vdev = video_devdata(file);
1575 struct saa7134_fh *fh = priv;
1576 u32 radio_caps, video_caps, vbi_caps; 1443 u32 radio_caps, video_caps, vbi_caps;
1577 1444
1578 unsigned int tuner_type = dev->tuner_type; 1445 unsigned int tuner_type = dev->tuner_type;
@@ -1591,7 +1458,7 @@ int saa7134_querycap(struct file *file, void *priv,
1591 radio_caps |= V4L2_CAP_RDS_CAPTURE; 1458 radio_caps |= V4L2_CAP_RDS_CAPTURE;
1592 1459
1593 video_caps = V4L2_CAP_VIDEO_CAPTURE; 1460 video_caps = V4L2_CAP_VIDEO_CAPTURE;
1594 if (saa7134_no_overlay <= 0 && !fh->is_empress) 1461 if (saa7134_no_overlay <= 0 && !is_empress(file))
1595 video_caps |= V4L2_CAP_VIDEO_OVERLAY; 1462 video_caps |= V4L2_CAP_VIDEO_OVERLAY;
1596 1463
1597 vbi_caps = V4L2_CAP_VBI_CAPTURE; 1464 vbi_caps = V4L2_CAP_VBI_CAPTURE;
@@ -1622,12 +1489,12 @@ EXPORT_SYMBOL_GPL(saa7134_querycap);
1622int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id) 1489int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
1623{ 1490{
1624 struct saa7134_dev *dev = video_drvdata(file); 1491 struct saa7134_dev *dev = video_drvdata(file);
1625 struct saa7134_fh *fh = priv; 1492 struct v4l2_fh *fh = priv;
1626 unsigned long flags; 1493 unsigned long flags;
1627 unsigned int i; 1494 unsigned int i;
1628 v4l2_std_id fixup; 1495 v4l2_std_id fixup;
1629 1496
1630 if (fh->is_empress && res_locked(dev, RESOURCE_OVERLAY)) { 1497 if (is_empress(file) && dev->overlay_owner) {
1631 /* Don't change the std from the mpeg device 1498 /* Don't change the std from the mpeg device
1632 if overlay is active. */ 1499 if overlay is active. */
1633 return -EBUSY; 1500 return -EBUSY;
@@ -1666,8 +1533,7 @@ int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
1666 1533
1667 id = tvnorms[i].id; 1534 id = tvnorms[i].id;
1668 1535
1669 mutex_lock(&dev->lock); 1536 if (!is_empress(file) && fh == dev->overlay_owner) {
1670 if (!fh->is_empress && res_check(fh, RESOURCE_OVERLAY)) {
1671 spin_lock_irqsave(&dev->slock, flags); 1537 spin_lock_irqsave(&dev->slock, flags);
1672 stop_preview(dev); 1538 stop_preview(dev);
1673 spin_unlock_irqrestore(&dev->slock, flags); 1539 spin_unlock_irqrestore(&dev->slock, flags);
@@ -1681,7 +1547,6 @@ int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
1681 set_tvnorm(dev, &tvnorms[i]); 1547 set_tvnorm(dev, &tvnorms[i]);
1682 1548
1683 saa7134_tvaudio_do_scan(dev); 1549 saa7134_tvaudio_do_scan(dev);
1684 mutex_unlock(&dev->lock);
1685 return 0; 1550 return 0;
1686} 1551}
1687EXPORT_SYMBOL_GPL(saa7134_s_std); 1552EXPORT_SYMBOL_GPL(saa7134_s_std);
@@ -1768,9 +1633,9 @@ static int saa7134_s_crop(struct file *file, void *f, const struct v4l2_crop *cr
1768 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY) 1633 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1769 return -EINVAL; 1634 return -EINVAL;
1770 1635
1771 if (res_locked(dev, RESOURCE_OVERLAY)) 1636 if (dev->overlay_owner)
1772 return -EBUSY; 1637 return -EBUSY;
1773 if (res_locked(dev, RESOURCE_VIDEO)) 1638 if (vb2_is_streaming(&dev->video_vbq))
1774 return -EBUSY; 1639 return -EBUSY;
1775 1640
1776 *c = crop->c; 1641 *c = crop->c;
@@ -1864,12 +1729,10 @@ int saa7134_s_frequency(struct file *file, void *priv,
1864 1729
1865 if (0 != f->tuner) 1730 if (0 != f->tuner)
1866 return -EINVAL; 1731 return -EINVAL;
1867 mutex_lock(&dev->lock);
1868 1732
1869 saa_call_all(dev, tuner, s_frequency, f); 1733 saa_call_all(dev, tuner, s_frequency, f);
1870 1734
1871 saa7134_tvaudio_do_scan(dev); 1735 saa7134_tvaudio_do_scan(dev);
1872 mutex_unlock(&dev->lock);
1873 return 0; 1736 return 0;
1874} 1737}
1875EXPORT_SYMBOL_GPL(saa7134_s_frequency); 1738EXPORT_SYMBOL_GPL(saa7134_s_frequency);
@@ -1953,87 +1816,24 @@ static int saa7134_overlay(struct file *file, void *priv, unsigned int on)
1953 return -EINVAL; 1816 return -EINVAL;
1954 } 1817 }
1955 1818
1956 if (!res_get(dev, priv, RESOURCE_OVERLAY)) 1819 if (dev->overlay_owner && priv != dev->overlay_owner)
1957 return -EBUSY; 1820 return -EBUSY;
1821 dev->overlay_owner = priv;
1958 spin_lock_irqsave(&dev->slock, flags); 1822 spin_lock_irqsave(&dev->slock, flags);
1959 start_preview(dev); 1823 start_preview(dev);
1960 spin_unlock_irqrestore(&dev->slock, flags); 1824 spin_unlock_irqrestore(&dev->slock, flags);
1961 } 1825 }
1962 if (!on) { 1826 if (!on) {
1963 if (!res_check(priv, RESOURCE_OVERLAY)) 1827 if (priv != dev->overlay_owner)
1964 return -EINVAL; 1828 return -EINVAL;
1965 spin_lock_irqsave(&dev->slock, flags); 1829 spin_lock_irqsave(&dev->slock, flags);
1966 stop_preview(dev); 1830 stop_preview(dev);
1967 spin_unlock_irqrestore(&dev->slock, flags); 1831 spin_unlock_irqrestore(&dev->slock, flags);
1968 res_free(dev, priv, RESOURCE_OVERLAY); 1832 dev->overlay_owner = NULL;
1969 } 1833 }
1970 return 0; 1834 return 0;
1971} 1835}
1972 1836
1973int saa7134_reqbufs(struct file *file, void *priv,
1974 struct v4l2_requestbuffers *p)
1975{
1976 return videobuf_reqbufs(saa7134_queue(file), p);
1977}
1978EXPORT_SYMBOL_GPL(saa7134_reqbufs);
1979
1980int saa7134_querybuf(struct file *file, void *priv,
1981 struct v4l2_buffer *b)
1982{
1983 return videobuf_querybuf(saa7134_queue(file), b);
1984}
1985EXPORT_SYMBOL_GPL(saa7134_querybuf);
1986
1987int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1988{
1989 return videobuf_qbuf(saa7134_queue(file), b);
1990}
1991EXPORT_SYMBOL_GPL(saa7134_qbuf);
1992
1993int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1994{
1995 return videobuf_dqbuf(saa7134_queue(file), b,
1996 file->f_flags & O_NONBLOCK);
1997}
1998EXPORT_SYMBOL_GPL(saa7134_dqbuf);
1999
2000int saa7134_streamon(struct file *file, void *priv,
2001 enum v4l2_buf_type type)
2002{
2003 struct saa7134_dev *dev = video_drvdata(file);
2004 int res = saa7134_resource(file);
2005
2006 if (!res_get(dev, priv, res))
2007 return -EBUSY;
2008
2009 /* The SAA7134 has a 1K FIFO; the datasheet suggests that when
2010 * configured conservatively, there's 22 usec of buffering for video.
2011 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
2012 * margin in case the FIFO is configured differently to the datasheet.
2013 * Unfortunately, I lack register-level documentation to check the
2014 * Linux FIFO setup and confirm the perfect value.
2015 */
2016 if (res != RESOURCE_EMPRESS)
2017 pm_qos_add_request(&dev->qos_request,
2018 PM_QOS_CPU_DMA_LATENCY, 20);
2019
2020 return videobuf_streamon(saa7134_queue(file));
2021}
2022EXPORT_SYMBOL_GPL(saa7134_streamon);
2023
2024int saa7134_streamoff(struct file *file, void *priv,
2025 enum v4l2_buf_type type)
2026{
2027 struct saa7134_dev *dev = video_drvdata(file);
2028 int res = saa7134_resource(file);
2029
2030 if (res != RESOURCE_EMPRESS)
2031 pm_qos_remove_request(&dev->qos_request);
2032
2033 return videobuf_streamoff(saa7134_queue(file));
2034}
2035EXPORT_SYMBOL_GPL(saa7134_streamoff);
2036
2037#ifdef CONFIG_VIDEO_ADV_DEBUG 1837#ifdef CONFIG_VIDEO_ADV_DEBUG
2038static int vidioc_g_register (struct file *file, void *priv, 1838static int vidioc_g_register (struct file *file, void *priv,
2039 struct v4l2_dbg_register *reg) 1839 struct v4l2_dbg_register *reg)
@@ -2091,10 +1891,10 @@ static const struct v4l2_file_operations video_fops =
2091 .owner = THIS_MODULE, 1891 .owner = THIS_MODULE,
2092 .open = video_open, 1892 .open = video_open,
2093 .release = video_release, 1893 .release = video_release,
2094 .read = video_read, 1894 .read = vb2_fop_read,
2095 .poll = video_poll, 1895 .poll = vb2_fop_poll,
2096 .mmap = video_mmap, 1896 .mmap = vb2_fop_mmap,
2097 .ioctl = video_ioctl2, 1897 .unlocked_ioctl = video_ioctl2,
2098}; 1898};
2099 1899
2100static const struct v4l2_ioctl_ops video_ioctl_ops = { 1900static const struct v4l2_ioctl_ops video_ioctl_ops = {
@@ -2111,18 +1911,18 @@ static const struct v4l2_ioctl_ops video_ioctl_ops = {
2111 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap, 1911 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2112 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap, 1912 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2113 .vidioc_cropcap = saa7134_cropcap, 1913 .vidioc_cropcap = saa7134_cropcap,
2114 .vidioc_reqbufs = saa7134_reqbufs, 1914 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2115 .vidioc_querybuf = saa7134_querybuf, 1915 .vidioc_querybuf = vb2_ioctl_querybuf,
2116 .vidioc_qbuf = saa7134_qbuf, 1916 .vidioc_qbuf = vb2_ioctl_qbuf,
2117 .vidioc_dqbuf = saa7134_dqbuf, 1917 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2118 .vidioc_s_std = saa7134_s_std, 1918 .vidioc_s_std = saa7134_s_std,
2119 .vidioc_g_std = saa7134_g_std, 1919 .vidioc_g_std = saa7134_g_std,
2120 .vidioc_querystd = saa7134_querystd, 1920 .vidioc_querystd = saa7134_querystd,
2121 .vidioc_enum_input = saa7134_enum_input, 1921 .vidioc_enum_input = saa7134_enum_input,
2122 .vidioc_g_input = saa7134_g_input, 1922 .vidioc_g_input = saa7134_g_input,
2123 .vidioc_s_input = saa7134_s_input, 1923 .vidioc_s_input = saa7134_s_input,
2124 .vidioc_streamon = saa7134_streamon, 1924 .vidioc_streamon = vb2_ioctl_streamon,
2125 .vidioc_streamoff = saa7134_streamoff, 1925 .vidioc_streamoff = vb2_ioctl_streamoff,
2126 .vidioc_g_tuner = saa7134_g_tuner, 1926 .vidioc_g_tuner = saa7134_g_tuner,
2127 .vidioc_s_tuner = saa7134_s_tuner, 1927 .vidioc_s_tuner = saa7134_s_tuner,
2128 .vidioc_g_crop = saa7134_g_crop, 1928 .vidioc_g_crop = saa7134_g_crop,
@@ -2146,7 +1946,7 @@ static const struct v4l2_file_operations radio_fops = {
2146 .open = video_open, 1946 .open = video_open,
2147 .read = radio_read, 1947 .read = radio_read,
2148 .release = video_release, 1948 .release = video_release,
2149 .ioctl = video_ioctl2, 1949 .unlocked_ioctl = video_ioctl2,
2150 .poll = radio_poll, 1950 .poll = radio_poll,
2151}; 1951};
2152 1952
@@ -2224,6 +2024,8 @@ static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
2224int saa7134_video_init1(struct saa7134_dev *dev) 2024int saa7134_video_init1(struct saa7134_dev *dev)
2225{ 2025{
2226 struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler; 2026 struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
2027 struct vb2_queue *q;
2028 int ret;
2227 2029
2228 /* sanitycheck insmod options */ 2030 /* sanitycheck insmod options */
2229 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME) 2031 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
@@ -2275,6 +2077,7 @@ int saa7134_video_init1(struct saa7134_dev *dev)
2275 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24); 2077 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
2276 dev->width = 720; 2078 dev->width = 720;
2277 dev->height = 576; 2079 dev->height = 576;
2080 dev->field = V4L2_FIELD_INTERLACED;
2278 dev->win.w.width = dev->width; 2081 dev->win.w.width = dev->width;
2279 dev->win.w.height = dev->height; 2082 dev->win.w.height = dev->height;
2280 dev->win.field = V4L2_FIELD_INTERLACED; 2083 dev->win.field = V4L2_FIELD_INTERLACED;
@@ -2286,19 +2089,40 @@ int saa7134_video_init1(struct saa7134_dev *dev)
2286 if (saa7134_boards[dev->board].video_out) 2089 if (saa7134_boards[dev->board].video_out)
2287 saa7134_videoport_init(dev); 2090 saa7134_videoport_init(dev);
2288 2091
2289 videobuf_queue_sg_init(&dev->video_vbq, &video_qops, 2092 q = &dev->video_vbq;
2290 &dev->pci->dev, &dev->slock, 2093 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2291 V4L2_BUF_TYPE_VIDEO_CAPTURE, 2094 /*
2292 V4L2_FIELD_INTERLACED, 2095 * Do not add VB2_USERPTR: the saa7134 DMA engine cannot handle
2293 sizeof(struct saa7134_buf), 2096 * transfers that do not start at the beginning of a page. A USERPTR
2294 &dev->video_q, NULL); 2097 * can start anywhere in a page, so USERPTR support is a no-go.
2295 videobuf_queue_sg_init(&dev->vbi_vbq, &saa7134_vbi_qops, 2098 */
2296 &dev->pci->dev, &dev->slock, 2099 q->io_modes = VB2_MMAP | VB2_READ;
2297 V4L2_BUF_TYPE_VBI_CAPTURE, 2100 q->drv_priv = &dev->video_q;
2298 V4L2_FIELD_SEQ_TB, 2101 q->ops = &vb2_qops;
2299 sizeof(struct saa7134_buf), 2102 q->gfp_flags = GFP_DMA32;
2300 &dev->vbi_q, NULL); 2103 q->mem_ops = &vb2_dma_sg_memops;
2104 q->buf_struct_size = sizeof(struct saa7134_buf);
2105 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2106 q->lock = &dev->lock;
2107 ret = vb2_queue_init(q);
2108 if (ret)
2109 return ret;
2301 saa7134_pgtable_alloc(dev->pci, &dev->video_q.pt); 2110 saa7134_pgtable_alloc(dev->pci, &dev->video_q.pt);
2111
2112 q = &dev->vbi_vbq;
2113 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
2114 /* Don't add VB2_USERPTR, see comment above */
2115 q->io_modes = VB2_MMAP | VB2_READ;
2116 q->drv_priv = &dev->vbi_q;
2117 q->ops = &saa7134_vbi_qops;
2118 q->gfp_flags = GFP_DMA32;
2119 q->mem_ops = &vb2_dma_sg_memops;
2120 q->buf_struct_size = sizeof(struct saa7134_buf);
2121 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2122 q->lock = &dev->lock;
2123 ret = vb2_queue_init(q);
2124 if (ret)
2125 return ret;
2302 saa7134_pgtable_alloc(dev->pci, &dev->vbi_q.pt); 2126 saa7134_pgtable_alloc(dev->pci, &dev->vbi_q.pt);
2303 2127
2304 return 0; 2128 return 0;
@@ -2307,7 +2131,9 @@ int saa7134_video_init1(struct saa7134_dev *dev)
2307void saa7134_video_fini(struct saa7134_dev *dev) 2131void saa7134_video_fini(struct saa7134_dev *dev)
2308{ 2132{
2309 /* free stuff */ 2133 /* free stuff */
2134 vb2_queue_release(&dev->video_vbq);
2310 saa7134_pgtable_free(dev->pci, &dev->video_q.pt); 2135 saa7134_pgtable_free(dev->pci, &dev->video_q.pt);
2136 vb2_queue_release(&dev->vbi_vbq);
2311 saa7134_pgtable_free(dev->pci, &dev->vbi_q.pt); 2137 saa7134_pgtable_free(dev->pci, &dev->vbi_q.pt);
2312 v4l2_ctrl_handler_free(&dev->ctrl_handler); 2138 v4l2_ctrl_handler_free(&dev->ctrl_handler);
2313 if (card_has_radio(dev)) 2139 if (card_has_radio(dev))
@@ -2401,8 +2227,7 @@ void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2401 2227
2402 spin_lock(&dev->slock); 2228 spin_lock(&dev->slock);
2403 if (dev->video_q.curr) { 2229 if (dev->video_q.curr) {
2404 dev->video_fieldcount++; 2230 field = dev->field;
2405 field = dev->video_q.curr->vb.field;
2406 if (V4L2_FIELD_HAS_BOTH(field)) { 2231 if (V4L2_FIELD_HAS_BOTH(field)) {
2407 /* make sure we have seen both fields */ 2232 /* make sure we have seen both fields */
2408 if ((status & 0x10) == 0x00) { 2233 if ((status & 0x10) == 0x00) {
@@ -2418,8 +2243,7 @@ void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2418 if ((status & 0x10) != 0x00) 2243 if ((status & 0x10) != 0x00)
2419 goto done; 2244 goto done;
2420 } 2245 }
2421 dev->video_q.curr->vb.field_count = dev->video_fieldcount; 2246 saa7134_buffer_finish(dev, &dev->video_q, VB2_BUF_STATE_DONE);
2422 saa7134_buffer_finish(dev, &dev->video_q, VIDEOBUF_DONE);
2423 } 2247 }
2424 saa7134_buffer_next(dev, &dev->video_q); 2248 saa7134_buffer_next(dev, &dev->video_q);
2425 2249
diff --git a/drivers/media/pci/saa7134/saa7134.h b/drivers/media/pci/saa7134/saa7134.h
index d8c612aa65e0..5f8307f7cc9a 100644
--- a/drivers/media/pci/saa7134/saa7134.h
+++ b/drivers/media/pci/saa7134/saa7134.h
@@ -41,11 +41,11 @@
41#include <media/tuner.h> 41#include <media/tuner.h>
42#include <media/rc-core.h> 42#include <media/rc-core.h>
43#include <media/ir-kbd-i2c.h> 43#include <media/ir-kbd-i2c.h>
44#include <media/videobuf-dma-sg.h> 44#include <media/videobuf2-dma-sg.h>
45#include <sound/core.h> 45#include <sound/core.h>
46#include <sound/pcm.h> 46#include <sound/pcm.h>
47#if IS_ENABLED(CONFIG_VIDEO_SAA7134_DVB) 47#if IS_ENABLED(CONFIG_VIDEO_SAA7134_DVB)
48#include <media/videobuf-dvb.h> 48#include <media/videobuf2-dvb.h>
49#endif 49#endif
50#include "tda8290.h" 50#include "tda8290.h"
51 51
@@ -453,13 +453,15 @@ struct saa7134_thread {
453/* buffer for one video/vbi/ts frame */ 453/* buffer for one video/vbi/ts frame */
454struct saa7134_buf { 454struct saa7134_buf {
455 /* common v4l buffer stuff -- must be first */ 455 /* common v4l buffer stuff -- must be first */
456 struct videobuf_buffer vb; 456 struct vb2_buffer vb2;
457 457
458 /* saa7134 specific */ 458 /* saa7134 specific */
459 unsigned int top_seen; 459 unsigned int top_seen;
460 int (*activate)(struct saa7134_dev *dev, 460 int (*activate)(struct saa7134_dev *dev,
461 struct saa7134_buf *buf, 461 struct saa7134_buf *buf,
462 struct saa7134_buf *next); 462 struct saa7134_buf *next);
463
464 struct list_head entry;
463}; 465};
464 466
465struct saa7134_dmaqueue { 467struct saa7134_dmaqueue {
@@ -468,16 +470,10 @@ struct saa7134_dmaqueue {
468 struct list_head queue; 470 struct list_head queue;
469 struct timer_list timeout; 471 struct timer_list timeout;
470 unsigned int need_two; 472 unsigned int need_two;
473 unsigned int seq_nr;
471 struct saa7134_pgtable pt; 474 struct saa7134_pgtable pt;
472}; 475};
473 476
474/* video filehandle status */
475struct saa7134_fh {
476 struct v4l2_fh fh;
477 bool is_empress;
478 unsigned int resources;
479};
480
481/* dmasound dsp status */ 477/* dmasound dsp status */
482struct saa7134_dmasound { 478struct saa7134_dmasound {
483 struct mutex lock; 479 struct mutex lock;
@@ -583,20 +579,35 @@ struct saa7134_dev {
583 struct v4l2_window win; 579 struct v4l2_window win;
584 struct v4l2_clip clips[8]; 580 struct v4l2_clip clips[8];
585 unsigned int nclips; 581 unsigned int nclips;
582 struct v4l2_fh *overlay_owner;
586 583
587 584
588 /* video+ts+vbi capture */ 585 /* video+ts+vbi capture */
589 struct saa7134_dmaqueue video_q; 586 struct saa7134_dmaqueue video_q;
590 struct videobuf_queue video_vbq; 587 struct vb2_queue video_vbq;
591 struct saa7134_dmaqueue vbi_q; 588 struct saa7134_dmaqueue vbi_q;
592 struct videobuf_queue vbi_vbq; 589 struct vb2_queue vbi_vbq;
593 unsigned int video_fieldcount; 590 enum v4l2_field field;
594 unsigned int vbi_fieldcount;
595 struct saa7134_format *fmt; 591 struct saa7134_format *fmt;
596 unsigned int width, height; 592 unsigned int width, height;
597 unsigned int vbi_hlen, vbi_vlen; 593 unsigned int vbi_hlen, vbi_vlen;
598 struct pm_qos_request qos_request; 594 struct pm_qos_request qos_request;
599 595
596 /* SAA7134_MPEG_* */
597 struct saa7134_ts ts;
598 struct saa7134_dmaqueue ts_q;
599 enum v4l2_field ts_field;
600 int ts_started;
601 struct saa7134_mpeg_ops *mops;
602
603 /* SAA7134_MPEG_EMPRESS only */
604 struct video_device *empress_dev;
605 struct v4l2_subdev *empress_sd;
606 struct vb2_queue empress_vbq;
607 struct work_struct empress_workqueue;
608 int empress_started;
609 struct v4l2_ctrl_handler empress_ctrl_handler;
610
600 /* various v4l controls */ 611 /* various v4l controls */
601 struct saa7134_tvnorm *tvnorm; /* video */ 612 struct saa7134_tvnorm *tvnorm; /* video */
602 struct saa7134_tvaudio *tvaudio; 613 struct saa7134_tvaudio *tvaudio;
@@ -633,23 +644,9 @@ struct saa7134_dev {
633 /* I2C keyboard data */ 644 /* I2C keyboard data */
634 struct IR_i2c_init_data init_data; 645 struct IR_i2c_init_data init_data;
635 646
636 /* SAA7134_MPEG_* */
637 struct saa7134_ts ts;
638 struct saa7134_dmaqueue ts_q;
639 int ts_started;
640 struct saa7134_mpeg_ops *mops;
641
642 /* SAA7134_MPEG_EMPRESS only */
643 struct video_device *empress_dev;
644 struct v4l2_subdev *empress_sd;
645 struct videobuf_queue empress_vbq;
646 struct work_struct empress_workqueue;
647 int empress_started;
648 struct v4l2_ctrl_handler empress_ctrl_handler;
649
650#if IS_ENABLED(CONFIG_VIDEO_SAA7134_DVB) 647#if IS_ENABLED(CONFIG_VIDEO_SAA7134_DVB)
651 /* SAA7134_MPEG_DVB only */ 648 /* SAA7134_MPEG_DVB only */
652 struct videobuf_dvb_frontends frontends; 649 struct vb2_dvb_frontends frontends;
653 int (*original_demod_sleep)(struct dvb_frontend *fe); 650 int (*original_demod_sleep)(struct dvb_frontend *fe);
654 int (*original_set_voltage)(struct dvb_frontend *fe, fe_sec_voltage_t voltage); 651 int (*original_set_voltage)(struct dvb_frontend *fe, fe_sec_voltage_t voltage);
655 int (*original_set_high_voltage)(struct dvb_frontend *fe, long arg); 652 int (*original_set_high_voltage)(struct dvb_frontend *fe, long arg);
@@ -703,14 +700,12 @@ struct saa7134_dev {
703 _rc; \ 700 _rc; \
704}) 701})
705 702
706static inline int res_check(struct saa7134_fh *fh, unsigned int bit) 703static inline bool is_empress(struct file *file)
707{ 704{
708 return fh->resources & bit; 705 struct video_device *vdev = video_devdata(file);
709} 706 struct saa7134_dev *dev = video_get_drvdata(vdev);
710 707
711static inline int res_locked(struct saa7134_dev *dev, unsigned int bit) 708 return vdev->queue == &dev->empress_vbq;
712{
713 return dev->resources & bit;
714} 709}
715 710
716/* ----------------------------------------------------------- */ 711/* ----------------------------------------------------------- */
@@ -741,7 +736,7 @@ void saa7134_buffer_finish(struct saa7134_dev *dev, struct saa7134_dmaqueue *q,
741 unsigned int state); 736 unsigned int state);
742void saa7134_buffer_next(struct saa7134_dev *dev, struct saa7134_dmaqueue *q); 737void saa7134_buffer_next(struct saa7134_dev *dev, struct saa7134_dmaqueue *q);
743void saa7134_buffer_timeout(unsigned long data); 738void saa7134_buffer_timeout(unsigned long data);
744void saa7134_dma_free(struct videobuf_queue *q,struct saa7134_buf *buf); 739void saa7134_stop_streaming(struct saa7134_dev *dev, struct saa7134_dmaqueue *q);
745 740
746int saa7134_set_dmabits(struct saa7134_dev *dev); 741int saa7134_set_dmabits(struct saa7134_dev *dev);
747 742
@@ -775,6 +770,10 @@ extern unsigned int video_debug;
775extern struct video_device saa7134_video_template; 770extern struct video_device saa7134_video_template;
776extern struct video_device saa7134_radio_template; 771extern struct video_device saa7134_radio_template;
777 772
773void saa7134_vb2_buffer_queue(struct vb2_buffer *vb);
774int saa7134_vb2_start_streaming(struct vb2_queue *vq, unsigned int count);
775void saa7134_vb2_stop_streaming(struct vb2_queue *vq);
776
778int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id); 777int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id);
779int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id); 778int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id);
780int saa7134_querystd(struct file *file, void *priv, v4l2_std_id *std); 779int saa7134_querystd(struct file *file, void *priv, v4l2_std_id *std);
@@ -791,16 +790,6 @@ int saa7134_g_frequency(struct file *file, void *priv,
791 struct v4l2_frequency *f); 790 struct v4l2_frequency *f);
792int saa7134_s_frequency(struct file *file, void *priv, 791int saa7134_s_frequency(struct file *file, void *priv,
793 const struct v4l2_frequency *f); 792 const struct v4l2_frequency *f);
794int saa7134_reqbufs(struct file *file, void *priv,
795 struct v4l2_requestbuffers *p);
796int saa7134_querybuf(struct file *file, void *priv,
797 struct v4l2_buffer *b);
798int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b);
799int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b);
800int saa7134_streamon(struct file *file, void *priv,
801 enum v4l2_buf_type type);
802int saa7134_streamoff(struct file *file, void *priv,
803 enum v4l2_buf_type type);
804 793
805int saa7134_videoport_init(struct saa7134_dev *dev); 794int saa7134_videoport_init(struct saa7134_dev *dev);
806void saa7134_set_tvnorm_hw(struct saa7134_dev *dev); 795void saa7134_set_tvnorm_hw(struct saa7134_dev *dev);
@@ -817,7 +806,16 @@ void saa7134_video_fini(struct saa7134_dev *dev);
817 806
818#define TS_PACKET_SIZE 188 /* TS packets 188 bytes */ 807#define TS_PACKET_SIZE 188 /* TS packets 188 bytes */
819 808
820extern struct videobuf_queue_ops saa7134_ts_qops; 809int saa7134_ts_buffer_init(struct vb2_buffer *vb2);
810int saa7134_ts_buffer_prepare(struct vb2_buffer *vb2);
811void saa7134_ts_buffer_finish(struct vb2_buffer *vb2);
812int saa7134_ts_queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
813 unsigned int *nbuffers, unsigned int *nplanes,
814 unsigned int sizes[], void *alloc_ctxs[]);
815int saa7134_ts_start_streaming(struct vb2_queue *vq, unsigned int count);
816void saa7134_ts_stop_streaming(struct vb2_queue *vq);
817
818extern struct vb2_ops saa7134_ts_qops;
821 819
822int saa7134_ts_init1(struct saa7134_dev *dev); 820int saa7134_ts_init1(struct saa7134_dev *dev);
823int saa7134_ts_fini(struct saa7134_dev *dev); 821int saa7134_ts_fini(struct saa7134_dev *dev);
@@ -834,7 +832,7 @@ int saa7134_ts_stop(struct saa7134_dev *dev);
834/* ----------------------------------------------------------- */ 832/* ----------------------------------------------------------- */
835/* saa7134-vbi.c */ 833/* saa7134-vbi.c */
836 834
837extern struct videobuf_queue_ops saa7134_vbi_qops; 835extern struct vb2_ops saa7134_vbi_qops;
838extern struct video_device saa7134_vbi_template; 836extern struct video_device saa7134_vbi_template;
839 837
840int saa7134_vbi_init1(struct saa7134_dev *dev); 838int saa7134_vbi_init1(struct saa7134_dev *dev);