aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/usb/em28xx/em28xx-video.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/usb/em28xx/em28xx-video.c')
-rw-r--r--drivers/media/usb/em28xx/em28xx-video.c1689
1 files changed, 509 insertions, 1180 deletions
diff --git a/drivers/media/usb/em28xx/em28xx-video.c b/drivers/media/usb/em28xx/em28xx-video.c
index 1e553d357380..32bd7de5dec1 100644
--- a/drivers/media/usb/em28xx/em28xx-video.c
+++ b/drivers/media/usb/em28xx/em28xx-video.c
@@ -6,6 +6,7 @@
6 Markus Rechberger <mrechberger@gmail.com> 6 Markus Rechberger <mrechberger@gmail.com>
7 Mauro Carvalho Chehab <mchehab@infradead.org> 7 Mauro Carvalho Chehab <mchehab@infradead.org>
8 Sascha Sommer <saschasommer@freenet.de> 8 Sascha Sommer <saschasommer@freenet.de>
9 Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
9 10
10 Some parts based on SN9C10x PC Camera Controllers GPL driver made 11 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it> 12 by Luca Risolia <luca.risolia@studio.unibo.it>
@@ -39,6 +40,7 @@
39#include "em28xx.h" 40#include "em28xx.h"
40#include <media/v4l2-common.h> 41#include <media/v4l2-common.h>
41#include <media/v4l2-ioctl.h> 42#include <media/v4l2-ioctl.h>
43#include <media/v4l2-event.h>
42#include <media/v4l2-chip-ident.h> 44#include <media/v4l2-chip-ident.h>
43#include <media/msp3400.h> 45#include <media/msp3400.h>
44#include <media/tuner.h> 46#include <media/tuner.h>
@@ -74,9 +76,9 @@ MODULE_DESCRIPTION(DRIVER_DESC);
74MODULE_LICENSE("GPL"); 76MODULE_LICENSE("GPL");
75MODULE_VERSION(EM28XX_VERSION); 77MODULE_VERSION(EM28XX_VERSION);
76 78
77static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; 79static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
78static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; 80static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
79static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; 81static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
80 82
81module_param_array(video_nr, int, NULL, 0444); 83module_param_array(video_nr, int, NULL, 0444);
82module_param_array(vbi_nr, int, NULL, 0444); 84module_param_array(vbi_nr, int, NULL, 0444);
@@ -124,101 +126,50 @@ static struct em28xx_fmt format[] = {
124 }, 126 },
125}; 127};
126 128
127/* supported controls */
128/* Common to all boards */
129static struct v4l2_queryctrl ac97_qctrl[] = {
130 {
131 .id = V4L2_CID_AUDIO_VOLUME,
132 .type = V4L2_CTRL_TYPE_INTEGER,
133 .name = "Volume",
134 .minimum = 0x0,
135 .maximum = 0x1f,
136 .step = 0x1,
137 .default_value = 0x1f,
138 .flags = V4L2_CTRL_FLAG_SLIDER,
139 }, {
140 .id = V4L2_CID_AUDIO_MUTE,
141 .type = V4L2_CTRL_TYPE_BOOLEAN,
142 .name = "Mute",
143 .minimum = 0,
144 .maximum = 1,
145 .step = 1,
146 .default_value = 1,
147 .flags = 0,
148 }
149};
150
151/* ------------------------------------------------------------------ 129/* ------------------------------------------------------------------
152 DMA and thread functions 130 DMA and thread functions
153 ------------------------------------------------------------------*/ 131 ------------------------------------------------------------------*/
154 132
155/* 133/*
156 * Announces that a buffer were filled and request the next 134 * Finish the current buffer
157 */ 135 */
158static inline void buffer_filled(struct em28xx *dev, 136static inline void finish_buffer(struct em28xx *dev,
159 struct em28xx_dmaqueue *dma_q, 137 struct em28xx_buffer *buf)
160 struct em28xx_buffer *buf)
161{ 138{
162 /* Advice that buffer was filled */ 139 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
163 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
164 buf->vb.state = VIDEOBUF_DONE;
165 buf->vb.field_count++;
166 do_gettimeofday(&buf->vb.ts);
167 140
168 dev->isoc_ctl.vid_buf = NULL; 141 buf->vb.v4l2_buf.sequence = dev->field_count++;
142 buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED;
143 v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
169 144
170 list_del(&buf->vb.queue); 145 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
171 wake_up(&buf->vb.done);
172}
173
174static inline void vbi_buffer_filled(struct em28xx *dev,
175 struct em28xx_dmaqueue *dma_q,
176 struct em28xx_buffer *buf)
177{
178 /* Advice that buffer was filled */
179 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
180
181 buf->vb.state = VIDEOBUF_DONE;
182 buf->vb.field_count++;
183 do_gettimeofday(&buf->vb.ts);
184
185 dev->isoc_ctl.vbi_buf = NULL;
186
187 list_del(&buf->vb.queue);
188 wake_up(&buf->vb.done);
189} 146}
190 147
191/* 148/*
192 * Identify the buffer header type and properly handles 149 * Copy picture data from USB buffer to videobuf buffer
193 */ 150 */
194static void em28xx_copy_video(struct em28xx *dev, 151static void em28xx_copy_video(struct em28xx *dev,
195 struct em28xx_dmaqueue *dma_q,
196 struct em28xx_buffer *buf, 152 struct em28xx_buffer *buf,
197 unsigned char *p, 153 unsigned char *usb_buf,
198 unsigned char *outp, unsigned long len) 154 unsigned long len)
199{ 155{
200 void *fieldstart, *startwrite, *startread; 156 void *fieldstart, *startwrite, *startread;
201 int linesdone, currlinedone, offset, lencopy, remain; 157 int linesdone, currlinedone, offset, lencopy, remain;
202 int bytesperline = dev->width << 1; 158 int bytesperline = dev->width << 1;
203 159
204 if (dma_q->pos + len > buf->vb.size) 160 if (buf->pos + len > buf->length)
205 len = buf->vb.size - dma_q->pos; 161 len = buf->length - buf->pos;
206 162
207 startread = p; 163 startread = usb_buf;
208 remain = len; 164 remain = len;
209 165
210 if (dev->progressive) 166 if (dev->progressive || buf->top_field)
211 fieldstart = outp; 167 fieldstart = buf->vb_buf;
212 else { 168 else /* interlaced mode, even nr. of lines */
213 /* Interlaces two half frames */ 169 fieldstart = buf->vb_buf + bytesperline;
214 if (buf->top_field)
215 fieldstart = outp;
216 else
217 fieldstart = outp + bytesperline;
218 }
219 170
220 linesdone = dma_q->pos / bytesperline; 171 linesdone = buf->pos / bytesperline;
221 currlinedone = dma_q->pos % bytesperline; 172 currlinedone = buf->pos % bytesperline;
222 173
223 if (dev->progressive) 174 if (dev->progressive)
224 offset = linesdone * bytesperline + currlinedone; 175 offset = linesdone * bytesperline + currlinedone;
@@ -229,11 +180,12 @@ static void em28xx_copy_video(struct em28xx *dev,
229 lencopy = bytesperline - currlinedone; 180 lencopy = bytesperline - currlinedone;
230 lencopy = lencopy > remain ? remain : lencopy; 181 lencopy = lencopy > remain ? remain : lencopy;
231 182
232 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) { 183 if ((char *)startwrite + lencopy > (char *)buf->vb_buf + buf->length) {
233 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n", 184 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
234 ((char *)startwrite + lencopy) - 185 ((char *)startwrite + lencopy) -
235 ((char *)outp + buf->vb.size)); 186 ((char *)buf->vb_buf + buf->length));
236 remain = (char *)outp + buf->vb.size - (char *)startwrite; 187 remain = (char *)buf->vb_buf + buf->length -
188 (char *)startwrite;
237 lencopy = remain; 189 lencopy = remain;
238 } 190 }
239 if (lencopy <= 0) 191 if (lencopy <= 0)
@@ -243,21 +195,24 @@ static void em28xx_copy_video(struct em28xx *dev,
243 remain -= lencopy; 195 remain -= lencopy;
244 196
245 while (remain > 0) { 197 while (remain > 0) {
246 startwrite += lencopy + bytesperline; 198 if (dev->progressive)
199 startwrite += lencopy;
200 else
201 startwrite += lencopy + bytesperline;
247 startread += lencopy; 202 startread += lencopy;
248 if (bytesperline > remain) 203 if (bytesperline > remain)
249 lencopy = remain; 204 lencopy = remain;
250 else 205 else
251 lencopy = bytesperline; 206 lencopy = bytesperline;
252 207
253 if ((char *)startwrite + lencopy > (char *)outp + 208 if ((char *)startwrite + lencopy > (char *)buf->vb_buf +
254 buf->vb.size) { 209 buf->length) {
255 em28xx_isocdbg("Overflow of %zi bytes past buffer end" 210 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
256 "(2)\n", 211 "(2)\n",
257 ((char *)startwrite + lencopy) - 212 ((char *)startwrite + lencopy) -
258 ((char *)outp + buf->vb.size)); 213 ((char *)buf->vb_buf + buf->length));
259 lencopy = remain = (char *)outp + buf->vb.size - 214 lencopy = remain = (char *)buf->vb_buf + buf->length -
260 (char *)startwrite; 215 (char *)startwrite;
261 } 216 }
262 if (lencopy <= 0) 217 if (lencopy <= 0)
263 break; 218 break;
@@ -267,57 +222,29 @@ static void em28xx_copy_video(struct em28xx *dev,
267 remain -= lencopy; 222 remain -= lencopy;
268 } 223 }
269 224
270 dma_q->pos += len; 225 buf->pos += len;
271} 226}
272 227
228/*
229 * Copy VBI data from USB buffer to videobuf buffer
230 */
273static void em28xx_copy_vbi(struct em28xx *dev, 231static void em28xx_copy_vbi(struct em28xx *dev,
274 struct em28xx_dmaqueue *dma_q, 232 struct em28xx_buffer *buf,
275 struct em28xx_buffer *buf, 233 unsigned char *usb_buf,
276 unsigned char *p, 234 unsigned long len)
277 unsigned char *outp, unsigned long len)
278{ 235{
279 void *startwrite, *startread; 236 unsigned int offset;
280 int offset;
281 int bytesperline;
282
283 if (dev == NULL) {
284 em28xx_isocdbg("dev is null\n");
285 return;
286 }
287 bytesperline = dev->vbi_width;
288 237
289 if (dma_q == NULL) { 238 if (buf->pos + len > buf->length)
290 em28xx_isocdbg("dma_q is null\n"); 239 len = buf->length - buf->pos;
291 return;
292 }
293 if (buf == NULL) {
294 return;
295 }
296 if (p == NULL) {
297 em28xx_isocdbg("p is null\n");
298 return;
299 }
300 if (outp == NULL) {
301 em28xx_isocdbg("outp is null\n");
302 return;
303 }
304
305 if (dma_q->pos + len > buf->vb.size)
306 len = buf->vb.size - dma_q->pos;
307
308 startread = p;
309
310 startwrite = outp + dma_q->pos;
311 offset = dma_q->pos;
312 240
241 offset = buf->pos;
313 /* 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 */
314 if (buf->top_field == 0) { 243 if (buf->top_field == 0)
315 startwrite += bytesperline * dev->vbi_height; 244 offset += dev->vbi_width * dev->vbi_height;
316 offset += bytesperline * dev->vbi_height;
317 }
318 245
319 memcpy(startwrite, startread, len); 246 memcpy(buf->vb_buf + offset, usb_buf, len);
320 dma_q->pos += len; 247 buf->pos += len;
321} 248}
322 249
323static inline void print_err_status(struct em28xx *dev, 250static inline void print_err_status(struct em28xx *dev,
@@ -360,470 +287,444 @@ static inline void print_err_status(struct em28xx *dev,
360} 287}
361 288
362/* 289/*
363 * video-buf generic routine to get the next available buffer 290 * get the next available buffer from dma queue
364 */ 291 */
365static inline void get_next_buf(struct em28xx_dmaqueue *dma_q, 292static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
366 struct em28xx_buffer **buf) 293 struct em28xx_dmaqueue *dma_q)
367{ 294{
368 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); 295 struct em28xx_buffer *buf;
369 char *outp;
370 296
371 if (list_empty(&dma_q->active)) { 297 if (list_empty(&dma_q->active)) {
372 em28xx_isocdbg("No active queue to serve\n"); 298 em28xx_isocdbg("No active queue to serve\n");
373 dev->isoc_ctl.vid_buf = NULL; 299 return NULL;
374 *buf = NULL;
375 return;
376 } 300 }
377 301
378 /* Get the next buffer */ 302 /* Get the next buffer */
379 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue); 303 buf = list_entry(dma_q->active.next, struct em28xx_buffer, list);
380
381 /* Cleans up buffer - Useful for testing for frame/URB loss */ 304 /* Cleans up buffer - Useful for testing for frame/URB loss */
382 outp = videobuf_to_vmalloc(&(*buf)->vb); 305 list_del(&buf->list);
383 memset(outp, 0, (*buf)->vb.size); 306 buf->pos = 0;
307 buf->vb_buf = buf->mem;
384 308
385 dev->isoc_ctl.vid_buf = *buf; 309 return buf;
386
387 return;
388} 310}
389 311
390/* 312/*
391 * video-buf generic routine to get the next available VBI buffer 313 * Finish the current buffer if completed and prepare for the next field
392 */ 314 */
393static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q, 315static struct em28xx_buffer *
394 struct em28xx_buffer **buf) 316finish_field_prepare_next(struct em28xx *dev,
395{ 317 struct em28xx_buffer *buf,
396 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq); 318 struct em28xx_dmaqueue *dma_q)
397 char *outp; 319{
398 320 if (dev->progressive || dev->top_field) { /* Brand new frame */
399 if (list_empty(&dma_q->active)) { 321 if (buf != NULL)
400 em28xx_isocdbg("No active queue to serve\n"); 322 finish_buffer(dev, buf);
401 dev->isoc_ctl.vbi_buf = NULL; 323 buf = get_next_buf(dev, dma_q);
402 *buf = NULL; 324 }
403 return; 325 if (buf != NULL) {
326 buf->top_field = dev->top_field;
327 buf->pos = 0;
404 } 328 }
405 329
406 /* Get the next buffer */ 330 return buf;
407 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
408 /* Cleans up buffer - Useful for testing for frame/URB loss */
409 outp = videobuf_to_vmalloc(&(*buf)->vb);
410 memset(outp, 0x00, (*buf)->vb.size);
411
412 dev->isoc_ctl.vbi_buf = *buf;
413
414 return;
415} 331}
416 332
417/* 333/*
418 * Controls the isoc copy of each urb packet 334 * Process data packet according to the em2710/em2750/em28xx frame data format
419 */ 335 */
420static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb) 336static inline void process_frame_data_em28xx(struct em28xx *dev,
337 unsigned char *data_pkt,
338 unsigned int data_len)
421{ 339{
422 struct em28xx_buffer *buf; 340 struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
341 struct em28xx_buffer *vbi_buf = dev->usb_ctl.vbi_buf;
423 struct em28xx_dmaqueue *dma_q = &dev->vidq; 342 struct em28xx_dmaqueue *dma_q = &dev->vidq;
424 unsigned char *outp = NULL; 343 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
425 int i, len = 0, rc = 1;
426 unsigned char *p;
427
428 if (!dev)
429 return 0;
430
431 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
432 return 0;
433
434 if (urb->status < 0) {
435 print_err_status(dev, -1, urb->status);
436 if (urb->status == -ENOENT)
437 return 0;
438 }
439
440 buf = dev->isoc_ctl.vid_buf;
441 if (buf != NULL)
442 outp = videobuf_to_vmalloc(&buf->vb);
443
444 for (i = 0; i < urb->number_of_packets; i++) {
445 int status = urb->iso_frame_desc[i].status;
446 344
447 if (status < 0) { 345 /* capture type 0 = vbi start
448 print_err_status(dev, i, status); 346 capture type 1 = vbi in progress
449 if (urb->iso_frame_desc[i].status != -EPROTO) 347 capture type 2 = video start
450 continue; 348 capture type 3 = video in progress */
349 if (data_len >= 4) {
350 /* NOTE: Headers are always 4 bytes and
351 * never split across packets */
352 if (data_pkt[0] == 0x88 && data_pkt[1] == 0x88 &&
353 data_pkt[2] == 0x88 && data_pkt[3] == 0x88) {
354 /* Continuation */
355 data_pkt += 4;
356 data_len -= 4;
357 } else if (data_pkt[0] == 0x33 && data_pkt[1] == 0x95) {
358 /* Field start (VBI mode) */
359 dev->capture_type = 0;
360 dev->vbi_read = 0;
361 em28xx_isocdbg("VBI START HEADER !!!\n");
362 dev->top_field = !(data_pkt[2] & 1);
363 data_pkt += 4;
364 data_len -= 4;
365 } else if (data_pkt[0] == 0x22 && data_pkt[1] == 0x5a) {
366 /* Field start (VBI disabled) */
367 dev->capture_type = 2;
368 em28xx_isocdbg("VIDEO START HEADER !!!\n");
369 dev->top_field = !(data_pkt[2] & 1);
370 data_pkt += 4;
371 data_len -= 4;
451 } 372 }
373 }
374 /* NOTE: With bulk transfers, intermediate data packets
375 * have no continuation header */
452 376
453 len = urb->iso_frame_desc[i].actual_length - 4; 377 if (dev->capture_type == 0) {
378 vbi_buf = finish_field_prepare_next(dev, vbi_buf, vbi_dma_q);
379 dev->usb_ctl.vbi_buf = vbi_buf;
380 dev->capture_type = 1;
381 }
454 382
455 if (urb->iso_frame_desc[i].actual_length <= 0) { 383 if (dev->capture_type == 1) {
456 /* em28xx_isocdbg("packet %d is empty",i); - spammy */ 384 int vbi_size = dev->vbi_width * dev->vbi_height;
457 continue; 385 int vbi_data_len = ((dev->vbi_read + data_len) > vbi_size) ?
458 } 386 (vbi_size - dev->vbi_read) : data_len;
459 if (urb->iso_frame_desc[i].actual_length >
460 dev->max_pkt_size) {
461 em28xx_isocdbg("packet bigger than packet size");
462 continue;
463 }
464 387
465 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset; 388 /* Copy VBI data */
389 if (vbi_buf != NULL)
390 em28xx_copy_vbi(dev, vbi_buf, data_pkt, vbi_data_len);
391 dev->vbi_read += vbi_data_len;
466 392
467 /* FIXME: incomplete buffer checks where removed to make 393 if (vbi_data_len < data_len) {
468 logic simpler. Impacts of those changes should be evaluated 394 /* Continue with copying video data */
469 */ 395 dev->capture_type = 2;
470 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) { 396 data_pkt += vbi_data_len;
471 em28xx_isocdbg("VBI HEADER!!!\n"); 397 data_len -= vbi_data_len;
472 /* FIXME: Should add vbi copy */
473 continue;
474 } 398 }
475 if (p[0] == 0x22 && p[1] == 0x5a) { 399 }
476 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
477 len, (p[2] & 1) ? "odd" : "even");
478
479 if (dev->progressive || !(p[2] & 1)) {
480 if (buf != NULL)
481 buffer_filled(dev, dma_q, buf);
482 get_next_buf(dma_q, &buf);
483 if (buf == NULL)
484 outp = NULL;
485 else
486 outp = videobuf_to_vmalloc(&buf->vb);
487 }
488
489 if (buf != NULL) {
490 if (p[2] & 1)
491 buf->top_field = 0;
492 else
493 buf->top_field = 1;
494 }
495 400
496 dma_q->pos = 0; 401 if (dev->capture_type == 2) {
497 } 402 buf = finish_field_prepare_next(dev, buf, dma_q);
498 if (buf != NULL) { 403 dev->usb_ctl.vid_buf = buf;
499 if (p[0] != 0x88 && p[0] != 0x22) { 404 dev->capture_type = 3;
500 em28xx_isocdbg("frame is not complete\n");
501 len += 4;
502 } else {
503 p += 4;
504 }
505 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
506 }
507 } 405 }
508 return rc; 406
407 if (dev->capture_type == 3 && buf != NULL && data_len > 0)
408 em28xx_copy_video(dev, buf, data_pkt, data_len);
509} 409}
510 410
511/* Version of isoc handler that takes into account a mixture of video and 411/* Processes and copies the URB data content (video and VBI data) */
512 VBI data */ 412static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
513static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
514{ 413{
515 struct em28xx_buffer *buf, *vbi_buf; 414 int xfer_bulk, num_packets, i;
516 struct em28xx_dmaqueue *dma_q = &dev->vidq; 415 unsigned char *usb_data_pkt;
517 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq; 416 unsigned int usb_data_len;
518 unsigned char *outp = NULL;
519 unsigned char *vbioutp = NULL;
520 int i, len = 0, rc = 1;
521 unsigned char *p;
522 int vbi_size;
523 417
524 if (!dev) 418 if (!dev)
525 return 0; 419 return 0;
526 420
527 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED)) 421 if (dev->disconnected)
528 return 0; 422 return 0;
529 423
530 if (urb->status < 0) { 424 if (urb->status < 0)
531 print_err_status(dev, -1, urb->status); 425 print_err_status(dev, -1, urb->status);
532 if (urb->status == -ENOENT)
533 return 0;
534 }
535 426
536 buf = dev->isoc_ctl.vid_buf; 427 xfer_bulk = usb_pipebulk(urb->pipe);
537 if (buf != NULL)
538 outp = videobuf_to_vmalloc(&buf->vb);
539 428
540 vbi_buf = dev->isoc_ctl.vbi_buf; 429 if (xfer_bulk) /* bulk */
541 if (vbi_buf != NULL) 430 num_packets = 1;
542 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb); 431 else /* isoc */
432 num_packets = urb->number_of_packets;
543 433
544 for (i = 0; i < urb->number_of_packets; i++) { 434 for (i = 0; i < num_packets; i++) {
545 int status = urb->iso_frame_desc[i].status; 435 if (xfer_bulk) { /* bulk */
436 usb_data_len = urb->actual_length;
546 437
547 if (status < 0) { 438 usb_data_pkt = urb->transfer_buffer;
548 print_err_status(dev, i, status); 439 } else { /* isoc */
549 if (urb->iso_frame_desc[i].status != -EPROTO) 440 if (urb->iso_frame_desc[i].status < 0) {
441 print_err_status(dev, i,
442 urb->iso_frame_desc[i].status);
443 if (urb->iso_frame_desc[i].status != -EPROTO)
444 continue;
445 }
446
447 usb_data_len = urb->iso_frame_desc[i].actual_length;
448 if (usb_data_len > dev->max_pkt_size) {
449 em28xx_isocdbg("packet bigger than packet size");
550 continue; 450 continue;
551 } 451 }
552 452
553 len = urb->iso_frame_desc[i].actual_length; 453 usb_data_pkt = urb->transfer_buffer +
554 if (urb->iso_frame_desc[i].actual_length <= 0) { 454 urb->iso_frame_desc[i].offset;
555 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
556 continue;
557 }
558 if (urb->iso_frame_desc[i].actual_length >
559 dev->max_pkt_size) {
560 em28xx_isocdbg("packet bigger than packet size");
561 continue;
562 } 455 }
563 456
564 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset; 457 if (usb_data_len == 0) {
565 458 /* NOTE: happens very often with isoc transfers */
566 /* capture type 0 = vbi start 459 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
567 capture type 1 = video start 460 continue;
568 capture type 2 = video in progress */
569 if (p[0] == 0x33 && p[1] == 0x95) {
570 dev->capture_type = 0;
571 dev->vbi_read = 0;
572 em28xx_isocdbg("VBI START HEADER!!!\n");
573 dev->cur_field = p[2];
574 p += 4;
575 len -= 4;
576 } else if (p[0] == 0x88 && p[1] == 0x88 &&
577 p[2] == 0x88 && p[3] == 0x88) {
578 /* continuation */
579 p += 4;
580 len -= 4;
581 } else if (p[0] == 0x22 && p[1] == 0x5a) {
582 /* start video */
583 p += 4;
584 len -= 4;
585 } 461 }
586 462
587 vbi_size = dev->vbi_width * dev->vbi_height; 463 process_frame_data_em28xx(dev, usb_data_pkt, usb_data_len);
588 464 }
589 if (dev->capture_type == 0) { 465 return 1;
590 if (dev->vbi_read >= vbi_size) { 466}
591 /* We've already read all the VBI data, so
592 treat the rest as video */
593 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
594 } else if ((dev->vbi_read + len) < vbi_size) {
595 /* This entire frame is VBI data */
596 if (dev->vbi_read == 0 &&
597 (!(dev->cur_field & 1))) {
598 /* Brand new frame */
599 if (vbi_buf != NULL)
600 vbi_buffer_filled(dev,
601 vbi_dma_q,
602 vbi_buf);
603 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
604 if (vbi_buf == NULL)
605 vbioutp = NULL;
606 else
607 vbioutp = videobuf_to_vmalloc(
608 &vbi_buf->vb);
609 }
610
611 if (dev->vbi_read == 0) {
612 vbi_dma_q->pos = 0;
613 if (vbi_buf != NULL) {
614 if (dev->cur_field & 1)
615 vbi_buf->top_field = 0;
616 else
617 vbi_buf->top_field = 1;
618 }
619 }
620
621 dev->vbi_read += len;
622 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
623 vbioutp, len);
624 } else {
625 /* Some of this frame is VBI data and some is
626 video data */
627 int vbi_data_len = vbi_size - dev->vbi_read;
628 dev->vbi_read += vbi_data_len;
629 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
630 vbioutp, vbi_data_len);
631 dev->capture_type = 1;
632 p += vbi_data_len;
633 len -= vbi_data_len;
634 }
635 }
636 467
637 if (dev->capture_type == 1) {
638 dev->capture_type = 2;
639 if (dev->progressive || !(dev->cur_field & 1)) {
640 if (buf != NULL)
641 buffer_filled(dev, dma_q, buf);
642 get_next_buf(dma_q, &buf);
643 if (buf == NULL)
644 outp = NULL;
645 else
646 outp = videobuf_to_vmalloc(&buf->vb);
647 }
648 if (buf != NULL) {
649 if (dev->cur_field & 1)
650 buf->top_field = 0;
651 else
652 buf->top_field = 1;
653 }
654 468
655 dma_q->pos = 0; 469static int get_ressource(enum v4l2_buf_type f_type)
656 } 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}
657 481
658 if (buf != NULL && dev->capture_type == 2) { 482/* Usage lock check functions */
659 if (len >= 4 && p[0] == 0x88 && p[1] == 0x88 && 483static int res_get(struct em28xx *dev, enum v4l2_buf_type f_type)
660 p[2] == 0x88 && p[3] == 0x88) { 484{
661 p += 4; 485 int res_type = get_ressource(f_type);
662 len -= 4;
663 }
664 if (len >= 4 && p[0] == 0x22 && p[1] == 0x5a) {
665 em28xx_isocdbg("Video frame %d, len=%i, %s\n",
666 p[2], len, (p[2] & 1) ?
667 "odd" : "even");
668 p += 4;
669 len -= 4;
670 }
671 486
672 if (len > 0) 487 /* is it free? */
673 em28xx_copy_video(dev, dma_q, buf, p, outp, 488 if (dev->resources & res_type) {
674 len); 489 /* no, someone else uses it */
675 } 490 return -EBUSY;
676 } 491 }
677 return rc; 492
493 /* it's free, grab it */
494 dev->resources |= res_type;
495 em28xx_videodbg("res: get %d\n", res_type);
496 return 0;
678} 497}
679 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}
680 506
681/* ------------------------------------------------------------------ 507/* ------------------------------------------------------------------
682 Videobuf operations 508 Videobuf2 operations
683 ------------------------------------------------------------------*/ 509 ------------------------------------------------------------------*/
684 510
685static int 511static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
686buffer_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[])
687{ 514{
688 struct em28xx_fh *fh = vq->priv_data; 515 struct em28xx *dev = vb2_get_drv_priv(vq);
689 struct em28xx *dev = fh->dev; 516 unsigned long size;
690 struct v4l2_frequency f;
691 517
692 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) 518 if (fmt)
693 >> 3; 519 size = fmt->fmt.pix.sizeimage;
694 520 else
695 if (0 == *count) 521 size = (dev->width * dev->height * dev->format->depth + 7) >> 3;
696 *count = EM28XX_DEF_BUF;
697 522
698 if (*count < EM28XX_MIN_BUF) 523 if (size == 0)
699 *count = EM28XX_MIN_BUF; 524 return -EINVAL;
700 525
701 /* Ask tuner to go to analog or radio mode */ 526 if (0 == *nbuffers)
702 memset(&f, 0, sizeof(f)); 527 *nbuffers = 32;
703 f.frequency = dev->ctl_freq;
704 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
705 528
706 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f); 529 *nplanes = 1;
530 sizes[0] = size;
707 531
708 return 0; 532 return 0;
709} 533}
710 534
711/* This is called *without* dev->slock held; please keep it that way */ 535static int
712static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf) 536buffer_prepare(struct vb2_buffer *vb)
713{ 537{
714 struct em28xx_fh *fh = vq->priv_data; 538 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
715 struct em28xx *dev = fh->dev; 539 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
716 unsigned long flags = 0; 540 unsigned long size;
717 if (in_interrupt())
718 BUG();
719 541
720 /* 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);
721 because, as we were keeping the state as VIDEOBUF_QUEUED,
722 videobuf_queue_cancel marked it as finished for us.
723 (Also, it could wedge forever if the hardware was misconfigured.)
724 543
725 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;
726 queued anymore. If we ever start marking the buffers as
727 VIDEOBUF_ACTIVE, it won't be, though.
728 */
729 spin_lock_irqsave(&dev->slock, flags);
730 if (dev->isoc_ctl.vid_buf == buf)
731 dev->isoc_ctl.vid_buf = NULL;
732 spin_unlock_irqrestore(&dev->slock, flags);
733 545
734 videobuf_vmalloc_free(&buf->vb); 546 if (vb2_plane_size(vb, 0) < size) {
735 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;
736} 554}
737 555
738static int 556int em28xx_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
739buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
740 enum v4l2_field field)
741{ 557{
742 struct em28xx_fh *fh = vq->priv_data; 558 struct em28xx *dev = vb2_get_drv_priv(vq);
743 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); 559 struct v4l2_frequency f;
744 struct em28xx *dev = fh->dev; 560 int rc = 0;
745 int rc = 0, urb_init = 0;
746 561
747 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth 562 em28xx_videodbg("%s\n", __func__);
748 + 7) >> 3;
749 563
750 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) 564 /* Make sure streaming is not already in progress for this type
751 return -EINVAL; 565 of filehandle (e.g. video, vbi) */
566 rc = res_get(dev, vq->type);
567 if (rc)
568 return rc;
569
570 if (dev->streaming_users++ == 0) {
571 /* First active streaming user, so allocate all the URBs */
752 572
753 buf->vb.width = dev->width; 573 /* Allocate the USB bandwidth */
754 buf->vb.height = dev->height; 574 em28xx_set_alternate(dev);
755 buf->vb.field = field; 575
576 /* Needed, since GPIO might have disabled power of
577 some i2c device
578 */
579 em28xx_wake_i2c(dev);
756 580
757 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { 581 dev->capture_type = -1;
758 rc = videobuf_iolock(vq, &buf->vb, NULL); 582 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
583 dev->analog_xfer_bulk,
584 EM28XX_NUM_BUFS,
585 dev->max_pkt_size,
586 dev->packet_multiplier,
587 em28xx_urb_data_copy);
759 if (rc < 0) 588 if (rc < 0)
760 goto fail; 589 goto fail;
761 }
762 590
763 if (!dev->isoc_ctl.analog_bufs.num_bufs) 591 /*
764 urb_init = 1; 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 */
765 596
766 if (urb_init) { 597 /* Ask tuner to go to analog or radio mode */
767 if (em28xx_vbi_supported(dev) == 1) 598 memset(&f, 0, sizeof(f));
768 rc = em28xx_init_isoc(dev, EM28XX_ANALOG_MODE, 599 f.frequency = dev->ctl_freq;
769 EM28XX_NUM_PACKETS, 600 if (vq->owner && vq->owner->vdev->vfl_type == VFL_TYPE_RADIO)
770 EM28XX_NUM_BUFS, 601 f.type = V4L2_TUNER_RADIO;
771 dev->max_pkt_size,
772 em28xx_isoc_copy_vbi);
773 else 602 else
774 rc = em28xx_init_isoc(dev, EM28XX_ANALOG_MODE, 603 f.type = V4L2_TUNER_ANALOG_TV;
775 EM28XX_NUM_PACKETS, 604 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
776 EM28XX_NUM_BUFS,
777 dev->max_pkt_size,
778 em28xx_isoc_copy);
779 if (rc < 0)
780 goto fail;
781 } 605 }
782 606
783 buf->vb.state = VIDEOBUF_PREPARED;
784 return 0;
785
786fail: 607fail:
787 free_buffer(vq, buf);
788 return rc; 608 return rc;
789} 609}
790 610
791static void 611static int em28xx_stop_streaming(struct vb2_queue *vq)
792buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
793{ 612{
794 struct em28xx_buffer *buf = container_of(vb, 613 struct em28xx *dev = vb2_get_drv_priv(vq);
795 struct em28xx_buffer, 614 struct em28xx_dmaqueue *vidq = &dev->vidq;
796 vb); 615 unsigned long flags = 0;
797 struct em28xx_fh *fh = vq->priv_data; 616
798 struct em28xx *dev = fh->dev; 617 em28xx_videodbg("%s\n", __func__);
799 struct em28xx_dmaqueue *vidq = &dev->vidq; 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 }
800 625
801 buf->vb.state = VIDEOBUF_QUEUED; 626 spin_lock_irqsave(&dev->slock, flags);
802 list_add_tail(&buf->vb.queue, &vidq->active); 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);
803 635
636 return 0;
804} 637}
805 638
806static void buffer_release(struct videobuf_queue *vq, 639int em28xx_stop_vbi_streaming(struct vb2_queue *vq)
807 struct videobuf_buffer *vb)
808{ 640{
809 struct em28xx_buffer *buf = container_of(vb, 641 struct em28xx *dev = vb2_get_drv_priv(vq);
810 struct em28xx_buffer, 642 struct em28xx_dmaqueue *vbiq = &dev->vbiq;
811 vb); 643 unsigned long flags = 0;
812 struct em28xx_fh *fh = vq->priv_data; 644
813 struct em28xx *dev = (struct em28xx *)fh->dev; 645 em28xx_videodbg("%s\n", __func__);
814 646
815 em28xx_isocdbg("em28xx: called buffer_release\n"); 647 res_free(dev, vq->type);
816 648
817 free_buffer(vq, buf); 649 if (dev->streaming_users-- == 1) {
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);
663
664 return 0;
818} 665}
819 666
820static struct videobuf_queue_ops em28xx_video_qops = { 667static void
821 .buf_setup = buffer_setup, 668buffer_queue(struct vb2_buffer *vb)
669{
670 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
671 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
672 struct em28xx_dmaqueue *vidq = &dev->vidq;
673 unsigned long flags = 0;
674
675 em28xx_videodbg("%s\n", __func__);
676 buf->mem = vb2_plane_vaddr(vb, 0);
677 buf->length = vb2_plane_size(vb, 0);
678
679 spin_lock_irqsave(&dev->slock, flags);
680 list_add_tail(&buf->list, &vidq->active);
681 spin_unlock_irqrestore(&dev->slock, flags);
682}
683
684static struct vb2_ops em28xx_video_qops = {
685 .queue_setup = queue_setup,
822 .buf_prepare = buffer_prepare, 686 .buf_prepare = buffer_prepare,
823 .buf_queue = buffer_queue, 687 .buf_queue = buffer_queue,
824 .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,
825}; 692};
826 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 | VB2_DMABUF;
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
827/********************* v4l2 interface **************************************/ 728/********************* v4l2 interface **************************************/
828 729
829static void video_mux(struct em28xx *dev, int index) 730static void video_mux(struct em28xx *dev, int index)
@@ -856,143 +757,54 @@ static void video_mux(struct em28xx *dev, int index)
856 em28xx_audio_analog_set(dev); 757 em28xx_audio_analog_set(dev);
857} 758}
858 759
859/* Usage lock check functions */ 760void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv)
860static int res_get(struct em28xx_fh *fh, unsigned int bit)
861{
862 struct em28xx *dev = fh->dev;
863
864 if (fh->resources & bit)
865 /* have it already allocated */
866 return 1;
867
868 /* is it free? */
869 if (dev->resources & bit) {
870 /* no, someone else uses it */
871 return 0;
872 }
873 /* it's free, grab it */
874 fh->resources |= bit;
875 dev->resources |= bit;
876 em28xx_videodbg("res: get %d\n", bit);
877 return 1;
878}
879
880static int res_check(struct em28xx_fh *fh, unsigned int bit)
881{
882 return fh->resources & bit;
883}
884
885static int res_locked(struct em28xx *dev, unsigned int bit)
886{
887 return dev->resources & bit;
888}
889
890static void res_free(struct em28xx_fh *fh, unsigned int bits)
891{ 761{
892 struct em28xx *dev = fh->dev; 762 struct em28xx *dev = priv;
893
894 BUG_ON((fh->resources & bits) != bits);
895
896 fh->resources &= ~bits;
897 dev->resources &= ~bits;
898 em28xx_videodbg("res: put %d\n", bits);
899}
900
901static int get_ressource(struct em28xx_fh *fh)
902{
903 switch (fh->type) {
904 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
905 return EM28XX_RESOURCE_VIDEO;
906 case V4L2_BUF_TYPE_VBI_CAPTURE:
907 return EM28XX_RESOURCE_VBI;
908 default:
909 BUG();
910 return 0;
911 }
912}
913
914/*
915 * ac97_queryctrl()
916 * return the ac97 supported controls
917 */
918static int ac97_queryctrl(struct v4l2_queryctrl *qc)
919{
920 int i;
921 763
922 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) { 764 /*
923 if (qc->id && qc->id == ac97_qctrl[i].id) { 765 * In the case of non-AC97 volume controls, we still need
924 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc)); 766 * to do some setups at em28xx, in order to mute/unmute
925 return 0; 767 * and to adjust audio volume. However, the value ranges
926 } 768 * should be checked by the corresponding V4L subdriver.
927 } 769 */
928
929 /* Control is not ac97 related */
930 return 1;
931}
932
933/*
934 * ac97_get_ctrl()
935 * return the current values for ac97 mute and volume
936 */
937static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
938{
939 switch (ctrl->id) { 770 switch (ctrl->id) {
940 case V4L2_CID_AUDIO_MUTE: 771 case V4L2_CID_AUDIO_MUTE:
941 ctrl->value = dev->mute; 772 dev->mute = ctrl->val;
942 return 0; 773 em28xx_audio_analog_set(dev);
774 break;
943 case V4L2_CID_AUDIO_VOLUME: 775 case V4L2_CID_AUDIO_VOLUME:
944 ctrl->value = dev->volume; 776 dev->volume = ctrl->val;
945 return 0; 777 em28xx_audio_analog_set(dev);
946 default: 778 break;
947 /* Control is not ac97 related */
948 return 1;
949 } 779 }
950} 780}
951 781
952/* 782static int em28xx_s_ctrl(struct v4l2_ctrl *ctrl)
953 * ac97_set_ctrl()
954 * set values for ac97 mute and volume
955 */
956static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
957{ 783{
958 int i; 784 struct em28xx *dev = container_of(ctrl->handler, struct em28xx, ctrl_handler);
959
960 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
961 if (ctrl->id == ac97_qctrl[i].id)
962 goto handle;
963
964 /* Announce that hasn't handle it */
965 return 1;
966
967handle:
968 if (ctrl->value < ac97_qctrl[i].minimum ||
969 ctrl->value > ac97_qctrl[i].maximum)
970 return -ERANGE;
971 785
972 switch (ctrl->id) { 786 switch (ctrl->id) {
973 case V4L2_CID_AUDIO_MUTE: 787 case V4L2_CID_AUDIO_MUTE:
974 dev->mute = ctrl->value; 788 dev->mute = ctrl->val;
975 break; 789 break;
976 case V4L2_CID_AUDIO_VOLUME: 790 case V4L2_CID_AUDIO_VOLUME:
977 dev->volume = ctrl->value; 791 dev->volume = ctrl->val;
978 break; 792 break;
979 } 793 }
980 794
981 return em28xx_audio_analog_set(dev); 795 return em28xx_audio_analog_set(dev);
982} 796}
983 797
798const struct v4l2_ctrl_ops em28xx_ctrl_ops = {
799 .s_ctrl = em28xx_s_ctrl,
800};
801
984static int check_dev(struct em28xx *dev) 802static int check_dev(struct em28xx *dev)
985{ 803{
986 if (dev->state & DEV_DISCONNECTED) { 804 if (dev->disconnected) {
987 em28xx_errdev("v4l2 ioctl: device not present\n"); 805 em28xx_errdev("v4l2 ioctl: device not present\n");
988 return -ENODEV; 806 return -ENODEV;
989 } 807 }
990
991 if (dev->state & DEV_MISCONFIGURED) {
992 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
993 "close and open it again\n");
994 return -EIO;
995 }
996 return 0; 808 return 0;
997} 809}
998 810
@@ -1072,8 +884,11 @@ static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1072 /* the em2800 can only scale down to 50% */ 884 /* the em2800 can only scale down to 50% */
1073 height = height > (3 * maxh / 4) ? maxh : maxh / 2; 885 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1074 width = width > (3 * maxw / 4) ? maxw : maxw / 2; 886 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
1075 /* MaxPacketSize for em2800 is too small to capture at full resolution 887 /*
1076 * use half of maxw as the scaler can only scale to 50% */ 888 * MaxPacketSize for em2800 is too small to capture at full
889 * resolution use half of maxw as the scaler can only scale
890 * to 50%
891 */
1077 if (width == maxw && height == maxh) 892 if (width == maxw && height == maxh)
1078 width /= 2; 893 width /= 2;
1079 } else { 894 } else {
@@ -1091,7 +906,7 @@ static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1091 f->fmt.pix.width = width; 906 f->fmt.pix.width = width;
1092 f->fmt.pix.height = height; 907 f->fmt.pix.height = height;
1093 f->fmt.pix.pixelformat = fmt->fourcc; 908 f->fmt.pix.pixelformat = fmt->fourcc;
1094 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3; 909 f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
1095 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height; 910 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1096 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 911 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1097 if (dev->progressive) 912 if (dev->progressive)
@@ -1119,7 +934,6 @@ static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1119 /* set new image size */ 934 /* set new image size */
1120 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); 935 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1121 936
1122 em28xx_set_alternate(dev);
1123 em28xx_resolution_set(dev); 937 em28xx_resolution_set(dev);
1124 938
1125 return 0; 939 return 0;
@@ -1128,21 +942,13 @@ static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1128static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, 942static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1129 struct v4l2_format *f) 943 struct v4l2_format *f)
1130{ 944{
1131 struct em28xx_fh *fh = priv; 945 struct em28xx *dev = video_drvdata(file);
1132 struct em28xx *dev = fh->dev;
1133 int rc;
1134 946
1135 rc = check_dev(dev); 947 if (dev->streaming_users > 0)
1136 if (rc < 0) 948 return -EBUSY;
1137 return rc;
1138 949
1139 vidioc_try_fmt_vid_cap(file, priv, f); 950 vidioc_try_fmt_vid_cap(file, priv, f);
1140 951
1141 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1142 em28xx_errdev("%s queue busy\n", __func__);
1143 return -EBUSY;
1144 }
1145
1146 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat, 952 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
1147 f->fmt.pix.width, f->fmt.pix.height); 953 f->fmt.pix.width, f->fmt.pix.height);
1148} 954}
@@ -1153,6 +959,8 @@ static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1153 struct em28xx *dev = fh->dev; 959 struct em28xx *dev = fh->dev;
1154 int rc; 960 int rc;
1155 961
962 if (dev->board.is_webcam)
963 return -ENOTTY;
1156 rc = check_dev(dev); 964 rc = check_dev(dev);
1157 if (rc < 0) 965 if (rc < 0)
1158 return rc; 966 return rc;
@@ -1168,6 +976,8 @@ static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1168 struct em28xx *dev = fh->dev; 976 struct em28xx *dev = fh->dev;
1169 int rc; 977 int rc;
1170 978
979 if (dev->board.is_webcam)
980 return -ENOTTY;
1171 rc = check_dev(dev); 981 rc = check_dev(dev);
1172 if (rc < 0) 982 if (rc < 0)
1173 return rc; 983 return rc;
@@ -1184,15 +994,22 @@ static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
1184 struct v4l2_format f; 994 struct v4l2_format f;
1185 int rc; 995 int rc;
1186 996
997 if (dev->board.is_webcam)
998 return -ENOTTY;
999 if (*norm == dev->norm)
1000 return 0;
1187 rc = check_dev(dev); 1001 rc = check_dev(dev);
1188 if (rc < 0) 1002 if (rc < 0)
1189 return rc; 1003 return rc;
1190 1004
1005 if (dev->streaming_users > 0)
1006 return -EBUSY;
1007
1191 dev->norm = *norm; 1008 dev->norm = *norm;
1192 1009
1193 /* Adjusts width/height, if needed */ 1010 /* Adjusts width/height, if needed */
1194 f.fmt.pix.width = dev->width; 1011 f.fmt.pix.width = 720;
1195 f.fmt.pix.height = dev->height; 1012 f.fmt.pix.height = (*norm & V4L2_STD_525_60) ? 480 : 576;
1196 vidioc_try_fmt_vid_cap(file, priv, &f); 1013 vidioc_try_fmt_vid_cap(file, priv, &f);
1197 1014
1198 /* set new image size */ 1015 /* set new image size */
@@ -1216,6 +1033,7 @@ static int vidioc_g_parm(struct file *file, void *priv,
1216 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1033 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1217 return -EINVAL; 1034 return -EINVAL;
1218 1035
1036 p->parm.capture.readbuffers = EM28XX_MIN_BUF;
1219 if (dev->board.is_webcam) 1037 if (dev->board.is_webcam)
1220 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0, 1038 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1221 video, g_parm, p); 1039 video, g_parm, p);
@@ -1233,11 +1051,12 @@ static int vidioc_s_parm(struct file *file, void *priv,
1233 struct em28xx *dev = fh->dev; 1051 struct em28xx *dev = fh->dev;
1234 1052
1235 if (!dev->board.is_webcam) 1053 if (!dev->board.is_webcam)
1236 return -EINVAL; 1054 return -ENOTTY;
1237 1055
1238 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1056 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1239 return -EINVAL; 1057 return -EINVAL;
1240 1058
1059 p->parm.capture.readbuffers = EM28XX_MIN_BUF;
1241 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p); 1060 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1242} 1061}
1243 1062
@@ -1276,6 +1095,9 @@ static int vidioc_enum_input(struct file *file, void *priv,
1276 i->type = V4L2_INPUT_TYPE_TUNER; 1095 i->type = V4L2_INPUT_TYPE_TUNER;
1277 1096
1278 i->std = dev->vdev->tvnorms; 1097 i->std = dev->vdev->tvnorms;
1098 /* webcams do not have the STD API */
1099 if (dev->board.is_webcam)
1100 i->capabilities = 0;
1279 1101
1280 return 0; 1102 return 0;
1281} 1103}
@@ -1375,131 +1197,6 @@ static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio
1375 return 0; 1197 return 0;
1376} 1198}
1377 1199
1378static int vidioc_queryctrl(struct file *file, void *priv,
1379 struct v4l2_queryctrl *qc)
1380{
1381 struct em28xx_fh *fh = priv;
1382 struct em28xx *dev = fh->dev;
1383 int id = qc->id;
1384 int rc;
1385
1386 rc = check_dev(dev);
1387 if (rc < 0)
1388 return rc;
1389
1390 memset(qc, 0, sizeof(*qc));
1391
1392 qc->id = id;
1393
1394 /* enumerate AC97 controls */
1395 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1396 rc = ac97_queryctrl(qc);
1397 if (!rc)
1398 return 0;
1399 }
1400
1401 /* enumerate V4L2 device controls */
1402 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
1403
1404 if (qc->type)
1405 return 0;
1406 else
1407 return -EINVAL;
1408}
1409
1410/*
1411 * FIXME: This is an indirect way to check if a control exists at a
1412 * subdev. Instead of that hack, maybe the better would be to change all
1413 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1414 */
1415static int check_subdev_ctrl(struct em28xx *dev, int id)
1416{
1417 struct v4l2_queryctrl qc;
1418
1419 memset(&qc, 0, sizeof(qc));
1420 qc.id = id;
1421
1422 /* enumerate V4L2 device controls */
1423 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1424
1425 if (qc.type)
1426 return 0;
1427 else
1428 return -EINVAL;
1429}
1430
1431static int vidioc_g_ctrl(struct file *file, void *priv,
1432 struct v4l2_control *ctrl)
1433{
1434 struct em28xx_fh *fh = priv;
1435 struct em28xx *dev = fh->dev;
1436 int rc;
1437
1438 rc = check_dev(dev);
1439 if (rc < 0)
1440 return rc;
1441 rc = 0;
1442
1443 /* Set an AC97 control */
1444 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1445 rc = ac97_get_ctrl(dev, ctrl);
1446 else
1447 rc = 1;
1448
1449 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1450 if (rc == 1) {
1451 if (check_subdev_ctrl(dev, ctrl->id))
1452 return -EINVAL;
1453
1454 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1455 rc = 0;
1456 }
1457
1458 return rc;
1459}
1460
1461static int vidioc_s_ctrl(struct file *file, void *priv,
1462 struct v4l2_control *ctrl)
1463{
1464 struct em28xx_fh *fh = priv;
1465 struct em28xx *dev = fh->dev;
1466 int rc;
1467
1468 rc = check_dev(dev);
1469 if (rc < 0)
1470 return rc;
1471
1472 /* Set an AC97 control */
1473 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1474 rc = ac97_set_ctrl(dev, ctrl);
1475 else
1476 rc = 1;
1477
1478 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
1479 if (rc == 1) {
1480 rc = check_subdev_ctrl(dev, ctrl->id);
1481 if (!rc)
1482 v4l2_device_call_all(&dev->v4l2_dev, 0,
1483 core, s_ctrl, ctrl);
1484 /*
1485 * In the case of non-AC97 volume controls, we still need
1486 * to do some setups at em28xx, in order to mute/unmute
1487 * and to adjust audio volume. However, the value ranges
1488 * should be checked by the corresponding V4L subdriver.
1489 */
1490 switch (ctrl->id) {
1491 case V4L2_CID_AUDIO_MUTE:
1492 dev->mute = ctrl->value;
1493 rc = em28xx_audio_analog_set(dev);
1494 break;
1495 case V4L2_CID_AUDIO_VOLUME:
1496 dev->volume = ctrl->value;
1497 rc = em28xx_audio_analog_set(dev);
1498 }
1499 }
1500 return (rc < 0) ? rc : 0;
1501}
1502
1503static int vidioc_g_tuner(struct file *file, void *priv, 1200static int vidioc_g_tuner(struct file *file, void *priv,
1504 struct v4l2_tuner *t) 1201 struct v4l2_tuner *t)
1505{ 1202{
@@ -1515,7 +1212,6 @@ static int vidioc_g_tuner(struct file *file, void *priv,
1515 return -EINVAL; 1212 return -EINVAL;
1516 1213
1517 strcpy(t->name, "Tuner"); 1214 strcpy(t->name, "Tuner");
1518 t->type = V4L2_TUNER_ANALOG_TV;
1519 1215
1520 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t); 1216 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1521 return 0; 1217 return 0;
@@ -1545,7 +1241,9 @@ static int vidioc_g_frequency(struct file *file, void *priv,
1545 struct em28xx_fh *fh = priv; 1241 struct em28xx_fh *fh = priv;
1546 struct em28xx *dev = fh->dev; 1242 struct em28xx *dev = fh->dev;
1547 1243
1548 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; 1244 if (0 != f->tuner)
1245 return -EINVAL;
1246
1549 f->frequency = dev->ctl_freq; 1247 f->frequency = dev->ctl_freq;
1550 return 0; 1248 return 0;
1551} 1249}
@@ -1564,13 +1262,9 @@ static int vidioc_s_frequency(struct file *file, void *priv,
1564 if (0 != f->tuner) 1262 if (0 != f->tuner)
1565 return -EINVAL; 1263 return -EINVAL;
1566 1264
1567 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1568 return -EINVAL;
1569 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1570 return -EINVAL;
1571
1572 dev->ctl_freq = f->frequency;
1573 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f); 1265 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1266 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
1267 dev->ctl_freq = f->frequency;
1574 1268
1575 return 0; 1269 return 0;
1576} 1270}
@@ -1596,6 +1290,14 @@ static int vidioc_g_chip_ident(struct file *file, void *priv,
1596 1290
1597 chip->ident = V4L2_IDENT_NONE; 1291 chip->ident = V4L2_IDENT_NONE;
1598 chip->revision = 0; 1292 chip->revision = 0;
1293 if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
1294 if (v4l2_chip_match_host(&chip->match))
1295 chip->ident = V4L2_IDENT_NONE;
1296 return 0;
1297 }
1298 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1299 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1300 return -EINVAL;
1599 1301
1600 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip); 1302 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
1601 1303
@@ -1704,72 +1406,10 @@ static int vidioc_cropcap(struct file *file, void *priv,
1704 return 0; 1406 return 0;
1705} 1407}
1706 1408
1707static int vidioc_streamon(struct file *file, void *priv,
1708 enum v4l2_buf_type type)
1709{
1710 struct em28xx_fh *fh = priv;
1711 struct em28xx *dev = fh->dev;
1712 int rc = -EINVAL;
1713
1714 rc = check_dev(dev);
1715 if (rc < 0)
1716 return rc;
1717
1718 if (unlikely(type != fh->type))
1719 return -EINVAL;
1720
1721 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1722 fh, type, fh->resources, dev->resources);
1723
1724 if (unlikely(!res_get(fh, get_ressource(fh))))
1725 return -EBUSY;
1726
1727 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1728 rc = videobuf_streamon(&fh->vb_vidq);
1729 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1730 rc = videobuf_streamon(&fh->vb_vbiq);
1731
1732 return rc;
1733}
1734
1735static int vidioc_streamoff(struct file *file, void *priv,
1736 enum v4l2_buf_type type)
1737{
1738 struct em28xx_fh *fh = priv;
1739 struct em28xx *dev = fh->dev;
1740 int rc;
1741
1742 rc = check_dev(dev);
1743 if (rc < 0)
1744 return rc;
1745
1746 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1747 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
1748 return -EINVAL;
1749 if (type != fh->type)
1750 return -EINVAL;
1751
1752 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1753 fh, type, fh->resources, dev->resources);
1754
1755 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1756 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1757 videobuf_streamoff(&fh->vb_vidq);
1758 res_free(fh, EM28XX_RESOURCE_VIDEO);
1759 }
1760 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1761 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1762 videobuf_streamoff(&fh->vb_vbiq);
1763 res_free(fh, EM28XX_RESOURCE_VBI);
1764 }
1765 }
1766
1767 return 0;
1768}
1769
1770static int vidioc_querycap(struct file *file, void *priv, 1409static int vidioc_querycap(struct file *file, void *priv,
1771 struct v4l2_capability *cap) 1410 struct v4l2_capability *cap)
1772{ 1411{
1412 struct video_device *vdev = video_devdata(file);
1773 struct em28xx_fh *fh = priv; 1413 struct em28xx_fh *fh = priv;
1774 struct em28xx *dev = fh->dev; 1414 struct em28xx *dev = fh->dev;
1775 1415
@@ -1777,20 +1417,26 @@ static int vidioc_querycap(struct file *file, void *priv,
1777 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card)); 1417 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1778 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); 1418 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1779 1419
1780 cap->capabilities = 1420 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1781 V4L2_CAP_SLICED_VBI_CAPTURE | 1421 cap->device_caps = V4L2_CAP_READWRITE |
1782 V4L2_CAP_VIDEO_CAPTURE | 1422 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1783 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; 1423 else if (vdev->vfl_type == VFL_TYPE_RADIO)
1784 1424 cap->device_caps = V4L2_CAP_RADIO;
1785 if (dev->vbi_dev) 1425 else
1786 cap->capabilities |= V4L2_CAP_VBI_CAPTURE; 1426 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE;
1787 1427
1788 if (dev->audio_mode.has_audio) 1428 if (dev->audio_mode.has_audio)
1789 cap->capabilities |= V4L2_CAP_AUDIO; 1429 cap->device_caps |= V4L2_CAP_AUDIO;
1790 1430
1791 if (dev->tuner_type != TUNER_ABSENT) 1431 if (dev->tuner_type != TUNER_ABSENT)
1792 cap->capabilities |= V4L2_CAP_TUNER; 1432 cap->device_caps |= V4L2_CAP_TUNER;
1793 1433
1434 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1435 V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1436 if (dev->vbi_dev)
1437 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1438 if (dev->radio_dev)
1439 cap->capabilities |= V4L2_CAP_RADIO;
1794 return 0; 1440 return 0;
1795} 1441}
1796 1442
@@ -1845,46 +1491,6 @@ static int vidioc_enum_framesizes(struct file *file, void *priv,
1845 return 0; 1491 return 0;
1846} 1492}
1847 1493
1848/* Sliced VBI ioctls */
1849static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1850 struct v4l2_format *f)
1851{
1852 struct em28xx_fh *fh = priv;
1853 struct em28xx *dev = fh->dev;
1854 int rc;
1855
1856 rc = check_dev(dev);
1857 if (rc < 0)
1858 return rc;
1859
1860 f->fmt.sliced.service_set = 0;
1861 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
1862
1863 if (f->fmt.sliced.service_set == 0)
1864 rc = -EINVAL;
1865
1866 return rc;
1867}
1868
1869static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1870 struct v4l2_format *f)
1871{
1872 struct em28xx_fh *fh = priv;
1873 struct em28xx *dev = fh->dev;
1874 int rc;
1875
1876 rc = check_dev(dev);
1877 if (rc < 0)
1878 return rc;
1879
1880 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
1881
1882 if (f->fmt.sliced.service_set == 0)
1883 return -EINVAL;
1884
1885 return 0;
1886}
1887
1888/* RAW VBI ioctls */ 1494/* RAW VBI ioctls */
1889 1495
1890static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv, 1496static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
@@ -1900,6 +1506,7 @@ static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1900 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; 1506 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1901 format->fmt.vbi.count[0] = dev->vbi_height; 1507 format->fmt.vbi.count[0] = dev->vbi_height;
1902 format->fmt.vbi.count[1] = dev->vbi_height; 1508 format->fmt.vbi.count[1] = dev->vbi_height;
1509 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1903 1510
1904 /* Varies by video standard (NTSC, PAL, etc.) */ 1511 /* Varies by video standard (NTSC, PAL, etc.) */
1905 if (dev->norm & V4L2_STD_525_60) { 1512 if (dev->norm & V4L2_STD_525_60) {
@@ -1928,6 +1535,7 @@ static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1928 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; 1535 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1929 format->fmt.vbi.count[0] = dev->vbi_height; 1536 format->fmt.vbi.count[0] = dev->vbi_height;
1930 format->fmt.vbi.count[1] = dev->vbi_height; 1537 format->fmt.vbi.count[1] = dev->vbi_height;
1538 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1931 1539
1932 /* Varies by video standard (NTSC, PAL, etc.) */ 1540 /* Varies by video standard (NTSC, PAL, etc.) */
1933 if (dev->norm & V4L2_STD_525_60) { 1541 if (dev->norm & V4L2_STD_525_60) {
@@ -1943,100 +1551,10 @@ static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1943 return 0; 1551 return 0;
1944} 1552}
1945 1553
1946static int vidioc_reqbufs(struct file *file, void *priv,
1947 struct v4l2_requestbuffers *rb)
1948{
1949 struct em28xx_fh *fh = priv;
1950 struct em28xx *dev = fh->dev;
1951 int rc;
1952
1953 rc = check_dev(dev);
1954 if (rc < 0)
1955 return rc;
1956
1957 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1958 return videobuf_reqbufs(&fh->vb_vidq, rb);
1959 else
1960 return videobuf_reqbufs(&fh->vb_vbiq, rb);
1961}
1962
1963static int vidioc_querybuf(struct file *file, void *priv,
1964 struct v4l2_buffer *b)
1965{
1966 struct em28xx_fh *fh = priv;
1967 struct em28xx *dev = fh->dev;
1968 int rc;
1969
1970 rc = check_dev(dev);
1971 if (rc < 0)
1972 return rc;
1973
1974 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1975 return videobuf_querybuf(&fh->vb_vidq, b);
1976 else {
1977 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1978 the videobuf framework, but we probably shouldn't be
1979 returning a buffer larger than that which was asked for.
1980 At a minimum, it causes a crash in zvbi since it does
1981 a memcpy based on the source buffer length */
1982 int result = videobuf_querybuf(&fh->vb_vbiq, b);
1983 b->length = dev->vbi_width * dev->vbi_height * 2;
1984
1985 return result;
1986 }
1987}
1988
1989static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1990{
1991 struct em28xx_fh *fh = priv;
1992 struct em28xx *dev = fh->dev;
1993 int rc;
1994
1995 rc = check_dev(dev);
1996 if (rc < 0)
1997 return rc;
1998
1999 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2000 return videobuf_qbuf(&fh->vb_vidq, b);
2001 else
2002 return videobuf_qbuf(&fh->vb_vbiq, b);
2003}
2004
2005static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2006{
2007 struct em28xx_fh *fh = priv;
2008 struct em28xx *dev = fh->dev;
2009 int rc;
2010
2011 rc = check_dev(dev);
2012 if (rc < 0)
2013 return rc;
2014
2015 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2016 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
2017 O_NONBLOCK);
2018 else
2019 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
2020 O_NONBLOCK);
2021}
2022
2023/* ----------------------------------------------------------- */ 1554/* ----------------------------------------------------------- */
2024/* RADIO ESPECIFIC IOCTLS */ 1555/* RADIO ESPECIFIC IOCTLS */
2025/* ----------------------------------------------------------- */ 1556/* ----------------------------------------------------------- */
2026 1557
2027static int radio_querycap(struct file *file, void *priv,
2028 struct v4l2_capability *cap)
2029{
2030 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2031
2032 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2033 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
2034 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
2035
2036 cap->capabilities = V4L2_CAP_TUNER;
2037 return 0;
2038}
2039
2040static int radio_g_tuner(struct file *file, void *priv, 1558static int radio_g_tuner(struct file *file, void *priv,
2041 struct v4l2_tuner *t) 1559 struct v4l2_tuner *t)
2042{ 1560{
@@ -2053,26 +1571,6 @@ static int radio_g_tuner(struct file *file, void *priv,
2053 return 0; 1571 return 0;
2054} 1572}
2055 1573
2056static int radio_enum_input(struct file *file, void *priv,
2057 struct v4l2_input *i)
2058{
2059 if (i->index != 0)
2060 return -EINVAL;
2061 strcpy(i->name, "Radio");
2062 i->type = V4L2_INPUT_TYPE_TUNER;
2063
2064 return 0;
2065}
2066
2067static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2068{
2069 if (unlikely(a->index))
2070 return -EINVAL;
2071
2072 strcpy(a->name, "Radio");
2073 return 0;
2074}
2075
2076static int radio_s_tuner(struct file *file, void *priv, 1574static int radio_s_tuner(struct file *file, void *priv,
2077 struct v4l2_tuner *t) 1575 struct v4l2_tuner *t)
2078{ 1576{
@@ -2086,48 +1584,16 @@ static int radio_s_tuner(struct file *file, void *priv,
2086 return 0; 1584 return 0;
2087} 1585}
2088 1586
2089static int radio_s_audio(struct file *file, void *fh,
2090 const struct v4l2_audio *a)
2091{
2092 return 0;
2093}
2094
2095static int radio_s_input(struct file *file, void *fh, unsigned int i)
2096{
2097 return 0;
2098}
2099
2100static int radio_queryctrl(struct file *file, void *priv,
2101 struct v4l2_queryctrl *qc)
2102{
2103 int i;
2104
2105 if (qc->id < V4L2_CID_BASE ||
2106 qc->id >= V4L2_CID_LASTP1)
2107 return -EINVAL;
2108
2109 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2110 if (qc->id && qc->id == ac97_qctrl[i].id) {
2111 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
2112 return 0;
2113 }
2114 }
2115
2116 return -EINVAL;
2117}
2118
2119/* 1587/*
2120 * em28xx_v4l2_open() 1588 * em28xx_v4l2_open()
2121 * inits the device and starts isoc transfer 1589 * inits the device and starts isoc transfer
2122 */ 1590 */
2123static int em28xx_v4l2_open(struct file *filp) 1591static int em28xx_v4l2_open(struct file *filp)
2124{ 1592{
2125 int errCode = 0, radio = 0;
2126 struct video_device *vdev = video_devdata(filp); 1593 struct video_device *vdev = video_devdata(filp);
2127 struct em28xx *dev = video_drvdata(filp); 1594 struct em28xx *dev = video_drvdata(filp);
2128 enum v4l2_buf_type fh_type = 0; 1595 enum v4l2_buf_type fh_type = 0;
2129 struct em28xx_fh *fh; 1596 struct em28xx_fh *fh;
2130 enum v4l2_field field;
2131 1597
2132 switch (vdev->vfl_type) { 1598 switch (vdev->vfl_type) {
2133 case VFL_TYPE_GRABBER: 1599 case VFL_TYPE_GRABBER:
@@ -2136,9 +1602,6 @@ static int em28xx_v4l2_open(struct file *filp)
2136 case VFL_TYPE_VBI: 1602 case VFL_TYPE_VBI:
2137 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE; 1603 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2138 break; 1604 break;
2139 case VFL_TYPE_RADIO:
2140 radio = 1;
2141 break;
2142 } 1605 }
2143 1606
2144 em28xx_videodbg("open dev=%s type=%s users=%d\n", 1607 em28xx_videodbg("open dev=%s type=%s users=%d\n",
@@ -2154,14 +1617,13 @@ static int em28xx_v4l2_open(struct file *filp)
2154 mutex_unlock(&dev->lock); 1617 mutex_unlock(&dev->lock);
2155 return -ENOMEM; 1618 return -ENOMEM;
2156 } 1619 }
1620 v4l2_fh_init(&fh->fh, vdev);
2157 fh->dev = dev; 1621 fh->dev = dev;
2158 fh->radio = radio;
2159 fh->type = fh_type; 1622 fh->type = fh_type;
2160 filp->private_data = fh; 1623 filp->private_data = fh;
2161 1624
2162 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) { 1625 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
2163 em28xx_set_mode(dev, EM28XX_ANALOG_MODE); 1626 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
2164 em28xx_set_alternate(dev);
2165 em28xx_resolution_set(dev); 1627 em28xx_resolution_set(dev);
2166 1628
2167 /* Needed, since GPIO might have disabled power of 1629 /* Needed, since GPIO might have disabled power of
@@ -2170,31 +1632,18 @@ static int em28xx_v4l2_open(struct file *filp)
2170 em28xx_wake_i2c(dev); 1632 em28xx_wake_i2c(dev);
2171 1633
2172 } 1634 }
2173 if (fh->radio) { 1635
1636 if (vdev->vfl_type == VFL_TYPE_RADIO) {
2174 em28xx_videodbg("video_open: setting radio device\n"); 1637 em28xx_videodbg("video_open: setting radio device\n");
2175 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio); 1638 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
2176 } 1639 }
2177 1640
2178 dev->users++; 1641 dev->users++;
2179 1642
2180 if (dev->progressive)
2181 field = V4L2_FIELD_NONE;
2182 else
2183 field = V4L2_FIELD_INTERLACED;
2184
2185 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2186 NULL, &dev->slock,
2187 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
2188 sizeof(struct em28xx_buffer), fh, &dev->lock);
2189
2190 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2191 NULL, &dev->slock,
2192 V4L2_BUF_TYPE_VBI_CAPTURE,
2193 V4L2_FIELD_SEQ_TB,
2194 sizeof(struct em28xx_buffer), fh, &dev->lock);
2195 mutex_unlock(&dev->lock); 1643 mutex_unlock(&dev->lock);
1644 v4l2_fh_add(&fh->fh);
2196 1645
2197 return errCode; 1646 return 0;
2198} 1647}
2199 1648
2200/* 1649/*
@@ -2248,25 +1697,16 @@ static int em28xx_v4l2_close(struct file *filp)
2248 em28xx_videodbg("users=%d\n", dev->users); 1697 em28xx_videodbg("users=%d\n", dev->users);
2249 1698
2250 mutex_lock(&dev->lock); 1699 mutex_lock(&dev->lock);
2251 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) { 1700 vb2_fop_release(filp);
2252 videobuf_stop(&fh->vb_vidq);
2253 res_free(fh, EM28XX_RESOURCE_VIDEO);
2254 }
2255
2256 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2257 videobuf_stop(&fh->vb_vbiq);
2258 res_free(fh, EM28XX_RESOURCE_VBI);
2259 }
2260 1701
2261 if (dev->users == 1) { 1702 if (dev->users == 1) {
2262 /* the device is already disconnect, 1703 /* the device is already disconnect,
2263 free the remaining resources */ 1704 free the remaining resources */
2264 if (dev->state & DEV_DISCONNECTED) { 1705 if (dev->disconnected) {
2265 em28xx_release_resources(dev); 1706 em28xx_release_resources(dev);
2266 kfree(dev->alt_max_pkt_size); 1707 kfree(dev->alt_max_pkt_size_isoc);
2267 mutex_unlock(&dev->lock); 1708 mutex_unlock(&dev->lock);
2268 kfree(dev); 1709 kfree(dev);
2269 kfree(fh);
2270 return 0; 1710 return 0;
2271 } 1711 }
2272 1712
@@ -2274,7 +1714,6 @@ static int em28xx_v4l2_close(struct file *filp)
2274 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0); 1714 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
2275 1715
2276 /* do this before setting alternate! */ 1716 /* do this before setting alternate! */
2277 em28xx_uninit_isoc(dev, EM28XX_ANALOG_MODE);
2278 em28xx_set_mode(dev, EM28XX_SUSPEND); 1717 em28xx_set_mode(dev, EM28XX_SUSPEND);
2279 1718
2280 /* set alternate 0 */ 1719 /* set alternate 0 */
@@ -2287,129 +1726,18 @@ static int em28xx_v4l2_close(struct file *filp)
2287 } 1726 }
2288 } 1727 }
2289 1728
2290 videobuf_mmap_free(&fh->vb_vidq);
2291 videobuf_mmap_free(&fh->vb_vbiq);
2292 kfree(fh);
2293 dev->users--; 1729 dev->users--;
2294 mutex_unlock(&dev->lock); 1730 mutex_unlock(&dev->lock);
2295 return 0; 1731 return 0;
2296} 1732}
2297 1733
2298/*
2299 * em28xx_v4l2_read()
2300 * will allocate buffers when called for the first time
2301 */
2302static ssize_t
2303em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2304 loff_t *pos)
2305{
2306 struct em28xx_fh *fh = filp->private_data;
2307 struct em28xx *dev = fh->dev;
2308 int rc;
2309
2310 rc = check_dev(dev);
2311 if (rc < 0)
2312 return rc;
2313
2314 if (mutex_lock_interruptible(&dev->lock))
2315 return -ERESTARTSYS;
2316 /* FIXME: read() is not prepared to allow changing the video
2317 resolution while streaming. Seems a bug at em28xx_set_fmt
2318 */
2319
2320 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2321 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
2322 rc = -EBUSY;
2323 else
2324 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2325 filp->f_flags & O_NONBLOCK);
2326 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2327 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2328 rc = -EBUSY;
2329 else
2330 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
2331 filp->f_flags & O_NONBLOCK);
2332 }
2333 mutex_unlock(&dev->lock);
2334
2335 return rc;
2336}
2337
2338/*
2339 * em28xx_poll()
2340 * will allocate buffers when called for the first time
2341 */
2342static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
2343{
2344 struct em28xx_fh *fh = filp->private_data;
2345 struct em28xx *dev = fh->dev;
2346 int rc;
2347
2348 rc = check_dev(dev);
2349 if (rc < 0)
2350 return rc;
2351
2352 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2353 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2354 return POLLERR;
2355 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2356 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2357 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2358 return POLLERR;
2359 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
2360 } else {
2361 return POLLERR;
2362 }
2363}
2364
2365static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2366{
2367 struct em28xx_fh *fh = filp->private_data;
2368 struct em28xx *dev = fh->dev;
2369 unsigned int res;
2370
2371 mutex_lock(&dev->lock);
2372 res = em28xx_poll(filp, wait);
2373 mutex_unlock(&dev->lock);
2374 return res;
2375}
2376
2377/*
2378 * em28xx_v4l2_mmap()
2379 */
2380static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2381{
2382 struct em28xx_fh *fh = filp->private_data;
2383 struct em28xx *dev = fh->dev;
2384 int rc;
2385
2386 rc = check_dev(dev);
2387 if (rc < 0)
2388 return rc;
2389
2390 if (mutex_lock_interruptible(&dev->lock))
2391 return -ERESTARTSYS;
2392 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2393 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2394 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2395 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
2396 mutex_unlock(&dev->lock);
2397
2398 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2399 (unsigned long)vma->vm_start,
2400 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2401 rc);
2402
2403 return rc;
2404}
2405
2406static const struct v4l2_file_operations em28xx_v4l_fops = { 1734static const struct v4l2_file_operations em28xx_v4l_fops = {
2407 .owner = THIS_MODULE, 1735 .owner = THIS_MODULE,
2408 .open = em28xx_v4l2_open, 1736 .open = em28xx_v4l2_open,
2409 .release = em28xx_v4l2_close, 1737 .release = em28xx_v4l2_close,
2410 .read = em28xx_v4l2_read, 1738 .read = vb2_fop_read,
2411 .poll = em28xx_v4l2_poll, 1739 .poll = vb2_fop_poll,
2412 .mmap = em28xx_v4l2_mmap, 1740 .mmap = vb2_fop_mmap,
2413 .unlocked_ioctl = video_ioctl2, 1741 .unlocked_ioctl = video_ioctl2,
2414}; 1742};
2415 1743
@@ -2420,19 +1748,20 @@ static const struct v4l2_ioctl_ops video_ioctl_ops = {
2420 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 1748 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2421 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 1749 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2422 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap, 1750 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1751 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2423 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap, 1752 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
2424 .vidioc_enum_framesizes = vidioc_enum_framesizes, 1753 .vidioc_enum_framesizes = vidioc_enum_framesizes,
2425 .vidioc_g_audio = vidioc_g_audio, 1754 .vidioc_g_audio = vidioc_g_audio,
2426 .vidioc_s_audio = vidioc_s_audio, 1755 .vidioc_s_audio = vidioc_s_audio,
2427 .vidioc_cropcap = vidioc_cropcap, 1756 .vidioc_cropcap = vidioc_cropcap,
2428 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap, 1757
2429 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap, 1758 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2430 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap, 1759 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2431 1760 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
2432 .vidioc_reqbufs = vidioc_reqbufs, 1761 .vidioc_querybuf = vb2_ioctl_querybuf,
2433 .vidioc_querybuf = vidioc_querybuf, 1762 .vidioc_qbuf = vb2_ioctl_qbuf,
2434 .vidioc_qbuf = vidioc_qbuf, 1763 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2435 .vidioc_dqbuf = vidioc_dqbuf, 1764
2436 .vidioc_g_std = vidioc_g_std, 1765 .vidioc_g_std = vidioc_g_std,
2437 .vidioc_querystd = vidioc_querystd, 1766 .vidioc_querystd = vidioc_querystd,
2438 .vidioc_s_std = vidioc_s_std, 1767 .vidioc_s_std = vidioc_s_std,
@@ -2441,15 +1770,14 @@ static const struct v4l2_ioctl_ops video_ioctl_ops = {
2441 .vidioc_enum_input = vidioc_enum_input, 1770 .vidioc_enum_input = vidioc_enum_input,
2442 .vidioc_g_input = vidioc_g_input, 1771 .vidioc_g_input = vidioc_g_input,
2443 .vidioc_s_input = vidioc_s_input, 1772 .vidioc_s_input = vidioc_s_input,
2444 .vidioc_queryctrl = vidioc_queryctrl, 1773 .vidioc_streamon = vb2_ioctl_streamon,
2445 .vidioc_g_ctrl = vidioc_g_ctrl, 1774 .vidioc_streamoff = vb2_ioctl_streamoff,
2446 .vidioc_s_ctrl = vidioc_s_ctrl,
2447 .vidioc_streamon = vidioc_streamon,
2448 .vidioc_streamoff = vidioc_streamoff,
2449 .vidioc_g_tuner = vidioc_g_tuner, 1775 .vidioc_g_tuner = vidioc_g_tuner,
2450 .vidioc_s_tuner = vidioc_s_tuner, 1776 .vidioc_s_tuner = vidioc_s_tuner,
2451 .vidioc_g_frequency = vidioc_g_frequency, 1777 .vidioc_g_frequency = vidioc_g_frequency,
2452 .vidioc_s_frequency = vidioc_s_frequency, 1778 .vidioc_s_frequency = vidioc_s_frequency,
1779 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1780 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2453#ifdef CONFIG_VIDEO_ADV_DEBUG 1781#ifdef CONFIG_VIDEO_ADV_DEBUG
2454 .vidioc_g_register = vidioc_g_register, 1782 .vidioc_g_register = vidioc_g_register,
2455 .vidioc_s_register = vidioc_s_register, 1783 .vidioc_s_register = vidioc_s_register,
@@ -2459,11 +1787,10 @@ static const struct v4l2_ioctl_ops video_ioctl_ops = {
2459 1787
2460static const struct video_device em28xx_video_template = { 1788static const struct video_device em28xx_video_template = {
2461 .fops = &em28xx_v4l_fops, 1789 .fops = &em28xx_v4l_fops,
2462 .release = video_device_release, 1790 .release = video_device_release_empty,
2463 .ioctl_ops = &video_ioctl_ops, 1791 .ioctl_ops = &video_ioctl_ops,
2464 1792
2465 .tvnorms = V4L2_STD_ALL, 1793 .tvnorms = V4L2_STD_ALL,
2466 .current_norm = V4L2_STD_PAL,
2467}; 1794};
2468 1795
2469static const struct v4l2_file_operations radio_fops = { 1796static const struct v4l2_file_operations radio_fops = {
@@ -2474,18 +1801,13 @@ static const struct v4l2_file_operations radio_fops = {
2474}; 1801};
2475 1802
2476static const struct v4l2_ioctl_ops radio_ioctl_ops = { 1803static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2477 .vidioc_querycap = radio_querycap, 1804 .vidioc_querycap = vidioc_querycap,
2478 .vidioc_g_tuner = radio_g_tuner, 1805 .vidioc_g_tuner = radio_g_tuner,
2479 .vidioc_enum_input = radio_enum_input,
2480 .vidioc_g_audio = radio_g_audio,
2481 .vidioc_s_tuner = radio_s_tuner, 1806 .vidioc_s_tuner = radio_s_tuner,
2482 .vidioc_s_audio = radio_s_audio,
2483 .vidioc_s_input = radio_s_input,
2484 .vidioc_queryctrl = radio_queryctrl,
2485 .vidioc_g_ctrl = vidioc_g_ctrl,
2486 .vidioc_s_ctrl = vidioc_s_ctrl,
2487 .vidioc_g_frequency = vidioc_g_frequency, 1807 .vidioc_g_frequency = vidioc_g_frequency,
2488 .vidioc_s_frequency = vidioc_s_frequency, 1808 .vidioc_s_frequency = vidioc_s_frequency,
1809 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1810 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2489#ifdef CONFIG_VIDEO_ADV_DEBUG 1811#ifdef CONFIG_VIDEO_ADV_DEBUG
2490 .vidioc_g_register = vidioc_g_register, 1812 .vidioc_g_register = vidioc_g_register,
2491 .vidioc_s_register = vidioc_s_register, 1813 .vidioc_s_register = vidioc_s_register,
@@ -2514,9 +1836,11 @@ static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2514 1836
2515 *vfd = *template; 1837 *vfd = *template;
2516 vfd->v4l2_dev = &dev->v4l2_dev; 1838 vfd->v4l2_dev = &dev->v4l2_dev;
2517 vfd->release = video_device_release;
2518 vfd->debug = video_debug; 1839 vfd->debug = video_debug;
2519 vfd->lock = &dev->lock; 1840 vfd->lock = &dev->lock;
1841 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
1842 if (dev->board.is_webcam)
1843 vfd->tvnorms = 0;
2520 1844
2521 snprintf(vfd->name, sizeof(vfd->name), "%s %s", 1845 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2522 dev->name, type_name); 1846 dev->name, type_name);
@@ -2527,7 +1851,7 @@ static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2527 1851
2528int em28xx_register_analog_devices(struct em28xx *dev) 1852int em28xx_register_analog_devices(struct em28xx *dev)
2529{ 1853{
2530 u8 val; 1854 u8 val;
2531 int ret; 1855 int ret;
2532 unsigned int maxw; 1856 unsigned int maxw;
2533 1857
@@ -2535,7 +1859,7 @@ int em28xx_register_analog_devices(struct em28xx *dev)
2535 dev->name, EM28XX_VERSION); 1859 dev->name, EM28XX_VERSION);
2536 1860
2537 /* set default norm */ 1861 /* set default norm */
2538 dev->norm = em28xx_video_template.current_norm; 1862 dev->norm = V4L2_STD_PAL;
2539 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm); 1863 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
2540 dev->interlaced = EM28XX_INTERLACED_DEFAULT; 1864 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
2541 1865
@@ -2543,10 +1867,10 @@ int em28xx_register_analog_devices(struct em28xx *dev)
2543 dev->format = &format[0]; 1867 dev->format = &format[0];
2544 1868
2545 maxw = norm_maxw(dev); 1869 maxw = norm_maxw(dev);
2546 /* MaxPacketSize for em2800 is too small to capture at full resolution 1870 /* MaxPacketSize for em2800 is too small to capture at full resolution
2547 * use half of maxw as the scaler can only scale to 50% */ 1871 * use half of maxw as the scaler can only scale to 50% */
2548 if (dev->board.is_em2800) 1872 if (dev->board.is_em2800)
2549 maxw /= 2; 1873 maxw /= 2;
2550 1874
2551 em28xx_set_video_format(dev, format[0].fourcc, 1875 em28xx_set_video_format(dev, format[0].fourcc,
2552 maxw, norm_maxh(dev)); 1876 maxw, norm_maxh(dev));
@@ -2572,6 +1896,8 @@ int em28xx_register_analog_devices(struct em28xx *dev)
2572 em28xx_errdev("cannot allocate video_device.\n"); 1896 em28xx_errdev("cannot allocate video_device.\n");
2573 return -ENODEV; 1897 return -ENODEV;
2574 } 1898 }
1899 dev->vdev->queue = &dev->vb_vidq;
1900 dev->vdev->queue->lock = &dev->vb_queue_lock;
2575 1901
2576 /* register v4l2 video video_device */ 1902 /* register v4l2 video video_device */
2577 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER, 1903 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
@@ -2587,6 +1913,9 @@ int em28xx_register_analog_devices(struct em28xx *dev)
2587 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, 1913 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2588 "vbi"); 1914 "vbi");
2589 1915
1916 dev->vbi_dev->queue = &dev->vb_vbiq;
1917 dev->vbi_dev->queue->lock = &dev->vb_vbi_queue_lock;
1918
2590 /* register v4l2 vbi video_device */ 1919 /* register v4l2 vbi video_device */
2591 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, 1920 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2592 vbi_nr[dev->devno]); 1921 vbi_nr[dev->devno]);