aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDevin Heitmueller <dheitmueller@kernellabs.com>2013-01-04 14:16:24 -0500
committerMauro Carvalho Chehab <mchehab@redhat.com>2013-01-04 22:03:38 -0500
commitd3829fadc4611e96aa360b8ead5adefdf61f45ea (patch)
tree338ec2c07da5b7eb30465768b1814667150e9600
parent2a221d34b646c7e1f44a1b1d8af4eee18015fbda (diff)
[media] em28xx: convert to videobuf2
This patch converts the em28xx driver over to videobuf2. It is likely that em28xx_fh can go away entirely, but that will come in a separate patch. [mchehab@redhat.com: fix a non-trivial merge conflict with some VBI patches; CodingStyle fixes] Signed-off-by: Devin Heitmueller <dheitmueller@kernellabs.com> Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
-rw-r--r--drivers/media/usb/em28xx/Kconfig3
-rw-r--r--drivers/media/usb/em28xx/em28xx-cards.c7
-rw-r--r--drivers/media/usb/em28xx/em28xx-dvb.c4
-rw-r--r--drivers/media/usb/em28xx/em28xx-vbi.c123
-rw-r--r--drivers/media/usb/em28xx/em28xx-video.c760
-rw-r--r--drivers/media/usb/em28xx/em28xx.h30
6 files changed, 338 insertions, 589 deletions
diff --git a/drivers/media/usb/em28xx/Kconfig b/drivers/media/usb/em28xx/Kconfig
index 094c4ecf086d..c754a80a8d8b 100644
--- a/drivers/media/usb/em28xx/Kconfig
+++ b/drivers/media/usb/em28xx/Kconfig
@@ -3,7 +3,7 @@ config VIDEO_EM28XX
3 depends on VIDEO_DEV && I2C 3 depends on VIDEO_DEV && I2C
4 select VIDEO_TUNER 4 select VIDEO_TUNER
5 select VIDEO_TVEEPROM 5 select VIDEO_TVEEPROM
6 select VIDEOBUF_VMALLOC 6 select VIDEOBUF2_VMALLOC
7 select VIDEO_SAA711X if MEDIA_SUBDRV_AUTOSELECT 7 select VIDEO_SAA711X if MEDIA_SUBDRV_AUTOSELECT
8 select VIDEO_TVP5150 if MEDIA_SUBDRV_AUTOSELECT 8 select VIDEO_TVP5150 if MEDIA_SUBDRV_AUTOSELECT
9 select VIDEO_MSP3400 if MEDIA_SUBDRV_AUTOSELECT 9 select VIDEO_MSP3400 if MEDIA_SUBDRV_AUTOSELECT
@@ -48,7 +48,6 @@ config VIDEO_EM28XX_DVB
48 select DVB_S5H1409 if MEDIA_SUBDRV_AUTOSELECT 48 select DVB_S5H1409 if MEDIA_SUBDRV_AUTOSELECT
49 select MEDIA_TUNER_QT1010 if MEDIA_SUBDRV_AUTOSELECT 49 select MEDIA_TUNER_QT1010 if MEDIA_SUBDRV_AUTOSELECT
50 select MEDIA_TUNER_TDA18271 if MEDIA_SUBDRV_AUTOSELECT 50 select MEDIA_TUNER_TDA18271 if MEDIA_SUBDRV_AUTOSELECT
51 select VIDEOBUF_DVB
52 ---help--- 51 ---help---
53 This adds support for DVB cards based on the 52 This adds support for DVB cards based on the
54 Empiatech em28xx chips. 53 Empiatech em28xx chips.
diff --git a/drivers/media/usb/em28xx/em28xx-cards.c b/drivers/media/usb/em28xx/em28xx-cards.c
index 1aca98f30a8d..a4d11a46fd4f 100644
--- a/drivers/media/usb/em28xx/em28xx-cards.c
+++ b/drivers/media/usb/em28xx/em28xx-cards.c
@@ -57,7 +57,7 @@ module_param(disable_usb_speed_check, int, 0444);
57MODULE_PARM_DESC(disable_usb_speed_check, 57MODULE_PARM_DESC(disable_usb_speed_check,
58 "override min bandwidth requirement of 480M bps"); 58 "override min bandwidth requirement of 480M bps");
59 59
60static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; 60static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
61module_param_array(card, int, NULL, 0444); 61module_param_array(card, int, NULL, 0444);
62MODULE_PARM_DESC(card, "card type"); 62MODULE_PARM_DESC(card, "card type");
63 63
@@ -2965,6 +2965,8 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
2965 const char *chip_name = default_chip_name; 2965 const char *chip_name = default_chip_name;
2966 2966
2967 dev->udev = udev; 2967 dev->udev = udev;
2968 mutex_init(&dev->vb_queue_lock);
2969 mutex_init(&dev->vb_vbi_queue_lock);
2968 mutex_init(&dev->ctrl_urb_lock); 2970 mutex_init(&dev->ctrl_urb_lock);
2969 spin_lock_init(&dev->slock); 2971 spin_lock_init(&dev->slock);
2970 2972
@@ -3411,6 +3413,9 @@ static int em28xx_usb_probe(struct usb_interface *interface,
3411 /* save our data pointer in this interface device */ 3413 /* save our data pointer in this interface device */
3412 usb_set_intfdata(interface, dev); 3414 usb_set_intfdata(interface, dev);
3413 3415
3416 /* initialize videobuf2 stuff */
3417 em28xx_vb2_setup(dev);
3418
3414 /* allocate device struct */ 3419 /* allocate device struct */
3415 mutex_init(&dev->lock); 3420 mutex_init(&dev->lock);
3416 mutex_lock(&dev->lock); 3421 mutex_lock(&dev->lock);
diff --git a/drivers/media/usb/em28xx/em28xx-dvb.c b/drivers/media/usb/em28xx/em28xx-dvb.c
index a70b19e07e37..01bb8006f659 100644
--- a/drivers/media/usb/em28xx/em28xx-dvb.c
+++ b/drivers/media/usb/em28xx/em28xx-dvb.c
@@ -27,7 +27,9 @@
27 27
28#include "em28xx.h" 28#include "em28xx.h"
29#include <media/v4l2-common.h> 29#include <media/v4l2-common.h>
30#include <media/videobuf-vmalloc.h> 30#include <dvb_demux.h>
31#include <dvb_net.h>
32#include <dmxdev.h>
31#include <media/tuner.h> 33#include <media/tuner.h>
32#include "tuner-simple.h" 34#include "tuner-simple.h"
33#include <linux/gpio.h> 35#include <linux/gpio.h>
diff --git a/drivers/media/usb/em28xx/em28xx-vbi.c b/drivers/media/usb/em28xx/em28xx-vbi.c
index d74713bd1d18..39f39c527c13 100644
--- a/drivers/media/usb/em28xx/em28xx-vbi.c
+++ b/drivers/media/usb/em28xx/em28xx-vbi.c
@@ -41,105 +41,72 @@ MODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]");
41 41
42/* ------------------------------------------------------------------ */ 42/* ------------------------------------------------------------------ */
43 43
44static void 44static int vbi_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
45free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf) 45 unsigned int *nbuffers, unsigned int *nplanes,
46 unsigned int sizes[], void *alloc_ctxs[])
46{ 47{
47 struct em28xx_fh *fh = vq->priv_data; 48 struct em28xx *dev = vb2_get_drv_priv(vq);
48 struct em28xx *dev = fh->dev; 49 unsigned long size;
49 unsigned long flags = 0;
50 if (in_interrupt())
51 BUG();
52
53 /* We used to wait for the buffer to finish here, but this didn't work
54 because, as we were keeping the state as VIDEOBUF_QUEUED,
55 videobuf_queue_cancel marked it as finished for us.
56 (Also, it could wedge forever if the hardware was misconfigured.)
57
58 This should be safe; by the time we get here, the buffer isn't
59 queued anymore. If we ever start marking the buffers as
60 VIDEOBUF_ACTIVE, it won't be, though.
61 */
62 spin_lock_irqsave(&dev->slock, flags);
63 if (dev->usb_ctl.vbi_buf == buf)
64 dev->usb_ctl.vbi_buf = NULL;
65 spin_unlock_irqrestore(&dev->slock, flags);
66 50
67 videobuf_vmalloc_free(&buf->vb); 51 if (fmt)
68 buf->vb.state = VIDEOBUF_NEEDS_INIT; 52 size = fmt->fmt.pix.sizeimage;
69} 53 else
54 size = dev->vbi_width * dev->vbi_height * 2;
70 55
71static int 56 if (0 == *nbuffers)
72vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size) 57 *nbuffers = 32;
73{ 58 if (*nbuffers < 2)
74 struct em28xx_fh *fh = q->priv_data; 59 *nbuffers = 2;
75 struct em28xx *dev = fh->dev; 60 if (*nbuffers > 32)
61 *nbuffers = 32;
76 62
77 *size = dev->vbi_width * dev->vbi_height * 2; 63 *nplanes = 1;
64 sizes[0] = size;
78 65
79 if (0 == *count)
80 *count = vbibufs;
81 if (*count < 2)
82 *count = 2;
83 if (*count > 32)
84 *count = 32;
85 return 0; 66 return 0;
86} 67}
87 68
88static int 69static int vbi_buffer_prepare(struct vb2_buffer *vb)
89vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
90 enum v4l2_field field)
91{ 70{
92 struct em28xx_fh *fh = q->priv_data; 71 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
93 struct em28xx *dev = fh->dev;
94 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); 72 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
95 int rc = 0; 73 unsigned long size;
96 74
97 buf->vb.size = dev->vbi_width * dev->vbi_height * 2; 75 size = dev->vbi_width * dev->vbi_height * 2;
98 76
99 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) 77 if (vb2_plane_size(vb, 0) < size) {
78 printk(KERN_INFO "%s data will not fit into plane (%lu < %lu)\n",
79 __func__, vb2_plane_size(vb, 0), size);
100 return -EINVAL; 80 return -EINVAL;
101
102 buf->vb.width = dev->vbi_width;
103 buf->vb.height = dev->vbi_height;
104 buf->vb.field = field;
105
106 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
107 rc = videobuf_iolock(q, &buf->vb, NULL);
108 if (rc < 0)
109 goto fail;
110 } 81 }
82 vb2_set_plane_payload(&buf->vb, 0, size);
111 83
112 buf->vb.state = VIDEOBUF_PREPARED;
113 return 0; 84 return 0;
114
115fail:
116 free_buffer(q, buf);
117 return rc;
118} 85}
119 86
120static void 87static void
121vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) 88vbi_buffer_queue(struct vb2_buffer *vb)
122{
123 struct em28xx_buffer *buf = container_of(vb,
124 struct em28xx_buffer,
125 vb);
126 struct em28xx_fh *fh = vq->priv_data;
127 struct em28xx *dev = fh->dev;
128 struct em28xx_dmaqueue *vbiq = &dev->vbiq;
129
130 buf->vb.state = VIDEOBUF_QUEUED;
131 list_add_tail(&buf->vb.queue, &vbiq->active);
132}
133
134static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
135{ 89{
90 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
136 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); 91 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
137 free_buffer(q, buf); 92 struct em28xx_dmaqueue *vbiq = &dev->vbiq;
93 unsigned long flags = 0;
94
95 buf->mem = vb2_plane_vaddr(vb, 0);
96 buf->length = vb2_plane_size(vb, 0);
97
98 spin_lock_irqsave(&dev->slock, flags);
99 list_add_tail(&buf->list, &vbiq->active);
100 spin_unlock_irqrestore(&dev->slock, flags);
138} 101}
139 102
140struct videobuf_queue_ops em28xx_vbi_qops = { 103
141 .buf_setup = vbi_setup, 104struct vb2_ops em28xx_vbi_qops = {
142 .buf_prepare = vbi_prepare, 105 .queue_setup = vbi_queue_setup,
143 .buf_queue = vbi_queue, 106 .buf_prepare = vbi_buffer_prepare,
144 .buf_release = vbi_release, 107 .buf_queue = vbi_buffer_queue,
108 .start_streaming = em28xx_start_analog_streaming,
109 .stop_streaming = em28xx_stop_vbi_streaming,
110 .wait_prepare = vb2_ops_wait_prepare,
111 .wait_finish = vb2_ops_wait_finish,
145}; 112};
diff --git a/drivers/media/usb/em28xx/em28xx-video.c b/drivers/media/usb/em28xx/em28xx-video.c
index e26e01422a2d..57510c0d383c 100644
--- a/drivers/media/usb/em28xx/em28xx-video.c
+++ b/drivers/media/usb/em28xx/em28xx-video.c
@@ -76,9 +76,9 @@ MODULE_DESCRIPTION(DRIVER_DESC);
76MODULE_LICENSE("GPL"); 76MODULE_LICENSE("GPL");
77MODULE_VERSION(EM28XX_VERSION); 77MODULE_VERSION(EM28XX_VERSION);
78 78
79static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; 79static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
80static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; 80static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
81static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; 81static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
82 82
83module_param_array(video_nr, int, NULL, 0444); 83module_param_array(video_nr, int, NULL, 0444);
84module_param_array(vbi_nr, int, NULL, 0444); 84module_param_array(vbi_nr, int, NULL, 0444);
@@ -136,12 +136,13 @@ static struct em28xx_fmt format[] = {
136static inline void finish_buffer(struct em28xx *dev, 136static inline void finish_buffer(struct em28xx *dev,
137 struct em28xx_buffer *buf) 137 struct em28xx_buffer *buf)
138{ 138{
139 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i); 139 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
140 buf->vb.state = VIDEOBUF_DONE; 140
141 buf->vb.field_count++; 141 buf->vb.v4l2_buf.sequence = dev->field_count++;
142 v4l2_get_timestamp(&buf->vb.ts); 142 buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED;
143 list_del(&buf->vb.queue); 143 v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
144 wake_up(&buf->vb.done); 144
145 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
145} 146}
146 147
147/* 148/*
@@ -156,8 +157,8 @@ static void em28xx_copy_video(struct em28xx *dev,
156 int linesdone, currlinedone, offset, lencopy, remain; 157 int linesdone, currlinedone, offset, lencopy, remain;
157 int bytesperline = dev->width << 1; 158 int bytesperline = dev->width << 1;
158 159
159 if (buf->pos + len > buf->vb.size) 160 if (buf->pos + len > buf->length)
160 len = buf->vb.size - buf->pos; 161 len = buf->length - buf->pos;
161 162
162 startread = usb_buf; 163 startread = usb_buf;
163 remain = len; 164 remain = len;
@@ -179,11 +180,11 @@ static void em28xx_copy_video(struct em28xx *dev,
179 lencopy = bytesperline - currlinedone; 180 lencopy = bytesperline - currlinedone;
180 lencopy = lencopy > remain ? remain : lencopy; 181 lencopy = lencopy > remain ? remain : lencopy;
181 182
182 if ((char *)startwrite + lencopy > (char *)buf->vb_buf + buf->vb.size) { 183 if ((char *)startwrite + lencopy > (char *)buf->vb_buf + buf->length) {
183 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n", 184 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
184 ((char *)startwrite + lencopy) - 185 ((char *)startwrite + lencopy) -
185 ((char *)buf->vb_buf + buf->vb.size)); 186 ((char *)buf->vb_buf + buf->length));
186 remain = (char *)buf->vb_buf + buf->vb.size - 187 remain = (char *)buf->vb_buf + buf->length -
187 (char *)startwrite; 188 (char *)startwrite;
188 lencopy = remain; 189 lencopy = remain;
189 } 190 }
@@ -205,13 +206,13 @@ static void em28xx_copy_video(struct em28xx *dev,
205 lencopy = bytesperline; 206 lencopy = bytesperline;
206 207
207 if ((char *)startwrite + lencopy > (char *)buf->vb_buf + 208 if ((char *)startwrite + lencopy > (char *)buf->vb_buf +
208 buf->vb.size) { 209 buf->length) {
209 em28xx_isocdbg("Overflow of %zi bytes past buffer end" 210 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
210 "(2)\n", 211 "(2)\n",
211 ((char *)startwrite + lencopy) - 212 ((char *)startwrite + lencopy) -
212 ((char *)buf->vb_buf + buf->vb.size)); 213 ((char *)buf->vb_buf + buf->length));
213 lencopy = remain = (char *)buf->vb_buf + buf->vb.size - 214 lencopy = remain = (char *)buf->vb_buf + buf->length -
214 (char *)startwrite; 215 (char *)startwrite;
215 } 216 }
216 if (lencopy <= 0) 217 if (lencopy <= 0)
217 break; 218 break;
@@ -234,8 +235,8 @@ static void em28xx_copy_vbi(struct em28xx *dev,
234{ 235{
235 unsigned int offset; 236 unsigned int offset;
236 237
237 if (buf->pos + len > buf->vb.size) 238 if (buf->pos + len > buf->length)
238 len = buf->vb.size - buf->pos; 239 len = buf->length - buf->pos;
239 240
240 offset = buf->pos; 241 offset = buf->pos;
241 /* Make sure the bottom field populates the second half of the frame */ 242 /* Make sure the bottom field populates the second half of the frame */
@@ -292,7 +293,6 @@ static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
292 struct em28xx_dmaqueue *dma_q) 293 struct em28xx_dmaqueue *dma_q)
293{ 294{
294 struct em28xx_buffer *buf; 295 struct em28xx_buffer *buf;
295 char *outp;
296 296
297 if (list_empty(&dma_q->active)) { 297 if (list_empty(&dma_q->active)) {
298 em28xx_isocdbg("No active queue to serve\n"); 298 em28xx_isocdbg("No active queue to serve\n");
@@ -300,12 +300,11 @@ static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
300 } 300 }
301 301
302 /* Get the next buffer */ 302 /* Get the next buffer */
303 buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue); 303 buf = list_entry(dma_q->active.next, struct em28xx_buffer, list);
304 /* Cleans up buffer - Useful for testing for frame/URB loss */ 304 /* Cleans up buffer - Useful for testing for frame/URB loss */
305 outp = videobuf_to_vmalloc(&buf->vb); 305 list_del(&buf->list);
306 memset(outp, 0, buf->vb.size);
307 buf->pos = 0; 306 buf->pos = 0;
308 buf->vb_buf = outp; 307 buf->vb_buf = buf->mem;
309 308
310 return buf; 309 return buf;
311} 310}
@@ -467,92 +466,118 @@ static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
467} 466}
468 467
469 468
469static int get_ressource(enum v4l2_buf_type f_type)
470{
471 switch (f_type) {
472 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
473 return EM28XX_RESOURCE_VIDEO;
474 case V4L2_BUF_TYPE_VBI_CAPTURE:
475 return EM28XX_RESOURCE_VBI;
476 default:
477 BUG();
478 return 0;
479 }
480}
481
482/* Usage lock check functions */
483static int res_get(struct em28xx *dev, enum v4l2_buf_type f_type)
484{
485 int res_type = get_ressource(f_type);
486
487 /* is it free? */
488 if (dev->resources & res_type) {
489 /* no, someone else uses it */
490 return -EBUSY;
491 }
492
493 /* it's free, grab it */
494 dev->resources |= res_type;
495 em28xx_videodbg("res: get %d\n", res_type);
496 return 0;
497}
498
499static void res_free(struct em28xx *dev, enum v4l2_buf_type f_type)
500{
501 int res_type = get_ressource(f_type);
502
503 dev->resources &= ~res_type;
504 em28xx_videodbg("res: put %d\n", res_type);
505}
506
470/* ------------------------------------------------------------------ 507/* ------------------------------------------------------------------
471 Videobuf operations 508 Videobuf2 operations
472 ------------------------------------------------------------------*/ 509 ------------------------------------------------------------------*/
473 510
474static int 511static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
475buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size) 512 unsigned int *nbuffers, unsigned int *nplanes,
513 unsigned int sizes[], void *alloc_ctxs[])
476{ 514{
477 struct em28xx_fh *fh = vq->priv_data; 515 struct em28xx *dev = vb2_get_drv_priv(vq);
478 struct em28xx *dev = fh->dev; 516 unsigned long size;
479 struct v4l2_frequency f;
480
481 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
482 >> 3;
483 517
484 if (0 == *count) 518 if (fmt)
485 *count = EM28XX_DEF_BUF; 519 size = fmt->fmt.pix.sizeimage;
520 else
521 size = (dev->width * dev->height * dev->format->depth + 7) >> 3;
486 522
487 if (*count < EM28XX_MIN_BUF) 523 if (size == 0)
488 *count = EM28XX_MIN_BUF; 524 return -EINVAL;
489 525
490 /* Ask tuner to go to analog or radio mode */ 526 if (0 == *nbuffers)
491 memset(&f, 0, sizeof(f)); 527 *nbuffers = 32;
492 f.frequency = dev->ctl_freq;
493 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
494 528
495 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f); 529 *nplanes = 1;
530 sizes[0] = size;
496 531
497 return 0; 532 return 0;
498} 533}
499 534
500/* This is called *without* dev->slock held; please keep it that way */ 535static int
501static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf) 536buffer_prepare(struct vb2_buffer *vb)
502{ 537{
503 struct em28xx_fh *fh = vq->priv_data; 538 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
504 struct em28xx *dev = fh->dev; 539 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
505 unsigned long flags = 0; 540 unsigned long size;
506 if (in_interrupt())
507 BUG();
508 541
509 /* We used to wait for the buffer to finish here, but this didn't work 542 em28xx_videodbg("%s, field=%d\n", __func__, vb->v4l2_buf.field);
510 because, as we were keeping the state as VIDEOBUF_QUEUED,
511 videobuf_queue_cancel marked it as finished for us.
512 (Also, it could wedge forever if the hardware was misconfigured.)
513 543
514 This should be safe; by the time we get here, the buffer isn't 544 size = (dev->width * dev->height * dev->format->depth + 7) >> 3;
515 queued anymore. If we ever start marking the buffers as
516 VIDEOBUF_ACTIVE, it won't be, though.
517 */
518 spin_lock_irqsave(&dev->slock, flags);
519 if (dev->usb_ctl.vid_buf == buf)
520 dev->usb_ctl.vid_buf = NULL;
521 spin_unlock_irqrestore(&dev->slock, flags);
522 545
523 videobuf_vmalloc_free(&buf->vb); 546 if (vb2_plane_size(vb, 0) < size) {
524 buf->vb.state = VIDEOBUF_NEEDS_INIT; 547 em28xx_videodbg("%s data will not fit into plane (%lu < %lu)\n",
548 __func__, vb2_plane_size(vb, 0), size);
549 return -EINVAL;
550 }
551 vb2_set_plane_payload(&buf->vb, 0, size);
552
553 return 0;
525} 554}
526 555
527static int 556int em28xx_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
528buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
529 enum v4l2_field field)
530{ 557{
531 struct em28xx_fh *fh = vq->priv_data; 558 struct em28xx *dev = vb2_get_drv_priv(vq);
532 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); 559 struct v4l2_frequency f;
533 struct em28xx *dev = fh->dev; 560 int rc = 0;
534 int rc = 0, urb_init = 0;
535 561
536 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth 562 em28xx_videodbg("%s\n", __func__);
537 + 7) >> 3;
538 563
539 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) 564 /* Make sure streaming is not already in progress for this type
540 return -EINVAL; 565 of filehandle (e.g. video, vbi) */
566 rc = res_get(dev, vq->type);
567 if (rc)
568 return rc;
541 569
542 buf->vb.width = dev->width; 570 if (dev->streaming_users++ == 0) {
543 buf->vb.height = dev->height; 571 /* First active streaming user, so allocate all the URBs */
544 buf->vb.field = field;
545 572
546 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { 573 /* Allocate the USB bandwidth */
547 rc = videobuf_iolock(vq, &buf->vb, NULL); 574 em28xx_set_alternate(dev);
548 if (rc < 0)
549 goto fail;
550 }
551 575
552 if (!dev->usb_ctl.analog_bufs.num_bufs) 576 /* Needed, since GPIO might have disabled power of
553 urb_init = 1; 577 some i2c device
578 */
579 em28xx_wake_i2c(dev);
554 580
555 if (urb_init) {
556 dev->capture_type = -1; 581 dev->capture_type = -1;
557 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE, 582 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
558 dev->analog_xfer_bulk, 583 dev->analog_xfer_bulk,
@@ -562,52 +587,144 @@ buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
562 em28xx_urb_data_copy); 587 em28xx_urb_data_copy);
563 if (rc < 0) 588 if (rc < 0)
564 goto fail; 589 goto fail;
565 }
566 590
567 buf->vb.state = VIDEOBUF_PREPARED; 591 /*
568 return 0; 592 * djh: it's not clear whether this code is still needed. I'm
593 * leaving it in here for now entirely out of concern for
594 * backward compatibility (the old code did it)
595 */
596
597 /* Ask tuner to go to analog or radio mode */
598 memset(&f, 0, sizeof(f));
599 f.frequency = dev->ctl_freq;
600 if (vq->owner && vq->owner->vdev->vfl_type == VFL_TYPE_RADIO)
601 f.type = V4L2_TUNER_RADIO;
602 else
603 f.type = V4L2_TUNER_ANALOG_TV;
604 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
605 }
569 606
570fail: 607fail:
571 free_buffer(vq, buf);
572 return rc; 608 return rc;
573} 609}
574 610
575static void 611int em28xx_stop_streaming(struct vb2_queue *vq)
576buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) 612{
613 struct em28xx *dev = vb2_get_drv_priv(vq);
614 struct em28xx_dmaqueue *vidq = &dev->vidq;
615 unsigned long flags = 0;
616
617 em28xx_videodbg("%s\n", __func__);
618
619 res_free(dev, vq->type);
620
621 if (dev->streaming_users-- == 1) {
622 /* Last active user, so shutdown all the URBS */
623 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
624 }
625
626 spin_lock_irqsave(&dev->slock, flags);
627 while (!list_empty(&vidq->active)) {
628 struct em28xx_buffer *buf;
629 buf = list_entry(vidq->active.next, struct em28xx_buffer, list);
630 list_del(&buf->list);
631 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
632 }
633 dev->usb_ctl.vid_buf = NULL;
634 spin_unlock_irqrestore(&dev->slock, flags);
635
636 return 0;
637}
638
639int em28xx_stop_vbi_streaming(struct vb2_queue *vq)
577{ 640{
578 struct em28xx_buffer *buf = container_of(vb, 641 struct em28xx *dev = vb2_get_drv_priv(vq);
579 struct em28xx_buffer, 642 struct em28xx_dmaqueue *vbiq = &dev->vbiq;
580 vb); 643 unsigned long flags = 0;
581 struct em28xx_fh *fh = vq->priv_data; 644
582 struct em28xx *dev = fh->dev; 645 em28xx_videodbg("%s\n", __func__);
583 struct em28xx_dmaqueue *vidq = &dev->vidq; 646
647 res_free(dev, vq->type);
584 648
585 buf->vb.state = VIDEOBUF_QUEUED; 649 if (dev->streaming_users-- == 1) {
586 list_add_tail(&buf->vb.queue, &vidq->active); 650 /* Last active user, so shutdown all the URBS */
651 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
652 }
653
654 spin_lock_irqsave(&dev->slock, flags);
655 while (!list_empty(&vbiq->active)) {
656 struct em28xx_buffer *buf;
657 buf = list_entry(vbiq->active.next, struct em28xx_buffer, list);
658 list_del(&buf->list);
659 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
660 }
661 dev->usb_ctl.vbi_buf = NULL;
662 spin_unlock_irqrestore(&dev->slock, flags);
587 663
664 return 0;
588} 665}
589 666
590static void buffer_release(struct videobuf_queue *vq, 667static void
591 struct videobuf_buffer *vb) 668buffer_queue(struct vb2_buffer *vb)
592{ 669{
593 struct em28xx_buffer *buf = container_of(vb, 670 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
594 struct em28xx_buffer, 671 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
595 vb); 672 struct em28xx_dmaqueue *vidq = &dev->vidq;
596 struct em28xx_fh *fh = vq->priv_data; 673 unsigned long flags = 0;
597 struct em28xx *dev = (struct em28xx *)fh->dev;
598 674
599 em28xx_isocdbg("em28xx: called buffer_release\n"); 675 em28xx_videodbg("%s\n", __func__);
676 buf->mem = vb2_plane_vaddr(vb, 0);
677 buf->length = vb2_plane_size(vb, 0);
600 678
601 free_buffer(vq, buf); 679 spin_lock_irqsave(&dev->slock, flags);
680 list_add_tail(&buf->list, &vidq->active);
681 spin_unlock_irqrestore(&dev->slock, flags);
602} 682}
603 683
604static struct videobuf_queue_ops em28xx_video_qops = { 684static struct vb2_ops em28xx_video_qops = {
605 .buf_setup = buffer_setup, 685 .queue_setup = queue_setup,
606 .buf_prepare = buffer_prepare, 686 .buf_prepare = buffer_prepare,
607 .buf_queue = buffer_queue, 687 .buf_queue = buffer_queue,
608 .buf_release = buffer_release, 688 .start_streaming = em28xx_start_analog_streaming,
689 .stop_streaming = em28xx_stop_streaming,
690 .wait_prepare = vb2_ops_wait_prepare,
691 .wait_finish = vb2_ops_wait_finish,
609}; 692};
610 693
694int em28xx_vb2_setup(struct em28xx *dev)
695{
696 int rc;
697 struct vb2_queue *q;
698
699 /* Setup Videobuf2 for Video capture */
700 q = &dev->vb_vidq;
701 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
702 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
703 q->drv_priv = dev;
704 q->buf_struct_size = sizeof(struct em28xx_buffer);
705 q->ops = &em28xx_video_qops;
706 q->mem_ops = &vb2_vmalloc_memops;
707
708 rc = vb2_queue_init(q);
709 if (rc < 0)
710 return rc;
711
712 /* Setup Videobuf2 for VBI capture */
713 q = &dev->vb_vbiq;
714 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
715 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
716 q->drv_priv = dev;
717 q->buf_struct_size = sizeof(struct em28xx_buffer);
718 q->ops = &em28xx_vbi_qops;
719 q->mem_ops = &vb2_vmalloc_memops;
720
721 rc = vb2_queue_init(q);
722 if (rc < 0)
723 return rc;
724
725 return 0;
726}
727
611/********************* v4l2 interface **************************************/ 728/********************* v4l2 interface **************************************/
612 729
613static void video_mux(struct em28xx *dev, int index) 730static void video_mux(struct em28xx *dev, int index)
@@ -640,61 +757,6 @@ static void video_mux(struct em28xx *dev, int index)
640 em28xx_audio_analog_set(dev); 757 em28xx_audio_analog_set(dev);
641} 758}
642 759
643/* Usage lock check functions */
644static int res_get(struct em28xx_fh *fh, unsigned int bit)
645{
646 struct em28xx *dev = fh->dev;
647
648 if (fh->resources & bit)
649 /* have it already allocated */
650 return 1;
651
652 /* is it free? */
653 if (dev->resources & bit) {
654 /* no, someone else uses it */
655 return 0;
656 }
657 /* it's free, grab it */
658 fh->resources |= bit;
659 dev->resources |= bit;
660 em28xx_videodbg("res: get %d\n", bit);
661 return 1;
662}
663
664static int res_check(struct em28xx_fh *fh, unsigned int bit)
665{
666 return fh->resources & bit;
667}
668
669static int res_locked(struct em28xx *dev, unsigned int bit)
670{
671 return dev->resources & bit;
672}
673
674static void res_free(struct em28xx_fh *fh, unsigned int bits)
675{
676 struct em28xx *dev = fh->dev;
677
678 BUG_ON((fh->resources & bits) != bits);
679
680 fh->resources &= ~bits;
681 dev->resources &= ~bits;
682 em28xx_videodbg("res: put %d\n", bits);
683}
684
685static int get_ressource(struct em28xx_fh *fh)
686{
687 switch (fh->type) {
688 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
689 return EM28XX_RESOURCE_VIDEO;
690 case V4L2_BUF_TYPE_VBI_CAPTURE:
691 return EM28XX_RESOURCE_VBI;
692 default:
693 BUG();
694 return 0;
695 }
696}
697
698void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv) 760void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv)
699{ 761{
700 struct em28xx *dev = priv; 762 struct em28xx *dev = priv;
@@ -828,8 +890,11 @@ static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
828 /* the em2800 can only scale down to 50% */ 890 /* the em2800 can only scale down to 50% */
829 height = height > (3 * maxh / 4) ? maxh : maxh / 2; 891 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
830 width = width > (3 * maxw / 4) ? maxw : maxw / 2; 892 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
831 /* MaxPacketSize for em2800 is too small to capture at full resolution 893 /*
832 * use half of maxw as the scaler can only scale to 50% */ 894 * MaxPacketSize for em2800 is too small to capture at full
895 * resolution use half of maxw as the scaler can only scale
896 * to 50%
897 */
833 if (width == maxw && height == maxh) 898 if (width == maxw && height == maxh)
834 width /= 2; 899 width /= 2;
835 } else { 900 } else {
@@ -875,7 +940,6 @@ static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
875 /* set new image size */ 940 /* set new image size */
876 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); 941 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
877 942
878 em28xx_set_alternate(dev);
879 em28xx_resolution_set(dev); 943 em28xx_resolution_set(dev);
880 944
881 return 0; 945 return 0;
@@ -884,21 +948,13 @@ static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
884static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, 948static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
885 struct v4l2_format *f) 949 struct v4l2_format *f)
886{ 950{
887 struct em28xx_fh *fh = priv; 951 struct em28xx *dev = video_drvdata(file);
888 struct em28xx *dev = fh->dev;
889 int rc;
890 952
891 rc = check_dev(dev); 953 if (dev->streaming_users > 0)
892 if (rc < 0) 954 return -EBUSY;
893 return rc;
894 955
895 vidioc_try_fmt_vid_cap(file, priv, f); 956 vidioc_try_fmt_vid_cap(file, priv, f);
896 957
897 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
898 em28xx_errdev("%s queue busy\n", __func__);
899 return -EBUSY;
900 }
901
902 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat, 958 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
903 f->fmt.pix.width, f->fmt.pix.height); 959 f->fmt.pix.width, f->fmt.pix.height);
904} 960}
@@ -952,10 +1008,8 @@ static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
952 if (rc < 0) 1008 if (rc < 0)
953 return rc; 1009 return rc;
954 1010
955 if (videobuf_queue_is_busy(&fh->vb_vidq)) { 1011 if (dev->streaming_users > 0)
956 em28xx_errdev("%s queue busy\n", __func__);
957 return -EBUSY; 1012 return -EBUSY;
958 }
959 1013
960 dev->norm = *norm; 1014 dev->norm = *norm;
961 1015
@@ -1358,69 +1412,6 @@ static int vidioc_cropcap(struct file *file, void *priv,
1358 return 0; 1412 return 0;
1359} 1413}
1360 1414
1361static int vidioc_streamon(struct file *file, void *priv,
1362 enum v4l2_buf_type type)
1363{
1364 struct em28xx_fh *fh = priv;
1365 struct em28xx *dev = fh->dev;
1366 int rc = -EINVAL;
1367
1368 rc = check_dev(dev);
1369 if (rc < 0)
1370 return rc;
1371
1372 if (unlikely(type != fh->type))
1373 return -EINVAL;
1374
1375 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1376 fh, type, fh->resources, dev->resources);
1377
1378 if (unlikely(!res_get(fh, get_ressource(fh))))
1379 return -EBUSY;
1380
1381 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1382 rc = videobuf_streamon(&fh->vb_vidq);
1383 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1384 rc = videobuf_streamon(&fh->vb_vbiq);
1385
1386 return rc;
1387}
1388
1389static int vidioc_streamoff(struct file *file, void *priv,
1390 enum v4l2_buf_type type)
1391{
1392 struct em28xx_fh *fh = priv;
1393 struct em28xx *dev = fh->dev;
1394 int rc;
1395
1396 rc = check_dev(dev);
1397 if (rc < 0)
1398 return rc;
1399
1400 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1401 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
1402 return -EINVAL;
1403 if (type != fh->type)
1404 return -EINVAL;
1405
1406 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1407 fh, type, fh->resources, dev->resources);
1408
1409 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1410 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1411 videobuf_streamoff(&fh->vb_vidq);
1412 res_free(fh, EM28XX_RESOURCE_VIDEO);
1413 }
1414 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1415 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1416 videobuf_streamoff(&fh->vb_vbiq);
1417 res_free(fh, EM28XX_RESOURCE_VBI);
1418 }
1419 }
1420
1421 return 0;
1422}
1423
1424static int vidioc_querycap(struct file *file, void *priv, 1415static int vidioc_querycap(struct file *file, void *priv,
1425 struct v4l2_capability *cap) 1416 struct v4l2_capability *cap)
1426{ 1417{
@@ -1566,83 +1557,6 @@ static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1566 return 0; 1557 return 0;
1567} 1558}
1568 1559
1569static int vidioc_reqbufs(struct file *file, void *priv,
1570 struct v4l2_requestbuffers *rb)
1571{
1572 struct em28xx_fh *fh = priv;
1573 struct em28xx *dev = fh->dev;
1574 int rc;
1575
1576 rc = check_dev(dev);
1577 if (rc < 0)
1578 return rc;
1579
1580 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1581 return videobuf_reqbufs(&fh->vb_vidq, rb);
1582 else
1583 return videobuf_reqbufs(&fh->vb_vbiq, rb);
1584}
1585
1586static int vidioc_querybuf(struct file *file, void *priv,
1587 struct v4l2_buffer *b)
1588{
1589 struct em28xx_fh *fh = priv;
1590 struct em28xx *dev = fh->dev;
1591 int rc;
1592
1593 rc = check_dev(dev);
1594 if (rc < 0)
1595 return rc;
1596
1597 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1598 return videobuf_querybuf(&fh->vb_vidq, b);
1599 else {
1600 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1601 the videobuf framework, but we probably shouldn't be
1602 returning a buffer larger than that which was asked for.
1603 At a minimum, it causes a crash in zvbi since it does
1604 a memcpy based on the source buffer length */
1605 int result = videobuf_querybuf(&fh->vb_vbiq, b);
1606 b->length = dev->vbi_width * dev->vbi_height * 2;
1607
1608 return result;
1609 }
1610}
1611
1612static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1613{
1614 struct em28xx_fh *fh = priv;
1615 struct em28xx *dev = fh->dev;
1616 int rc;
1617
1618 rc = check_dev(dev);
1619 if (rc < 0)
1620 return rc;
1621
1622 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1623 return videobuf_qbuf(&fh->vb_vidq, b);
1624 else
1625 return videobuf_qbuf(&fh->vb_vbiq, b);
1626}
1627
1628static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1629{
1630 struct em28xx_fh *fh = priv;
1631 struct em28xx *dev = fh->dev;
1632 int rc;
1633
1634 rc = check_dev(dev);
1635 if (rc < 0)
1636 return rc;
1637
1638 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1639 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1640 O_NONBLOCK);
1641 else
1642 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1643 O_NONBLOCK);
1644}
1645
1646/* ----------------------------------------------------------- */ 1560/* ----------------------------------------------------------- */
1647/* RADIO ESPECIFIC IOCTLS */ 1561/* RADIO ESPECIFIC IOCTLS */
1648/* ----------------------------------------------------------- */ 1562/* ----------------------------------------------------------- */
@@ -1682,12 +1596,10 @@ static int radio_s_tuner(struct file *file, void *priv,
1682 */ 1596 */
1683static int em28xx_v4l2_open(struct file *filp) 1597static int em28xx_v4l2_open(struct file *filp)
1684{ 1598{
1685 int errCode = 0, radio = 0;
1686 struct video_device *vdev = video_devdata(filp); 1599 struct video_device *vdev = video_devdata(filp);
1687 struct em28xx *dev = video_drvdata(filp); 1600 struct em28xx *dev = video_drvdata(filp);
1688 enum v4l2_buf_type fh_type = 0; 1601 enum v4l2_buf_type fh_type = 0;
1689 struct em28xx_fh *fh; 1602 struct em28xx_fh *fh;
1690 enum v4l2_field field;
1691 1603
1692 switch (vdev->vfl_type) { 1604 switch (vdev->vfl_type) {
1693 case VFL_TYPE_GRABBER: 1605 case VFL_TYPE_GRABBER:
@@ -1696,9 +1608,6 @@ static int em28xx_v4l2_open(struct file *filp)
1696 case VFL_TYPE_VBI: 1608 case VFL_TYPE_VBI:
1697 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE; 1609 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1698 break; 1610 break;
1699 case VFL_TYPE_RADIO:
1700 radio = 1;
1701 break;
1702 } 1611 }
1703 1612
1704 em28xx_videodbg("open dev=%s type=%s users=%d\n", 1613 em28xx_videodbg("open dev=%s type=%s users=%d\n",
@@ -1716,13 +1625,11 @@ static int em28xx_v4l2_open(struct file *filp)
1716 } 1625 }
1717 v4l2_fh_init(&fh->fh, vdev); 1626 v4l2_fh_init(&fh->fh, vdev);
1718 fh->dev = dev; 1627 fh->dev = dev;
1719 fh->radio = radio;
1720 fh->type = fh_type; 1628 fh->type = fh_type;
1721 filp->private_data = fh; 1629 filp->private_data = fh;
1722 1630
1723 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) { 1631 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1724 em28xx_set_mode(dev, EM28XX_ANALOG_MODE); 1632 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
1725 em28xx_set_alternate(dev);
1726 em28xx_resolution_set(dev); 1633 em28xx_resolution_set(dev);
1727 1634
1728 /* Needed, since GPIO might have disabled power of 1635 /* Needed, since GPIO might have disabled power of
@@ -1731,32 +1638,18 @@ static int em28xx_v4l2_open(struct file *filp)
1731 em28xx_wake_i2c(dev); 1638 em28xx_wake_i2c(dev);
1732 1639
1733 } 1640 }
1734 if (fh->radio) { 1641
1642 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1735 em28xx_videodbg("video_open: setting radio device\n"); 1643 em28xx_videodbg("video_open: setting radio device\n");
1736 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio); 1644 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1737 } 1645 }
1738 1646
1739 dev->users++; 1647 dev->users++;
1740 1648
1741 if (dev->progressive)
1742 field = V4L2_FIELD_NONE;
1743 else
1744 field = V4L2_FIELD_INTERLACED;
1745
1746 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1747 NULL, &dev->slock,
1748 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
1749 sizeof(struct em28xx_buffer), fh, &dev->lock);
1750
1751 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
1752 NULL, &dev->slock,
1753 V4L2_BUF_TYPE_VBI_CAPTURE,
1754 V4L2_FIELD_SEQ_TB,
1755 sizeof(struct em28xx_buffer), fh, &dev->lock);
1756 mutex_unlock(&dev->lock); 1649 mutex_unlock(&dev->lock);
1757 v4l2_fh_add(&fh->fh); 1650 v4l2_fh_add(&fh->fh);
1758 1651
1759 return errCode; 1652 return 0;
1760} 1653}
1761 1654
1762/* 1655/*
@@ -1810,15 +1703,7 @@ static int em28xx_v4l2_close(struct file *filp)
1810 em28xx_videodbg("users=%d\n", dev->users); 1703 em28xx_videodbg("users=%d\n", dev->users);
1811 1704
1812 mutex_lock(&dev->lock); 1705 mutex_lock(&dev->lock);
1813 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) { 1706 vb2_fop_release(filp);
1814 videobuf_stop(&fh->vb_vidq);
1815 res_free(fh, EM28XX_RESOURCE_VIDEO);
1816 }
1817
1818 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1819 videobuf_stop(&fh->vb_vbiq);
1820 res_free(fh, EM28XX_RESOURCE_VBI);
1821 }
1822 1707
1823 if (dev->users == 1) { 1708 if (dev->users == 1) {
1824 /* the device is already disconnect, 1709 /* the device is already disconnect,
@@ -1828,7 +1713,6 @@ static int em28xx_v4l2_close(struct file *filp)
1828 kfree(dev->alt_max_pkt_size_isoc); 1713 kfree(dev->alt_max_pkt_size_isoc);
1829 mutex_unlock(&dev->lock); 1714 mutex_unlock(&dev->lock);
1830 kfree(dev); 1715 kfree(dev);
1831 kfree(fh);
1832 return 0; 1716 return 0;
1833 } 1717 }
1834 1718
@@ -1836,7 +1720,6 @@ static int em28xx_v4l2_close(struct file *filp)
1836 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0); 1720 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
1837 1721
1838 /* do this before setting alternate! */ 1722 /* do this before setting alternate! */
1839 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
1840 em28xx_set_mode(dev, EM28XX_SUSPEND); 1723 em28xx_set_mode(dev, EM28XX_SUSPEND);
1841 1724
1842 /* set alternate 0 */ 1725 /* set alternate 0 */
@@ -1848,141 +1731,19 @@ static int em28xx_v4l2_close(struct file *filp)
1848 "0 (error=%i)\n", errCode); 1731 "0 (error=%i)\n", errCode);
1849 } 1732 }
1850 } 1733 }
1851 v4l2_fh_del(&fh->fh);
1852 v4l2_fh_exit(&fh->fh);
1853 1734
1854 videobuf_mmap_free(&fh->vb_vidq);
1855 videobuf_mmap_free(&fh->vb_vbiq);
1856 kfree(fh);
1857 dev->users--; 1735 dev->users--;
1858 mutex_unlock(&dev->lock); 1736 mutex_unlock(&dev->lock);
1859 return 0; 1737 return 0;
1860} 1738}
1861 1739
1862/*
1863 * em28xx_v4l2_read()
1864 * will allocate buffers when called for the first time
1865 */
1866static ssize_t
1867em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1868 loff_t *pos)
1869{
1870 struct em28xx_fh *fh = filp->private_data;
1871 struct em28xx *dev = fh->dev;
1872 int rc;
1873
1874 rc = check_dev(dev);
1875 if (rc < 0)
1876 return rc;
1877
1878 if (mutex_lock_interruptible(&dev->lock))
1879 return -ERESTARTSYS;
1880 /* FIXME: read() is not prepared to allow changing the video
1881 resolution while streaming. Seems a bug at em28xx_set_fmt
1882 */
1883
1884 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1885 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
1886 rc = -EBUSY;
1887 else
1888 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1889 filp->f_flags & O_NONBLOCK);
1890 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1891 if (!res_get(fh, EM28XX_RESOURCE_VBI))
1892 rc = -EBUSY;
1893 else
1894 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
1895 filp->f_flags & O_NONBLOCK);
1896 }
1897 mutex_unlock(&dev->lock);
1898
1899 return rc;
1900}
1901
1902/*
1903 * em28xx_poll()
1904 * will allocate buffers when called for the first time
1905 */
1906static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
1907{
1908 struct em28xx_fh *fh = filp->private_data;
1909 unsigned long req_events = poll_requested_events(wait);
1910 struct em28xx *dev = fh->dev;
1911 unsigned int res = 0;
1912 int rc;
1913
1914 rc = check_dev(dev);
1915 if (rc < 0)
1916 return DEFAULT_POLLMASK;
1917
1918 if (v4l2_event_pending(&fh->fh))
1919 res = POLLPRI;
1920 else if (req_events & POLLPRI)
1921 poll_wait(filp, &fh->fh.wait, wait);
1922
1923 if (req_events & (POLLIN | POLLRDNORM)) {
1924 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1925 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
1926 return res | POLLERR;
1927 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1928 }
1929 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1930 if (!res_get(fh, EM28XX_RESOURCE_VBI))
1931 return res | POLLERR;
1932 return res | videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
1933 }
1934 }
1935 return res;
1936}
1937
1938static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
1939{
1940 struct em28xx_fh *fh = filp->private_data;
1941 struct em28xx *dev = fh->dev;
1942 unsigned int res;
1943
1944 mutex_lock(&dev->lock);
1945 res = em28xx_poll(filp, wait);
1946 mutex_unlock(&dev->lock);
1947 return res;
1948}
1949
1950/*
1951 * em28xx_v4l2_mmap()
1952 */
1953static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1954{
1955 struct em28xx_fh *fh = filp->private_data;
1956 struct em28xx *dev = fh->dev;
1957 int rc;
1958
1959 rc = check_dev(dev);
1960 if (rc < 0)
1961 return rc;
1962
1963 if (mutex_lock_interruptible(&dev->lock))
1964 return -ERESTARTSYS;
1965 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1966 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1967 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1968 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
1969 mutex_unlock(&dev->lock);
1970
1971 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1972 (unsigned long)vma->vm_start,
1973 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1974 rc);
1975
1976 return rc;
1977}
1978
1979static const struct v4l2_file_operations em28xx_v4l_fops = { 1740static const struct v4l2_file_operations em28xx_v4l_fops = {
1980 .owner = THIS_MODULE, 1741 .owner = THIS_MODULE,
1981 .open = em28xx_v4l2_open, 1742 .open = em28xx_v4l2_open,
1982 .release = em28xx_v4l2_close, 1743 .release = em28xx_v4l2_close,
1983 .read = em28xx_v4l2_read, 1744 .read = vb2_fop_read,
1984 .poll = em28xx_v4l2_poll, 1745 .poll = vb2_fop_poll,
1985 .mmap = em28xx_v4l2_mmap, 1746 .mmap = vb2_fop_mmap,
1986 .unlocked_ioctl = video_ioctl2, 1747 .unlocked_ioctl = video_ioctl2,
1987}; 1748};
1988 1749
@@ -2000,10 +1761,13 @@ static const struct v4l2_ioctl_ops video_ioctl_ops = {
2000 .vidioc_s_audio = vidioc_s_audio, 1761 .vidioc_s_audio = vidioc_s_audio,
2001 .vidioc_cropcap = vidioc_cropcap, 1762 .vidioc_cropcap = vidioc_cropcap,
2002 1763
2003 .vidioc_reqbufs = vidioc_reqbufs, 1764 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2004 .vidioc_querybuf = vidioc_querybuf, 1765 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2005 .vidioc_qbuf = vidioc_qbuf, 1766 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
2006 .vidioc_dqbuf = vidioc_dqbuf, 1767 .vidioc_querybuf = vb2_ioctl_querybuf,
1768 .vidioc_qbuf = vb2_ioctl_qbuf,
1769 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1770
2007 .vidioc_g_std = vidioc_g_std, 1771 .vidioc_g_std = vidioc_g_std,
2008 .vidioc_querystd = vidioc_querystd, 1772 .vidioc_querystd = vidioc_querystd,
2009 .vidioc_s_std = vidioc_s_std, 1773 .vidioc_s_std = vidioc_s_std,
@@ -2012,8 +1776,8 @@ static const struct v4l2_ioctl_ops video_ioctl_ops = {
2012 .vidioc_enum_input = vidioc_enum_input, 1776 .vidioc_enum_input = vidioc_enum_input,
2013 .vidioc_g_input = vidioc_g_input, 1777 .vidioc_g_input = vidioc_g_input,
2014 .vidioc_s_input = vidioc_s_input, 1778 .vidioc_s_input = vidioc_s_input,
2015 .vidioc_streamon = vidioc_streamon, 1779 .vidioc_streamon = vb2_ioctl_streamon,
2016 .vidioc_streamoff = vidioc_streamoff, 1780 .vidioc_streamoff = vb2_ioctl_streamoff,
2017 .vidioc_g_tuner = vidioc_g_tuner, 1781 .vidioc_g_tuner = vidioc_g_tuner,
2018 .vidioc_s_tuner = vidioc_s_tuner, 1782 .vidioc_s_tuner = vidioc_s_tuner,
2019 .vidioc_g_frequency = vidioc_g_frequency, 1783 .vidioc_g_frequency = vidioc_g_frequency,
@@ -2029,7 +1793,7 @@ static const struct v4l2_ioctl_ops video_ioctl_ops = {
2029 1793
2030static const struct video_device em28xx_video_template = { 1794static const struct video_device em28xx_video_template = {
2031 .fops = &em28xx_v4l_fops, 1795 .fops = &em28xx_v4l_fops,
2032 .release = video_device_release, 1796 .release = video_device_release_empty,
2033 .ioctl_ops = &video_ioctl_ops, 1797 .ioctl_ops = &video_ioctl_ops,
2034 1798
2035 .tvnorms = V4L2_STD_ALL, 1799 .tvnorms = V4L2_STD_ALL,
@@ -2078,7 +1842,6 @@ static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2078 1842
2079 *vfd = *template; 1843 *vfd = *template;
2080 vfd->v4l2_dev = &dev->v4l2_dev; 1844 vfd->v4l2_dev = &dev->v4l2_dev;
2081 vfd->release = video_device_release;
2082 vfd->debug = video_debug; 1845 vfd->debug = video_debug;
2083 vfd->lock = &dev->lock; 1846 vfd->lock = &dev->lock;
2084 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags); 1847 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
@@ -2110,10 +1873,10 @@ int em28xx_register_analog_devices(struct em28xx *dev)
2110 dev->format = &format[0]; 1873 dev->format = &format[0];
2111 1874
2112 maxw = norm_maxw(dev); 1875 maxw = norm_maxw(dev);
2113 /* MaxPacketSize for em2800 is too small to capture at full resolution 1876 /* MaxPacketSize for em2800 is too small to capture at full resolution
2114 * use half of maxw as the scaler can only scale to 50% */ 1877 * use half of maxw as the scaler can only scale to 50% */
2115 if (dev->board.is_em2800) 1878 if (dev->board.is_em2800)
2116 maxw /= 2; 1879 maxw /= 2;
2117 1880
2118 em28xx_set_video_format(dev, format[0].fourcc, 1881 em28xx_set_video_format(dev, format[0].fourcc,
2119 maxw, norm_maxh(dev)); 1882 maxw, norm_maxh(dev));
@@ -2139,6 +1902,8 @@ int em28xx_register_analog_devices(struct em28xx *dev)
2139 em28xx_errdev("cannot allocate video_device.\n"); 1902 em28xx_errdev("cannot allocate video_device.\n");
2140 return -ENODEV; 1903 return -ENODEV;
2141 } 1904 }
1905 dev->vdev->queue = &dev->vb_vidq;
1906 dev->vdev->queue->lock = &dev->vb_queue_lock;
2142 1907
2143 /* register v4l2 video video_device */ 1908 /* register v4l2 video video_device */
2144 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER, 1909 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
@@ -2154,6 +1919,9 @@ int em28xx_register_analog_devices(struct em28xx *dev)
2154 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, 1919 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2155 "vbi"); 1920 "vbi");
2156 1921
1922 dev->vbi_dev->queue = &dev->vb_vbiq;
1923 dev->vbi_dev->queue->lock = &dev->vb_vbi_queue_lock;
1924
2157 /* register v4l2 vbi video_device */ 1925 /* register v4l2 vbi video_device */
2158 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, 1926 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2159 vbi_nr[dev->devno]); 1927 vbi_nr[dev->devno]);
diff --git a/drivers/media/usb/em28xx/em28xx.h b/drivers/media/usb/em28xx/em28xx.h
index 7432be483c22..bf0a790f7eb9 100644
--- a/drivers/media/usb/em28xx/em28xx.h
+++ b/drivers/media/usb/em28xx/em28xx.h
@@ -31,15 +31,12 @@
31#include <linux/mutex.h> 31#include <linux/mutex.h>
32#include <linux/videodev2.h> 32#include <linux/videodev2.h>
33 33
34#include <media/videobuf-vmalloc.h> 34#include <media/videobuf2-vmalloc.h>
35#include <media/v4l2-device.h> 35#include <media/v4l2-device.h>
36#include <media/v4l2-ctrls.h> 36#include <media/v4l2-ctrls.h>
37#include <media/v4l2-fh.h> 37#include <media/v4l2-fh.h>
38#include <media/ir-kbd-i2c.h> 38#include <media/ir-kbd-i2c.h>
39#include <media/rc-core.h> 39#include <media/rc-core.h>
40#if defined(CONFIG_VIDEO_EM28XX_DVB) || defined(CONFIG_VIDEO_EM28XX_DVB_MODULE)
41#include <media/videobuf-dvb.h>
42#endif
43#include "tuner-xc2028.h" 40#include "tuner-xc2028.h"
44#include "xc5000.h" 41#include "xc5000.h"
45#include "em28xx-reg.h" 42#include "em28xx-reg.h"
@@ -252,8 +249,11 @@ struct em28xx_fmt {
252/* buffer for one video frame */ 249/* buffer for one video frame */
253struct em28xx_buffer { 250struct em28xx_buffer {
254 /* common v4l buffer stuff -- must be first */ 251 /* common v4l buffer stuff -- must be first */
255 struct videobuf_buffer vb; 252 struct vb2_buffer vb;
253 struct list_head list;
256 254
255 void *mem;
256 unsigned int length;
257 int top_field; 257 int top_field;
258 258
259 /* counter to control buffer fill */ 259 /* counter to control buffer fill */
@@ -480,11 +480,6 @@ struct em28xx;
480struct em28xx_fh { 480struct em28xx_fh {
481 struct v4l2_fh fh; 481 struct v4l2_fh fh;
482 struct em28xx *dev; 482 struct em28xx *dev;
483 int radio;
484 unsigned int resources;
485
486 struct videobuf_queue vb_vidq;
487 struct videobuf_queue vb_vbiq;
488 483
489 enum v4l2_buf_type type; 484 enum v4l2_buf_type type;
490}; 485};
@@ -545,6 +540,7 @@ struct em28xx {
545 struct i2c_client i2c_client; 540 struct i2c_client i2c_client;
546 /* video for linux */ 541 /* video for linux */
547 int users; /* user count for exclusive use */ 542 int users; /* user count for exclusive use */
543 int streaming_users; /* Number of actively streaming users */
548 struct video_device *vdev; /* video for linux device struct */ 544 struct video_device *vdev; /* video for linux device struct */
549 v4l2_std_id norm; /* selected tv norm */ 545 v4l2_std_id norm; /* selected tv norm */
550 int ctl_freq; /* selected frequency */ 546 int ctl_freq; /* selected frequency */
@@ -587,6 +583,12 @@ struct em28xx {
587 struct video_device *vbi_dev; 583 struct video_device *vbi_dev;
588 struct video_device *radio_dev; 584 struct video_device *radio_dev;
589 585
586 /* Videobuf2 */
587 struct vb2_queue vb_vidq;
588 struct vb2_queue vb_vbiq;
589 struct mutex vb_queue_lock;
590 struct mutex vb_vbi_queue_lock;
591
590 /* resources in use */ 592 /* resources in use */
591 unsigned int resources; 593 unsigned int resources;
592 594
@@ -598,6 +600,9 @@ struct em28xx {
598 struct em28xx_usb_ctl usb_ctl; 600 struct em28xx_usb_ctl usb_ctl;
599 spinlock_t slock; 601 spinlock_t slock;
600 602
603 unsigned int field_count;
604 unsigned int vbi_field_count;
605
601 /* usb transfer */ 606 /* usb transfer */
602 struct usb_device *udev; /* the usb device */ 607 struct usb_device *udev; /* the usb device */
603 u8 analog_ep_isoc; /* address of isoc endpoint for analog */ 608 u8 analog_ep_isoc; /* address of isoc endpoint for analog */
@@ -709,9 +714,12 @@ void em28xx_init_extension(struct em28xx *dev);
709void em28xx_close_extension(struct em28xx *dev); 714void em28xx_close_extension(struct em28xx *dev);
710 715
711/* Provided by em28xx-video.c */ 716/* Provided by em28xx-video.c */
717int em28xx_vb2_setup(struct em28xx *dev);
712int em28xx_register_analog_devices(struct em28xx *dev); 718int em28xx_register_analog_devices(struct em28xx *dev);
713void em28xx_release_analog_resources(struct em28xx *dev); 719void em28xx_release_analog_resources(struct em28xx *dev);
714void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv); 720void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv);
721int em28xx_start_analog_streaming(struct vb2_queue *vq, unsigned int count);
722int em28xx_stop_vbi_streaming(struct vb2_queue *vq);
715extern const struct v4l2_ctrl_ops em28xx_ctrl_ops; 723extern const struct v4l2_ctrl_ops em28xx_ctrl_ops;
716 724
717/* Provided by em28xx-cards.c */ 725/* Provided by em28xx-cards.c */
@@ -723,7 +731,7 @@ int em28xx_tuner_callback(void *ptr, int component, int command, int arg);
723void em28xx_release_resources(struct em28xx *dev); 731void em28xx_release_resources(struct em28xx *dev);
724 732
725/* Provided by em28xx-vbi.c */ 733/* Provided by em28xx-vbi.c */
726extern struct videobuf_queue_ops em28xx_vbi_qops; 734extern struct vb2_ops em28xx_vbi_qops;
727 735
728/* printk macros */ 736/* printk macros */
729 737