]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/media/video/em28xx/em28xx-video.c
Merge branch 'docs-next' of git://git.lwn.net/linux-2.6
[mirror_ubuntu-hirsute-kernel.git] / drivers / media / video / em28xx / em28xx-video.c
CommitLineData
a6c2ba28 1/*
6ea54d93
DSL
2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
a6c2ba28 4
f7abcd38
MCC
5 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
2e7c6dc3 7 Mauro Carvalho Chehab <mchehab@infradead.org>
f7abcd38 8 Sascha Sommer <saschasommer@freenet.de>
a6c2ba28 9
439090d7
MCC
10 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
12
a6c2ba28 13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
31#include <linux/kernel.h>
e5589bef 32#include <linux/bitmap.h>
a6c2ba28 33#include <linux/usb.h>
a6c2ba28 34#include <linux/i2c.h>
b296fc60 35#include <linux/version.h>
6d35c8f6 36#include <linux/mm.h>
1e4baed3 37#include <linux/mutex.h>
5a0e3ad6 38#include <linux/slab.h>
a6c2ba28 39
f7abcd38 40#include "em28xx.h"
c0477ad9 41#include <media/v4l2-common.h>
35ea11ff 42#include <media/v4l2-ioctl.h>
14983d81 43#include <media/v4l2-chip-ident.h>
2474ed44 44#include <media/msp3400.h>
ed086314 45#include <media/tuner.h>
a6c2ba28 46
f7abcd38
MCC
47#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
48 "Markus Rechberger <mrechberger@gmail.com>, " \
2e7c6dc3 49 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
f7abcd38 50 "Sascha Sommer <saschasommer@freenet.de>"
a6c2ba28 51
f7abcd38 52#define DRIVER_DESC "Empia em28xx based USB video device driver"
f2cf250a 53#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
a6c2ba28 54
3acf2809 55#define em28xx_videodbg(fmt, arg...) do {\
4ac97914
MCC
56 if (video_debug) \
57 printk(KERN_INFO "%s %s :"fmt, \
d80e134d 58 dev->name, __func__ , ##arg); } while (0)
a6c2ba28 59
ad0ebb96 60static unsigned int isoc_debug;
f245e549
MCC
61module_param(isoc_debug, int, 0644);
62MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
ad0ebb96 63
6ea54d93
DSL
64#define em28xx_isocdbg(fmt, arg...) \
65do {\
66 if (isoc_debug) { \
ad0ebb96 67 printk(KERN_INFO "%s %s :"fmt, \
6ea54d93
DSL
68 dev->name, __func__ , ##arg); \
69 } \
70 } while (0)
ad0ebb96 71
a6c2ba28 72MODULE_AUTHOR(DRIVER_AUTHOR);
73MODULE_DESCRIPTION(DRIVER_DESC);
74MODULE_LICENSE("GPL");
75
e5589bef 76static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
0be43754
MCC
77static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
78static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79
e5589bef
MCC
80module_param_array(video_nr, int, NULL, 0444);
81module_param_array(vbi_nr, int, NULL, 0444);
0be43754 82module_param_array(radio_nr, int, NULL, 0444);
0be43754
MCC
83MODULE_PARM_DESC(video_nr, "video device numbers");
84MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
85MODULE_PARM_DESC(radio_nr, "radio device numbers");
596d92d5 86
ff699e6b 87static unsigned int video_debug;
6ea54d93
DSL
88module_param(video_debug, int, 0644);
89MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
a6c2ba28 90
bddcf633
MCC
91/* supported video standards */
92static struct em28xx_fmt format[] = {
93 {
58fc1ce3 94 .name = "16 bpp YUY2, 4:2:2, packed",
bddcf633
MCC
95 .fourcc = V4L2_PIX_FMT_YUYV,
96 .depth = 16,
3fbf9309 97 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
43cb9fe3 98 }, {
58fc1ce3 99 .name = "16 bpp RGB 565, LE",
43cb9fe3
MCC
100 .fourcc = V4L2_PIX_FMT_RGB565,
101 .depth = 16,
58fc1ce3
MCC
102 .reg = EM28XX_OUTFMT_RGB_16_656,
103 }, {
104 .name = "8 bpp Bayer BGBG..GRGR",
105 .fourcc = V4L2_PIX_FMT_SBGGR8,
106 .depth = 8,
107 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
108 }, {
109 .name = "8 bpp Bayer GRGR..BGBG",
110 .fourcc = V4L2_PIX_FMT_SGRBG8,
111 .depth = 8,
112 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
113 }, {
114 .name = "8 bpp Bayer GBGB..RGRG",
115 .fourcc = V4L2_PIX_FMT_SGBRG8,
116 .depth = 8,
117 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
118 }, {
119 .name = "12 bpp YUV411",
120 .fourcc = V4L2_PIX_FMT_YUV411P,
121 .depth = 12,
122 .reg = EM28XX_OUTFMT_YUV411,
bddcf633
MCC
123 },
124};
125
a6c2ba28 126/* supported controls */
c0477ad9 127/* Common to all boards */
ed10daae 128static struct v4l2_queryctrl ac97_qctrl[] = {
c0477ad9
MCC
129 {
130 .id = V4L2_CID_AUDIO_VOLUME,
131 .type = V4L2_CTRL_TYPE_INTEGER,
132 .name = "Volume",
133 .minimum = 0x0,
134 .maximum = 0x1f,
135 .step = 0x1,
136 .default_value = 0x1f,
a98f6af9 137 .flags = V4L2_CTRL_FLAG_SLIDER,
6ea54d93 138 }, {
c0477ad9
MCC
139 .id = V4L2_CID_AUDIO_MUTE,
140 .type = V4L2_CTRL_TYPE_BOOLEAN,
141 .name = "Mute",
142 .minimum = 0,
143 .maximum = 1,
144 .step = 1,
145 .default_value = 1,
146 .flags = 0,
147 }
148};
149
ad0ebb96
MCC
150/* ------------------------------------------------------------------
151 DMA and thread functions
152 ------------------------------------------------------------------*/
153
154/*
155 * Announces that a buffer were filled and request the next
156 */
f245e549 157static inline void buffer_filled(struct em28xx *dev,
ad0ebb96
MCC
158 struct em28xx_dmaqueue *dma_q,
159 struct em28xx_buffer *buf)
160{
ad0ebb96
MCC
161 /* Advice that buffer was filled */
162 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
163 buf->vb.state = VIDEOBUF_DONE;
164 buf->vb.field_count++;
165 do_gettimeofday(&buf->vb.ts);
166
28abf083
DH
167 dev->isoc_ctl.vid_buf = NULL;
168
169 list_del(&buf->vb.queue);
170 wake_up(&buf->vb.done);
171}
172
173static inline void vbi_buffer_filled(struct em28xx *dev,
174 struct em28xx_dmaqueue *dma_q,
175 struct em28xx_buffer *buf)
176{
177 /* Advice that buffer was filled */
178 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
179
180 buf->vb.state = VIDEOBUF_DONE;
181 buf->vb.field_count++;
182 do_gettimeofday(&buf->vb.ts);
183
184 dev->isoc_ctl.vbi_buf = NULL;
cb784724 185
ad0ebb96
MCC
186 list_del(&buf->vb.queue);
187 wake_up(&buf->vb.done);
188}
189
190/*
191 * Identify the buffer header type and properly handles
192 */
193static void em28xx_copy_video(struct em28xx *dev,
194 struct em28xx_dmaqueue *dma_q,
195 struct em28xx_buffer *buf,
196 unsigned char *p,
197 unsigned char *outp, unsigned long len)
198{
199 void *fieldstart, *startwrite, *startread;
f245e549 200 int linesdone, currlinedone, offset, lencopy, remain;
44dc733c 201 int bytesperline = dev->width << 1;
ad0ebb96
MCC
202
203 if (dma_q->pos + len > buf->vb.size)
204 len = buf->vb.size - dma_q->pos;
205
ad0ebb96
MCC
206 startread = p;
207 remain = len;
208
c2a6b54a 209 if (dev->progressive)
ad0ebb96 210 fieldstart = outp;
c2a6b54a
MCC
211 else {
212 /* Interlaces two half frames */
213 if (buf->top_field)
214 fieldstart = outp;
215 else
216 fieldstart = outp + bytesperline;
217 }
ad0ebb96 218
44dc733c
MCC
219 linesdone = dma_q->pos / bytesperline;
220 currlinedone = dma_q->pos % bytesperline;
c2a6b54a
MCC
221
222 if (dev->progressive)
223 offset = linesdone * bytesperline + currlinedone;
224 else
225 offset = linesdone * bytesperline * 2 + currlinedone;
226
ad0ebb96 227 startwrite = fieldstart + offset;
44dc733c 228 lencopy = bytesperline - currlinedone;
ad0ebb96
MCC
229 lencopy = lencopy > remain ? remain : lencopy;
230
f245e549 231 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
ea8df7e0 232 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
f245e549
MCC
233 ((char *)startwrite + lencopy) -
234 ((char *)outp + buf->vb.size));
a1a6ee74
NS
235 remain = (char *)outp + buf->vb.size - (char *)startwrite;
236 lencopy = remain;
d7aa8020 237 }
e0fadfd3
AT
238 if (lencopy <= 0)
239 return;
d7aa8020 240 memcpy(startwrite, startread, lencopy);
ad0ebb96
MCC
241
242 remain -= lencopy;
243
244 while (remain > 0) {
44dc733c 245 startwrite += lencopy + bytesperline;
ad0ebb96 246 startread += lencopy;
44dc733c 247 if (bytesperline > remain)
ad0ebb96
MCC
248 lencopy = remain;
249 else
44dc733c 250 lencopy = bytesperline;
ad0ebb96 251
a1a6ee74
NS
252 if ((char *)startwrite + lencopy > (char *)outp +
253 buf->vb.size) {
e3ba4d34
DH
254 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
255 "(2)\n",
f245e549
MCC
256 ((char *)startwrite + lencopy) -
257 ((char *)outp + buf->vb.size));
258 lencopy = remain = (char *)outp + buf->vb.size -
259 (char *)startwrite;
d7aa8020 260 }
f245e549
MCC
261 if (lencopy <= 0)
262 break;
d7aa8020
AT
263
264 memcpy(startwrite, startread, lencopy);
ad0ebb96
MCC
265
266 remain -= lencopy;
267 }
268
269 dma_q->pos += len;
270}
271
28abf083
DH
272static void em28xx_copy_vbi(struct em28xx *dev,
273 struct em28xx_dmaqueue *dma_q,
274 struct em28xx_buffer *buf,
275 unsigned char *p,
276 unsigned char *outp, unsigned long len)
277{
278 void *startwrite, *startread;
279 int offset;
66d9cbad 280 int bytesperline = dev->vbi_width;
28abf083
DH
281
282 if (dev == NULL) {
e3ba4d34 283 em28xx_isocdbg("dev is null\n");
28abf083
DH
284 return;
285 }
286
287 if (dma_q == NULL) {
e3ba4d34 288 em28xx_isocdbg("dma_q is null\n");
28abf083
DH
289 return;
290 }
291 if (buf == NULL) {
292 return;
293 }
294 if (p == NULL) {
e3ba4d34 295 em28xx_isocdbg("p is null\n");
28abf083
DH
296 return;
297 }
298 if (outp == NULL) {
e3ba4d34 299 em28xx_isocdbg("outp is null\n");
28abf083
DH
300 return;
301 }
302
303 if (dma_q->pos + len > buf->vb.size)
304 len = buf->vb.size - dma_q->pos;
305
28abf083
DH
306 startread = p;
307
308 startwrite = outp + dma_q->pos;
309 offset = dma_q->pos;
310
311 /* Make sure the bottom field populates the second half of the frame */
312 if (buf->top_field == 0) {
66d9cbad
DH
313 startwrite += bytesperline * dev->vbi_height;
314 offset += bytesperline * dev->vbi_height;
28abf083
DH
315 }
316
317 memcpy(startwrite, startread, len);
318 dma_q->pos += len;
319}
320
f245e549 321static inline void print_err_status(struct em28xx *dev,
ad0ebb96
MCC
322 int packet, int status)
323{
324 char *errmsg = "Unknown";
325
f245e549 326 switch (status) {
ad0ebb96
MCC
327 case -ENOENT:
328 errmsg = "unlinked synchronuously";
329 break;
330 case -ECONNRESET:
331 errmsg = "unlinked asynchronuously";
332 break;
333 case -ENOSR:
334 errmsg = "Buffer error (overrun)";
335 break;
336 case -EPIPE:
337 errmsg = "Stalled (device not responding)";
338 break;
339 case -EOVERFLOW:
340 errmsg = "Babble (bad cable?)";
341 break;
342 case -EPROTO:
343 errmsg = "Bit-stuff error (bad cable?)";
344 break;
345 case -EILSEQ:
346 errmsg = "CRC/Timeout (could be anything)";
347 break;
348 case -ETIME:
349 errmsg = "Device does not respond";
350 break;
351 }
f245e549 352 if (packet < 0) {
ad0ebb96
MCC
353 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
354 } else {
355 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
356 packet, status, errmsg);
357 }
358}
359
360/*
361 * video-buf generic routine to get the next available buffer
362 */
3b5fa928 363static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
ad0ebb96
MCC
364 struct em28xx_buffer **buf)
365{
366 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
dbecb44c 367 char *outp;
ad0ebb96 368
dbecb44c
MCC
369 if (list_empty(&dma_q->active)) {
370 em28xx_isocdbg("No active queue to serve\n");
28abf083 371 dev->isoc_ctl.vid_buf = NULL;
dbecb44c 372 *buf = NULL;
3b5fa928 373 return;
dbecb44c
MCC
374 }
375
dbecb44c
MCC
376 /* Get the next buffer */
377 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
378
dbecb44c
MCC
379 /* Cleans up buffer - Usefull for testing for frame/URB loss */
380 outp = videobuf_to_vmalloc(&(*buf)->vb);
381 memset(outp, 0, (*buf)->vb.size);
cb784724 382
28abf083
DH
383 dev->isoc_ctl.vid_buf = *buf;
384
385 return;
386}
387
388/*
389 * video-buf generic routine to get the next available VBI buffer
390 */
391static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
392 struct em28xx_buffer **buf)
393{
394 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
395 char *outp;
396
397 if (list_empty(&dma_q->active)) {
398 em28xx_isocdbg("No active queue to serve\n");
399 dev->isoc_ctl.vbi_buf = NULL;
400 *buf = NULL;
401 return;
402 }
403
404 /* Get the next buffer */
405 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
406 /* Cleans up buffer - Usefull for testing for frame/URB loss */
407 outp = videobuf_to_vmalloc(&(*buf)->vb);
408 memset(outp, 0x00, (*buf)->vb.size);
409
410 dev->isoc_ctl.vbi_buf = *buf;
cb784724 411
3b5fa928 412 return;
ad0ebb96
MCC
413}
414
415/*
416 * Controls the isoc copy of each urb packet
417 */
579f72e4 418static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
ad0ebb96 419{
d7aa8020 420 struct em28xx_buffer *buf;
da52a55c 421 struct em28xx_dmaqueue *dma_q = &dev->vidq;
3b5fa928 422 unsigned char *outp = NULL;
ad0ebb96 423 int i, len = 0, rc = 1;
d7aa8020 424 unsigned char *p;
ad0ebb96
MCC
425
426 if (!dev)
427 return 0;
428
429 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
430 return 0;
431
f245e549
MCC
432 if (urb->status < 0) {
433 print_err_status(dev, -1, urb->status);
ad0ebb96
MCC
434 if (urb->status == -ENOENT)
435 return 0;
436 }
437
28abf083 438 buf = dev->isoc_ctl.vid_buf;
3b5fa928
AT
439 if (buf != NULL)
440 outp = videobuf_to_vmalloc(&buf->vb);
d7aa8020 441
ad0ebb96
MCC
442 for (i = 0; i < urb->number_of_packets; i++) {
443 int status = urb->iso_frame_desc[i].status;
444
f245e549
MCC
445 if (status < 0) {
446 print_err_status(dev, i, status);
ad0ebb96
MCC
447 if (urb->iso_frame_desc[i].status != -EPROTO)
448 continue;
449 }
450
f245e549 451 len = urb->iso_frame_desc[i].actual_length - 4;
ad0ebb96
MCC
452
453 if (urb->iso_frame_desc[i].actual_length <= 0) {
d7aa8020 454 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
ad0ebb96
MCC
455 continue;
456 }
457 if (urb->iso_frame_desc[i].actual_length >
458 dev->max_pkt_size) {
459 em28xx_isocdbg("packet bigger than packet size");
460 continue;
461 }
462
463 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
ad0ebb96
MCC
464
465 /* FIXME: incomplete buffer checks where removed to make
466 logic simpler. Impacts of those changes should be evaluated
467 */
b4916f8c
MCC
468 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
469 em28xx_isocdbg("VBI HEADER!!!\n");
470 /* FIXME: Should add vbi copy */
471 continue;
472 }
d7aa8020 473 if (p[0] == 0x22 && p[1] == 0x5a) {
78bb3949 474 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
a1a6ee74 475 len, (p[2] & 1) ? "odd" : "even");
d7aa8020 476
c2a6b54a 477 if (dev->progressive || !(p[2] & 1)) {
3b5fa928
AT
478 if (buf != NULL)
479 buffer_filled(dev, dma_q, buf);
480 get_next_buf(dma_q, &buf);
481 if (buf == NULL)
482 outp = NULL;
483 else
484 outp = videobuf_to_vmalloc(&buf->vb);
485 }
486
487 if (buf != NULL) {
488 if (p[2] & 1)
489 buf->top_field = 0;
490 else
491 buf->top_field = 1;
e0fadfd3 492 }
b4916f8c 493
ad0ebb96 494 dma_q->pos = 0;
ad0ebb96 495 }
5fee3340
DH
496 if (buf != NULL) {
497 if (p[0] != 0x88 && p[0] != 0x22) {
498 em28xx_isocdbg("frame is not complete\n");
499 len += 4;
500 } else {
501 p += 4;
502 }
3b5fa928 503 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
5fee3340 504 }
ad0ebb96
MCC
505 }
506 return rc;
507}
508
da52a55c
DH
509/* Version of isoc handler that takes into account a mixture of video and
510 VBI data */
511static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
512{
513 struct em28xx_buffer *buf, *vbi_buf;
514 struct em28xx_dmaqueue *dma_q = &dev->vidq;
28abf083 515 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
da52a55c
DH
516 unsigned char *outp = NULL;
517 unsigned char *vbioutp = NULL;
518 int i, len = 0, rc = 1;
519 unsigned char *p;
520 int vbi_size;
521
522 if (!dev)
523 return 0;
524
525 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
526 return 0;
527
528 if (urb->status < 0) {
529 print_err_status(dev, -1, urb->status);
530 if (urb->status == -ENOENT)
531 return 0;
532 }
533
28abf083 534 buf = dev->isoc_ctl.vid_buf;
da52a55c
DH
535 if (buf != NULL)
536 outp = videobuf_to_vmalloc(&buf->vb);
28abf083
DH
537
538 vbi_buf = dev->isoc_ctl.vbi_buf;
539 if (vbi_buf != NULL)
540 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
541
da52a55c
DH
542 for (i = 0; i < urb->number_of_packets; i++) {
543 int status = urb->iso_frame_desc[i].status;
544
545 if (status < 0) {
546 print_err_status(dev, i, status);
547 if (urb->iso_frame_desc[i].status != -EPROTO)
548 continue;
549 }
550
5fee3340 551 len = urb->iso_frame_desc[i].actual_length;
da52a55c
DH
552 if (urb->iso_frame_desc[i].actual_length <= 0) {
553 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
554 continue;
555 }
556 if (urb->iso_frame_desc[i].actual_length >
557 dev->max_pkt_size) {
558 em28xx_isocdbg("packet bigger than packet size");
559 continue;
560 }
561
562 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
563
564 /* capture type 0 = vbi start
565 capture type 1 = video start
566 capture type 2 = video in progress */
567 if (p[0] == 0x33 && p[1] == 0x95) {
568 dev->capture_type = 0;
569 dev->vbi_read = 0;
570 em28xx_isocdbg("VBI START HEADER!!!\n");
571 dev->cur_field = p[2];
5fee3340
DH
572 p += 4;
573 len -= 4;
574 } else if (p[0] == 0x88 && p[1] == 0x88 &&
575 p[2] == 0x88 && p[3] == 0x88) {
576 /* continuation */
577 p += 4;
578 len -= 4;
579 } else if (p[0] == 0x22 && p[1] == 0x5a) {
580 /* start video */
581 p += 4;
582 len -= 4;
da52a55c
DH
583 }
584
66d9cbad 585 vbi_size = dev->vbi_width * dev->vbi_height;
da52a55c
DH
586
587 if (dev->capture_type == 0) {
588 if (dev->vbi_read >= vbi_size) {
589 /* We've already read all the VBI data, so
590 treat the rest as video */
e3ba4d34 591 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
da52a55c
DH
592 } else if ((dev->vbi_read + len) < vbi_size) {
593 /* This entire frame is VBI data */
28abf083
DH
594 if (dev->vbi_read == 0 &&
595 (!(dev->cur_field & 1))) {
596 /* Brand new frame */
597 if (vbi_buf != NULL)
598 vbi_buffer_filled(dev,
599 vbi_dma_q,
600 vbi_buf);
601 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
602 if (vbi_buf == NULL)
603 vbioutp = NULL;
e3ba4d34
DH
604 else
605 vbioutp = videobuf_to_vmalloc(
606 &vbi_buf->vb);
28abf083
DH
607 }
608
609 if (dev->vbi_read == 0) {
610 vbi_dma_q->pos = 0;
611 if (vbi_buf != NULL) {
612 if (dev->cur_field & 1)
613 vbi_buf->top_field = 0;
614 else
615 vbi_buf->top_field = 1;
616 }
617 }
618
da52a55c 619 dev->vbi_read += len;
28abf083
DH
620 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
621 vbioutp, len);
da52a55c
DH
622 } else {
623 /* Some of this frame is VBI data and some is
624 video data */
625 int vbi_data_len = vbi_size - dev->vbi_read;
626 dev->vbi_read += vbi_data_len;
28abf083
DH
627 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
628 vbioutp, vbi_data_len);
da52a55c
DH
629 dev->capture_type = 1;
630 p += vbi_data_len;
631 len -= vbi_data_len;
632 }
633 }
634
635 if (dev->capture_type == 1) {
636 dev->capture_type = 2;
da52a55c
DH
637 if (dev->progressive || !(dev->cur_field & 1)) {
638 if (buf != NULL)
639 buffer_filled(dev, dma_q, buf);
640 get_next_buf(dma_q, &buf);
641 if (buf == NULL)
642 outp = NULL;
643 else
644 outp = videobuf_to_vmalloc(&buf->vb);
645 }
646 if (buf != NULL) {
647 if (dev->cur_field & 1)
648 buf->top_field = 0;
649 else
650 buf->top_field = 1;
651 }
652
653 dma_q->pos = 0;
654 }
5fee3340
DH
655
656 if (buf != NULL && dev->capture_type == 2) {
657 if (len > 4 && p[0] == 0x88 && p[1] == 0x88 &&
658 p[2] == 0x88 && p[3] == 0x88) {
659 p += 4;
660 len -= 4;
661 }
662 if (len > 4 && p[0] == 0x22 && p[1] == 0x5a) {
663 em28xx_isocdbg("Video frame %d, len=%i, %s\n",
664 p[2], len, (p[2] & 1) ?
665 "odd" : "even");
666 p += 4;
667 len -= 4;
668 }
669
670 if (len > 0)
671 em28xx_copy_video(dev, dma_q, buf, p, outp,
672 len);
673 }
da52a55c
DH
674 }
675 return rc;
676}
677
678
ad0ebb96
MCC
679/* ------------------------------------------------------------------
680 Videobuf operations
681 ------------------------------------------------------------------*/
682
683static int
684buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
685{
686 struct em28xx_fh *fh = vq->priv_data;
d2d9fbfd
MCC
687 struct em28xx *dev = fh->dev;
688 struct v4l2_frequency f;
ad0ebb96 689
e3ba4d34
DH
690 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
691 >> 3;
bddcf633 692
ad0ebb96
MCC
693 if (0 == *count)
694 *count = EM28XX_DEF_BUF;
695
f245e549
MCC
696 if (*count < EM28XX_MIN_BUF)
697 *count = EM28XX_MIN_BUF;
ad0ebb96 698
381aaba9 699 /* Ask tuner to go to analog or radio mode */
6ea54d93 700 memset(&f, 0, sizeof(f));
d2d9fbfd 701 f.frequency = dev->ctl_freq;
381aaba9 702 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
d2d9fbfd 703
f2cf250a 704 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
d2d9fbfd 705
ad0ebb96
MCC
706 return 0;
707}
708
3b5fa928 709/* This is called *without* dev->slock held; please keep it that way */
ad0ebb96
MCC
710static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
711{
3b5fa928
AT
712 struct em28xx_fh *fh = vq->priv_data;
713 struct em28xx *dev = fh->dev;
714 unsigned long flags = 0;
ad0ebb96
MCC
715 if (in_interrupt())
716 BUG();
717
3b5fa928
AT
718 /* We used to wait for the buffer to finish here, but this didn't work
719 because, as we were keeping the state as VIDEOBUF_QUEUED,
720 videobuf_queue_cancel marked it as finished for us.
721 (Also, it could wedge forever if the hardware was misconfigured.)
722
723 This should be safe; by the time we get here, the buffer isn't
724 queued anymore. If we ever start marking the buffers as
725 VIDEOBUF_ACTIVE, it won't be, though.
726 */
727 spin_lock_irqsave(&dev->slock, flags);
28abf083
DH
728 if (dev->isoc_ctl.vid_buf == buf)
729 dev->isoc_ctl.vid_buf = NULL;
3b5fa928
AT
730 spin_unlock_irqrestore(&dev->slock, flags);
731
ad0ebb96
MCC
732 videobuf_vmalloc_free(&buf->vb);
733 buf->vb.state = VIDEOBUF_NEEDS_INIT;
734}
735
736static int
737buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
738 enum v4l2_field field)
739{
740 struct em28xx_fh *fh = vq->priv_data;
f245e549 741 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
ad0ebb96
MCC
742 struct em28xx *dev = fh->dev;
743 int rc = 0, urb_init = 0;
ad0ebb96 744
e3ba4d34
DH
745 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
746 + 7) >> 3;
ad0ebb96
MCC
747
748 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
749 return -EINVAL;
750
05612975
BP
751 buf->vb.width = dev->width;
752 buf->vb.height = dev->height;
753 buf->vb.field = field;
ad0ebb96
MCC
754
755 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
d7aa8020
AT
756 rc = videobuf_iolock(vq, &buf->vb, NULL);
757 if (rc < 0)
ad0ebb96 758 goto fail;
ad0ebb96
MCC
759 }
760
ad0ebb96 761 if (!dev->isoc_ctl.num_bufs)
f245e549 762 urb_init = 1;
ad0ebb96
MCC
763
764 if (urb_init) {
da52a55c
DH
765 if (em28xx_vbi_supported(dev) == 1)
766 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
767 EM28XX_NUM_BUFS,
768 dev->max_pkt_size,
769 em28xx_isoc_copy_vbi);
770 else
771 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
772 EM28XX_NUM_BUFS,
773 dev->max_pkt_size,
774 em28xx_isoc_copy);
f245e549 775 if (rc < 0)
ad0ebb96
MCC
776 goto fail;
777 }
778
779 buf->vb.state = VIDEOBUF_PREPARED;
780 return 0;
781
782fail:
f245e549 783 free_buffer(vq, buf);
ad0ebb96
MCC
784 return rc;
785}
786
787static void
788buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
789{
a1a6ee74
NS
790 struct em28xx_buffer *buf = container_of(vb,
791 struct em28xx_buffer,
792 vb);
ad0ebb96 793 struct em28xx_fh *fh = vq->priv_data;
f245e549 794 struct em28xx *dev = fh->dev;
ad0ebb96 795 struct em28xx_dmaqueue *vidq = &dev->vidq;
ad0ebb96 796
d7aa8020
AT
797 buf->vb.state = VIDEOBUF_QUEUED;
798 list_add_tail(&buf->vb.queue, &vidq->active);
799
ad0ebb96
MCC
800}
801
6ea54d93
DSL
802static void buffer_release(struct videobuf_queue *vq,
803 struct videobuf_buffer *vb)
ad0ebb96 804{
a1a6ee74
NS
805 struct em28xx_buffer *buf = container_of(vb,
806 struct em28xx_buffer,
807 vb);
ad0ebb96 808 struct em28xx_fh *fh = vq->priv_data;
f245e549 809 struct em28xx *dev = (struct em28xx *)fh->dev;
ad0ebb96 810
d7aa8020 811 em28xx_isocdbg("em28xx: called buffer_release\n");
ad0ebb96 812
f245e549 813 free_buffer(vq, buf);
ad0ebb96
MCC
814}
815
816static struct videobuf_queue_ops em28xx_video_qops = {
817 .buf_setup = buffer_setup,
818 .buf_prepare = buffer_prepare,
819 .buf_queue = buffer_queue,
820 .buf_release = buffer_release,
821};
a6c2ba28 822
6ea54d93 823/********************* v4l2 interface **************************************/
a6c2ba28 824
eac94356
MCC
825static void video_mux(struct em28xx *dev, int index)
826{
eac94356
MCC
827 dev->ctl_input = index;
828 dev->ctl_ainput = INPUT(index)->amux;
35ae6f04 829 dev->ctl_aoutput = INPUT(index)->aout;
eac94356 830
e879b8eb
MCC
831 if (!dev->ctl_aoutput)
832 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
833
5325b427
HV
834 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
835 INPUT(index)->vmux, 0, 0);
eac94356 836
505b6d0b 837 if (dev->board.has_msp34xx) {
6ea54d93 838 if (dev->i2s_speed) {
f2cf250a
DSL
839 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
840 s_i2s_clock_freq, dev->i2s_speed);
6ea54d93 841 }
2474ed44 842 /* Note: this is msp3400 specific */
5325b427
HV
843 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
844 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
eac94356 845 }
539c96d0 846
2bd1d9eb 847 if (dev->board.adecoder != EM28XX_NOADECODER) {
5325b427
HV
848 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
849 dev->ctl_ainput, dev->ctl_aoutput, 0);
2bd1d9eb
VW
850 }
851
00b8730f 852 em28xx_audio_analog_set(dev);
eac94356
MCC
853}
854
a225452e 855/* Usage lock check functions */
8c873d31 856static int res_get(struct em28xx_fh *fh, unsigned int bit)
a225452e
MCC
857{
858 struct em28xx *dev = fh->dev;
a225452e 859
8c873d31
DH
860 if (fh->resources & bit)
861 /* have it already allocated */
862 return 1;
a225452e 863
8c873d31
DH
864 /* is it free? */
865 mutex_lock(&dev->lock);
866 if (dev->resources & bit) {
867 /* no, someone else uses it */
868 mutex_unlock(&dev->lock);
869 return 0;
870 }
871 /* it's free, grab it */
872 fh->resources |= bit;
873 dev->resources |= bit;
874 em28xx_videodbg("res: get %d\n", bit);
875 mutex_unlock(&dev->lock);
876 return 1;
877}
a225452e 878
8c873d31
DH
879static int res_check(struct em28xx_fh *fh, unsigned int bit)
880{
e3ba4d34 881 return fh->resources & bit;
a225452e
MCC
882}
883
8c873d31 884static int res_locked(struct em28xx *dev, unsigned int bit)
a225452e 885{
e3ba4d34 886 return dev->resources & bit;
a225452e
MCC
887}
888
8c873d31 889static void res_free(struct em28xx_fh *fh, unsigned int bits)
a225452e
MCC
890{
891 struct em28xx *dev = fh->dev;
892
8c873d31
DH
893 BUG_ON((fh->resources & bits) != bits);
894
895 mutex_lock(&dev->lock);
896 fh->resources &= ~bits;
897 dev->resources &= ~bits;
898 em28xx_videodbg("res: put %d\n", bits);
899 mutex_unlock(&dev->lock);
900}
901
902static int get_ressource(struct em28xx_fh *fh)
903{
904 switch (fh->type) {
905 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
906 return EM28XX_RESOURCE_VIDEO;
907 case V4L2_BUF_TYPE_VBI_CAPTURE:
908 return EM28XX_RESOURCE_VBI;
909 default:
910 BUG();
911 return 0;
912 }
a225452e
MCC
913}
914
195a4ef6 915/*
a98f6af9
MCC
916 * ac97_queryctrl()
917 * return the ac97 supported controls
918 */
919static int ac97_queryctrl(struct v4l2_queryctrl *qc)
920{
921 int i;
922
923 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
924 if (qc->id && qc->id == ac97_qctrl[i].id) {
925 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
926 return 0;
927 }
928 }
929
930 /* Control is not ac97 related */
931 return 1;
932}
933
934/*
935 * ac97_get_ctrl()
936 * return the current values for ac97 mute and volume
195a4ef6 937 */
a98f6af9 938static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
195a4ef6
MCC
939{
940 switch (ctrl->id) {
941 case V4L2_CID_AUDIO_MUTE:
942 ctrl->value = dev->mute;
943 return 0;
944 case V4L2_CID_AUDIO_VOLUME:
945 ctrl->value = dev->volume;
946 return 0;
947 default:
a98f6af9
MCC
948 /* Control is not ac97 related */
949 return 1;
a6c2ba28 950 }
195a4ef6 951}
a6c2ba28 952
195a4ef6 953/*
a98f6af9
MCC
954 * ac97_set_ctrl()
955 * set values for ac97 mute and volume
195a4ef6 956 */
a98f6af9 957static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
195a4ef6 958{
a98f6af9
MCC
959 int i;
960
961 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
962 if (ctrl->id == ac97_qctrl[i].id)
963 goto handle;
964
965 /* Announce that hasn't handle it */
966 return 1;
967
968handle:
969 if (ctrl->value < ac97_qctrl[i].minimum ||
970 ctrl->value > ac97_qctrl[i].maximum)
971 return -ERANGE;
972
195a4ef6
MCC
973 switch (ctrl->id) {
974 case V4L2_CID_AUDIO_MUTE:
a98f6af9
MCC
975 dev->mute = ctrl->value;
976 break;
195a4ef6
MCC
977 case V4L2_CID_AUDIO_VOLUME:
978 dev->volume = ctrl->value;
a98f6af9 979 break;
195a4ef6 980 }
a98f6af9
MCC
981
982 return em28xx_audio_analog_set(dev);
195a4ef6 983}
a6c2ba28 984
195a4ef6
MCC
985static int check_dev(struct em28xx *dev)
986{
987 if (dev->state & DEV_DISCONNECTED) {
988 em28xx_errdev("v4l2 ioctl: device not present\n");
989 return -ENODEV;
e5589bef 990 }
a6c2ba28 991
195a4ef6
MCC
992 if (dev->state & DEV_MISCONFIGURED) {
993 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
994 "close and open it again\n");
995 return -EIO;
996 }
997 return 0;
998}
a6c2ba28 999
195a4ef6
MCC
1000static void get_scale(struct em28xx *dev,
1001 unsigned int width, unsigned int height,
1002 unsigned int *hscale, unsigned int *vscale)
1003{
55699964
MCC
1004 unsigned int maxw = norm_maxw(dev);
1005 unsigned int maxh = norm_maxh(dev);
195a4ef6
MCC
1006
1007 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1008 if (*hscale >= 0x4000)
1009 *hscale = 0x3fff;
1010
1011 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1012 if (*vscale >= 0x4000)
1013 *vscale = 0x3fff;
a6c2ba28 1014}
1015
195a4ef6
MCC
1016/* ------------------------------------------------------------------
1017 IOCTL vidioc handling
1018 ------------------------------------------------------------------*/
1019
78b526a4 1020static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
195a4ef6 1021 struct v4l2_format *f)
a6c2ba28 1022{
195a4ef6
MCC
1023 struct em28xx_fh *fh = priv;
1024 struct em28xx *dev = fh->dev;
a6c2ba28 1025
195a4ef6 1026 mutex_lock(&dev->lock);
e5589bef 1027
195a4ef6
MCC
1028 f->fmt.pix.width = dev->width;
1029 f->fmt.pix.height = dev->height;
bddcf633
MCC
1030 f->fmt.pix.pixelformat = dev->format->fourcc;
1031 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
44dc733c 1032 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
195a4ef6 1033 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
e5589bef 1034
195a4ef6 1035 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
c2a6b54a
MCC
1036 if (dev->progressive)
1037 f->fmt.pix.field = V4L2_FIELD_NONE;
1038 else
1039 f->fmt.pix.field = dev->interlaced ?
195a4ef6 1040 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
e5589bef 1041
195a4ef6
MCC
1042 mutex_unlock(&dev->lock);
1043 return 0;
a6c2ba28 1044}
1045
bddcf633
MCC
1046static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1047{
1048 unsigned int i;
1049
1050 for (i = 0; i < ARRAY_SIZE(format); i++)
1051 if (format[i].fourcc == fourcc)
1052 return &format[i];
1053
1054 return NULL;
1055}
1056
78b526a4 1057static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
195a4ef6 1058 struct v4l2_format *f)
a6c2ba28 1059{
195a4ef6
MCC
1060 struct em28xx_fh *fh = priv;
1061 struct em28xx *dev = fh->dev;
ccb83408
TP
1062 unsigned int width = f->fmt.pix.width;
1063 unsigned int height = f->fmt.pix.height;
195a4ef6
MCC
1064 unsigned int maxw = norm_maxw(dev);
1065 unsigned int maxh = norm_maxh(dev);
1066 unsigned int hscale, vscale;
bddcf633
MCC
1067 struct em28xx_fmt *fmt;
1068
1069 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1070 if (!fmt) {
1071 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1072 f->fmt.pix.pixelformat);
1073 return -EINVAL;
1074 }
195a4ef6 1075
55699964 1076 if (dev->board.is_em2800) {
195a4ef6 1077 /* the em2800 can only scale down to 50% */
ccb83408
TP
1078 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1079 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
ccb83408
TP
1080 } else {
1081 /* width must even because of the YUYV format
1082 height must be even because of interlacing */
e3ba4d34
DH
1083 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1084 1, 0);
195a4ef6 1085 }
a225452e 1086
195a4ef6 1087 get_scale(dev, width, height, &hscale, &vscale);
a6c2ba28 1088
195a4ef6
MCC
1089 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1090 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
a6c2ba28 1091
195a4ef6
MCC
1092 f->fmt.pix.width = width;
1093 f->fmt.pix.height = height;
bddcf633
MCC
1094 f->fmt.pix.pixelformat = fmt->fourcc;
1095 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1096 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
195a4ef6 1097 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
c2a6b54a
MCC
1098 if (dev->progressive)
1099 f->fmt.pix.field = V4L2_FIELD_NONE;
1100 else
1101 f->fmt.pix.field = dev->interlaced ?
1102 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
a6c2ba28 1103
a6c2ba28 1104 return 0;
1105}
1106
ed5f1431
MCC
1107static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1108 unsigned width, unsigned height)
1109{
1110 struct em28xx_fmt *fmt;
1111
ed5f1431
MCC
1112 fmt = format_by_fourcc(fourcc);
1113 if (!fmt)
1114 return -EINVAL;
1115
1116 dev->format = fmt;
1117 dev->width = width;
1118 dev->height = height;
1119
1120 /* set new image size */
1121 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1122
1123 em28xx_set_alternate(dev);
1124 em28xx_resolution_set(dev);
1125
1126 return 0;
1127}
1128
78b526a4 1129static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
195a4ef6 1130 struct v4l2_format *f)
a6c2ba28 1131{
195a4ef6
MCC
1132 struct em28xx_fh *fh = priv;
1133 struct em28xx *dev = fh->dev;
ad0ebb96 1134 int rc;
a6c2ba28 1135
195a4ef6
MCC
1136 rc = check_dev(dev);
1137 if (rc < 0)
1138 return rc;
a225452e 1139
5a80415b
SS
1140 mutex_lock(&dev->lock);
1141
efc52a94
MCC
1142 vidioc_try_fmt_vid_cap(file, priv, f);
1143
05612975
BP
1144 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1145 em28xx_errdev("%s queue busy\n", __func__);
1146 rc = -EBUSY;
1147 goto out;
1148 }
1149
ed5f1431
MCC
1150 rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
1151 f->fmt.pix.width, f->fmt.pix.height);
05612975
BP
1152
1153out:
195a4ef6 1154 mutex_unlock(&dev->lock);
05612975 1155 return rc;
195a4ef6
MCC
1156}
1157
19bf0038
DH
1158static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1159{
1160 struct em28xx_fh *fh = priv;
1161 struct em28xx *dev = fh->dev;
19bf0038
DH
1162 int rc;
1163
1164 rc = check_dev(dev);
1165 if (rc < 0)
1166 return rc;
1167
1168 *norm = dev->norm;
1169
1170 return 0;
1171}
1172
a1a6ee74 1173static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
195a4ef6
MCC
1174{
1175 struct em28xx_fh *fh = priv;
1176 struct em28xx *dev = fh->dev;
1177 struct v4l2_format f;
195a4ef6
MCC
1178 int rc;
1179
1180 rc = check_dev(dev);
1181 if (rc < 0)
1182 return rc;
1183
195a4ef6 1184 mutex_lock(&dev->lock);
7d497f8a 1185 dev->norm = *norm;
a6c2ba28 1186
195a4ef6
MCC
1187 /* Adjusts width/height, if needed */
1188 f.fmt.pix.width = dev->width;
1189 f.fmt.pix.height = dev->height;
78b526a4 1190 vidioc_try_fmt_vid_cap(file, priv, &f);
a6c2ba28 1191
195a4ef6
MCC
1192 /* set new image size */
1193 dev->width = f.fmt.pix.width;
1194 dev->height = f.fmt.pix.height;
195a4ef6 1195 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
a6c2ba28 1196
195a4ef6 1197 em28xx_resolution_set(dev);
f41737ec 1198 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
a6c2ba28 1199
195a4ef6
MCC
1200 mutex_unlock(&dev->lock);
1201 return 0;
1202}
9e31ced8 1203
d96ecda6
MCC
1204static int vidioc_g_parm(struct file *file, void *priv,
1205 struct v4l2_streamparm *p)
1206{
1207 struct em28xx_fh *fh = priv;
1208 struct em28xx *dev = fh->dev;
1209 int rc = 0;
1210
1211 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1212 return -EINVAL;
1213
1214 if (dev->board.is_webcam)
1215 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1216 video, g_parm, p);
1217 else
1218 v4l2_video_std_frame_period(dev->norm,
1219 &p->parm.capture.timeperframe);
1220
1221 return rc;
1222}
1223
1224static int vidioc_s_parm(struct file *file, void *priv,
1225 struct v4l2_streamparm *p)
1226{
1227 struct em28xx_fh *fh = priv;
1228 struct em28xx *dev = fh->dev;
1229
1230 if (!dev->board.is_webcam)
1231 return -EINVAL;
1232
1233 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1234 return -EINVAL;
1235
1236 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1237}
1238
195a4ef6
MCC
1239static const char *iname[] = {
1240 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1241 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1242 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1243 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1244 [EM28XX_VMUX_SVIDEO] = "S-Video",
1245 [EM28XX_VMUX_TELEVISION] = "Television",
1246 [EM28XX_VMUX_CABLE] = "Cable TV",
1247 [EM28XX_VMUX_DVB] = "DVB",
1248 [EM28XX_VMUX_DEBUG] = "for debug only",
1249};
9e31ced8 1250
195a4ef6
MCC
1251static int vidioc_enum_input(struct file *file, void *priv,
1252 struct v4l2_input *i)
1253{
1254 struct em28xx_fh *fh = priv;
1255 struct em28xx *dev = fh->dev;
1256 unsigned int n;
9e31ced8 1257
195a4ef6
MCC
1258 n = i->index;
1259 if (n >= MAX_EM28XX_INPUT)
1260 return -EINVAL;
1261 if (0 == INPUT(n)->type)
1262 return -EINVAL;
9e31ced8 1263
195a4ef6
MCC
1264 i->index = n;
1265 i->type = V4L2_INPUT_TYPE_CAMERA;
a6c2ba28 1266
195a4ef6 1267 strcpy(i->name, iname[INPUT(n)->type]);
a6c2ba28 1268
195a4ef6
MCC
1269 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1270 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1271 i->type = V4L2_INPUT_TYPE_TUNER;
1272
7d497f8a 1273 i->std = dev->vdev->tvnorms;
195a4ef6
MCC
1274
1275 return 0;
a6c2ba28 1276}
1277
195a4ef6 1278static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
a6c2ba28 1279{
195a4ef6
MCC
1280 struct em28xx_fh *fh = priv;
1281 struct em28xx *dev = fh->dev;
a6c2ba28 1282
195a4ef6
MCC
1283 *i = dev->ctl_input;
1284
1285 return 0;
1286}
1287
1288static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1289{
1290 struct em28xx_fh *fh = priv;
1291 struct em28xx *dev = fh->dev;
1292 int rc;
1293
1294 rc = check_dev(dev);
1295 if (rc < 0)
1296 return rc;
1297
1298 if (i >= MAX_EM28XX_INPUT)
1299 return -EINVAL;
1300 if (0 == INPUT(i)->type)
1301 return -EINVAL;
a225452e 1302
24c3c415 1303 dev->ctl_input = i;
195a4ef6 1304
24c3c415
MCC
1305 mutex_lock(&dev->lock);
1306 video_mux(dev, dev->ctl_input);
195a4ef6
MCC
1307 mutex_unlock(&dev->lock);
1308 return 0;
1309}
1310
1311static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1312{
1313 struct em28xx_fh *fh = priv;
1314 struct em28xx *dev = fh->dev;
195a4ef6 1315
6c428b57
MCC
1316 if (!dev->audio_mode.has_audio)
1317 return -EINVAL;
1318
35ae6f04
MCC
1319 switch (a->index) {
1320 case EM28XX_AMUX_VIDEO:
195a4ef6 1321 strcpy(a->name, "Television");
35ae6f04
MCC
1322 break;
1323 case EM28XX_AMUX_LINE_IN:
195a4ef6 1324 strcpy(a->name, "Line In");
35ae6f04
MCC
1325 break;
1326 case EM28XX_AMUX_VIDEO2:
1327 strcpy(a->name, "Television alt");
1328 break;
1329 case EM28XX_AMUX_PHONE:
1330 strcpy(a->name, "Phone");
1331 break;
1332 case EM28XX_AMUX_MIC:
1333 strcpy(a->name, "Mic");
1334 break;
1335 case EM28XX_AMUX_CD:
1336 strcpy(a->name, "CD");
1337 break;
1338 case EM28XX_AMUX_AUX:
1339 strcpy(a->name, "Aux");
1340 break;
1341 case EM28XX_AMUX_PCM_OUT:
1342 strcpy(a->name, "PCM");
1343 break;
1344 default:
1345 return -EINVAL;
1346 }
6ea54d93 1347
35ae6f04 1348 a->index = dev->ctl_ainput;
195a4ef6 1349 a->capability = V4L2_AUDCAP_STEREO;
195a4ef6
MCC
1350
1351 return 0;
1352}
1353
1354static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1355{
1356 struct em28xx_fh *fh = priv;
1357 struct em28xx *dev = fh->dev;
1358
24c3c415 1359
6c428b57
MCC
1360 if (!dev->audio_mode.has_audio)
1361 return -EINVAL;
1362
24c3c415
MCC
1363 if (a->index >= MAX_EM28XX_INPUT)
1364 return -EINVAL;
1365 if (0 == INPUT(a->index)->type)
1366 return -EINVAL;
1367
efc52a94
MCC
1368 mutex_lock(&dev->lock);
1369
35ae6f04
MCC
1370 dev->ctl_ainput = INPUT(a->index)->amux;
1371 dev->ctl_aoutput = INPUT(a->index)->aout;
e879b8eb
MCC
1372
1373 if (!dev->ctl_aoutput)
1374 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
efc52a94
MCC
1375
1376 mutex_unlock(&dev->lock);
195a4ef6
MCC
1377 return 0;
1378}
1379
1380static int vidioc_queryctrl(struct file *file, void *priv,
1381 struct v4l2_queryctrl *qc)
1382{
1383 struct em28xx_fh *fh = priv;
1384 struct em28xx *dev = fh->dev;
1385 int id = qc->id;
195a4ef6
MCC
1386 int rc;
1387
1388 rc = check_dev(dev);
1389 if (rc < 0)
1390 return rc;
1391
1392 memset(qc, 0, sizeof(*qc));
1393
1394 qc->id = id;
1395
a98f6af9
MCC
1396 /* enumberate AC97 controls */
1397 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1398 rc = ac97_queryctrl(qc);
1399 if (!rc)
1400 return 0;
195a4ef6 1401 }
f2cf250a 1402
a98f6af9 1403 /* enumberate V4L2 device controls */
195a4ef6 1404 mutex_lock(&dev->lock);
f2cf250a 1405 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
195a4ef6 1406 mutex_unlock(&dev->lock);
a6c2ba28 1407
195a4ef6
MCC
1408 if (qc->type)
1409 return 0;
1410 else
1411 return -EINVAL;
1412}
a6c2ba28 1413
195a4ef6
MCC
1414static int vidioc_g_ctrl(struct file *file, void *priv,
1415 struct v4l2_control *ctrl)
1416{
1417 struct em28xx_fh *fh = priv;
1418 struct em28xx *dev = fh->dev;
1419 int rc;
a6c2ba28 1420
195a4ef6
MCC
1421 rc = check_dev(dev);
1422 if (rc < 0)
1423 return rc;
b6070f07 1424 rc = 0;
efc52a94 1425
195a4ef6 1426 mutex_lock(&dev->lock);
a6c2ba28 1427
a98f6af9
MCC
1428 /* Set an AC97 control */
1429 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1430 rc = ac97_get_ctrl(dev, ctrl);
1431 else
1432 rc = 1;
1433
1434 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1435 if (rc == 1) {
f2cf250a 1436 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
a98f6af9 1437 rc = 0;
07f7db4c 1438 }
195a4ef6
MCC
1439
1440 mutex_unlock(&dev->lock);
1441 return rc;
1442}
1443
1444static int vidioc_s_ctrl(struct file *file, void *priv,
1445 struct v4l2_control *ctrl)
1446{
1447 struct em28xx_fh *fh = priv;
1448 struct em28xx *dev = fh->dev;
195a4ef6
MCC
1449 int rc;
1450
1451 rc = check_dev(dev);
1452 if (rc < 0)
1453 return rc;
1454
1455 mutex_lock(&dev->lock);
1456
a98f6af9
MCC
1457 /* Set an AC97 control */
1458 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1459 rc = ac97_set_ctrl(dev, ctrl);
1460 else
195a4ef6 1461 rc = 1;
a6c2ba28 1462
73c6f462 1463 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
195a4ef6 1464 if (rc == 1) {
f2cf250a 1465 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
73c6f462
MCC
1466
1467 /*
1468 * In the case of non-AC97 volume controls, we still need
1469 * to do some setups at em28xx, in order to mute/unmute
1470 * and to adjust audio volume. However, the value ranges
1471 * should be checked by the corresponding V4L subdriver.
1472 */
1473 switch (ctrl->id) {
1474 case V4L2_CID_AUDIO_MUTE:
1475 dev->mute = ctrl->value;
1476 rc = em28xx_audio_analog_set(dev);
1477 break;
1478 case V4L2_CID_AUDIO_VOLUME:
1479 dev->volume = ctrl->value;
1480 rc = em28xx_audio_analog_set(dev);
1481 }
195a4ef6
MCC
1482 }
1483
5a80415b 1484 mutex_unlock(&dev->lock);
195a4ef6 1485 return rc;
a6c2ba28 1486}
1487
195a4ef6
MCC
1488static int vidioc_g_tuner(struct file *file, void *priv,
1489 struct v4l2_tuner *t)
a6c2ba28 1490{
195a4ef6
MCC
1491 struct em28xx_fh *fh = priv;
1492 struct em28xx *dev = fh->dev;
1493 int rc;
1494
1495 rc = check_dev(dev);
1496 if (rc < 0)
1497 return rc;
1498
1499 if (0 != t->index)
1500 return -EINVAL;
1501
1502 strcpy(t->name, "Tuner");
0eed42e4 1503 t->type = V4L2_TUNER_ANALOG_TV;
195a4ef6
MCC
1504
1505 mutex_lock(&dev->lock);
f2cf250a 1506 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
195a4ef6 1507 mutex_unlock(&dev->lock);
f2cf250a 1508
195a4ef6 1509 return 0;
a6c2ba28 1510}
1511
195a4ef6
MCC
1512static int vidioc_s_tuner(struct file *file, void *priv,
1513 struct v4l2_tuner *t)
a6c2ba28 1514{
195a4ef6
MCC
1515 struct em28xx_fh *fh = priv;
1516 struct em28xx *dev = fh->dev;
1517 int rc;
63337dd3 1518
195a4ef6
MCC
1519 rc = check_dev(dev);
1520 if (rc < 0)
1521 return rc;
1522
1523 if (0 != t->index)
1524 return -EINVAL;
1525
1526 mutex_lock(&dev->lock);
f2cf250a 1527 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
195a4ef6 1528 mutex_unlock(&dev->lock);
f2cf250a 1529
195a4ef6 1530 return 0;
a6c2ba28 1531}
1532
195a4ef6
MCC
1533static int vidioc_g_frequency(struct file *file, void *priv,
1534 struct v4l2_frequency *f)
1535{
1536 struct em28xx_fh *fh = priv;
1537 struct em28xx *dev = fh->dev;
a6c2ba28 1538
efc52a94 1539 mutex_lock(&dev->lock);
0be43754 1540 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
195a4ef6 1541 f->frequency = dev->ctl_freq;
efc52a94 1542 mutex_unlock(&dev->lock);
195a4ef6
MCC
1543
1544 return 0;
1545}
1546
1547static int vidioc_s_frequency(struct file *file, void *priv,
1548 struct v4l2_frequency *f)
a6c2ba28 1549{
195a4ef6
MCC
1550 struct em28xx_fh *fh = priv;
1551 struct em28xx *dev = fh->dev;
1552 int rc;
9c75541f 1553
195a4ef6
MCC
1554 rc = check_dev(dev);
1555 if (rc < 0)
1556 return rc;
1557
1558 if (0 != f->tuner)
1559 return -EINVAL;
1560
0be43754
MCC
1561 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1562 return -EINVAL;
1563 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
195a4ef6 1564 return -EINVAL;
a225452e
MCC
1565
1566 mutex_lock(&dev->lock);
a3a048ce 1567
195a4ef6 1568 dev->ctl_freq = f->frequency;
f2cf250a 1569 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
a6c2ba28 1570
195a4ef6 1571 mutex_unlock(&dev->lock);
efc52a94 1572
195a4ef6
MCC
1573 return 0;
1574}
a6c2ba28 1575
1e7ad56f
MCC
1576#ifdef CONFIG_VIDEO_ADV_DEBUG
1577static int em28xx_reg_len(int reg)
1578{
1579 switch (reg) {
41facaa4
MCC
1580 case EM28XX_R40_AC97LSB:
1581 case EM28XX_R30_HSCALELOW:
1582 case EM28XX_R32_VSCALELOW:
1e7ad56f
MCC
1583 return 2;
1584 default:
1585 return 1;
1586 }
1587}
1588
14983d81 1589static int vidioc_g_chip_ident(struct file *file, void *priv,
aecde8b5 1590 struct v4l2_dbg_chip_ident *chip)
14983d81
MCC
1591{
1592 struct em28xx_fh *fh = priv;
1593 struct em28xx *dev = fh->dev;
1594
1595 chip->ident = V4L2_IDENT_NONE;
1596 chip->revision = 0;
1597
f2cf250a 1598 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
14983d81
MCC
1599
1600 return 0;
1601}
1602
1603
1e7ad56f 1604static int vidioc_g_register(struct file *file, void *priv,
aecde8b5 1605 struct v4l2_dbg_register *reg)
1e7ad56f
MCC
1606{
1607 struct em28xx_fh *fh = priv;
1608 struct em28xx *dev = fh->dev;
1609 int ret;
1610
aecde8b5 1611 switch (reg->match.type) {
14983d81 1612 case V4L2_CHIP_MATCH_AC97:
531c98e7
MCC
1613 mutex_lock(&dev->lock);
1614 ret = em28xx_read_ac97(dev, reg->reg);
1615 mutex_unlock(&dev->lock);
1616 if (ret < 0)
1617 return ret;
1618
1619 reg->val = ret;
aecde8b5 1620 reg->size = 1;
531c98e7 1621 return 0;
14983d81 1622 case V4L2_CHIP_MATCH_I2C_DRIVER:
f2cf250a 1623 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
14983d81
MCC
1624 return 0;
1625 case V4L2_CHIP_MATCH_I2C_ADDR:
4efa2d75
MCC
1626 /* TODO: is this correct? */
1627 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1628 return 0;
14983d81 1629 default:
aecde8b5 1630 if (!v4l2_chip_match_host(&reg->match))
14983d81
MCC
1631 return -EINVAL;
1632 }
1e7ad56f 1633
14983d81 1634 /* Match host */
aecde8b5
HV
1635 reg->size = em28xx_reg_len(reg->reg);
1636 if (reg->size == 1) {
efc52a94 1637 mutex_lock(&dev->lock);
1e7ad56f 1638 ret = em28xx_read_reg(dev, reg->reg);
efc52a94
MCC
1639 mutex_unlock(&dev->lock);
1640
1e7ad56f
MCC
1641 if (ret < 0)
1642 return ret;
1643
1644 reg->val = ret;
1645 } else {
aecde8b5 1646 __le16 val = 0;
efc52a94 1647 mutex_lock(&dev->lock);
1e7ad56f
MCC
1648 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1649 reg->reg, (char *)&val, 2);
efc52a94 1650 mutex_unlock(&dev->lock);
1e7ad56f
MCC
1651 if (ret < 0)
1652 return ret;
1653
aecde8b5 1654 reg->val = le16_to_cpu(val);
1e7ad56f
MCC
1655 }
1656
1657 return 0;
1658}
1659
1660static int vidioc_s_register(struct file *file, void *priv,
aecde8b5 1661 struct v4l2_dbg_register *reg)
1e7ad56f
MCC
1662{
1663 struct em28xx_fh *fh = priv;
1664 struct em28xx *dev = fh->dev;
aecde8b5 1665 __le16 buf;
efc52a94 1666 int rc;
1e7ad56f 1667
aecde8b5 1668 switch (reg->match.type) {
14983d81 1669 case V4L2_CHIP_MATCH_AC97:
531c98e7
MCC
1670 mutex_lock(&dev->lock);
1671 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1672 mutex_unlock(&dev->lock);
1673
1674 return rc;
14983d81 1675 case V4L2_CHIP_MATCH_I2C_DRIVER:
f2cf250a 1676 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
14983d81
MCC
1677 return 0;
1678 case V4L2_CHIP_MATCH_I2C_ADDR:
4efa2d75
MCC
1679 /* TODO: is this correct? */
1680 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1681 return 0;
14983d81 1682 default:
aecde8b5 1683 if (!v4l2_chip_match_host(&reg->match))
14983d81 1684 return -EINVAL;
531c98e7
MCC
1685 }
1686
14983d81 1687 /* Match host */
aecde8b5 1688 buf = cpu_to_le16(reg->val);
1e7ad56f 1689
efc52a94
MCC
1690 mutex_lock(&dev->lock);
1691 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1692 em28xx_reg_len(reg->reg));
1693 mutex_unlock(&dev->lock);
1694
1695 return rc;
1e7ad56f
MCC
1696}
1697#endif
1698
1699
195a4ef6
MCC
1700static int vidioc_cropcap(struct file *file, void *priv,
1701 struct v4l2_cropcap *cc)
1702{
1703 struct em28xx_fh *fh = priv;
1704 struct em28xx *dev = fh->dev;
1705
1706 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
a6c2ba28 1707 return -EINVAL;
a6c2ba28 1708
195a4ef6
MCC
1709 cc->bounds.left = 0;
1710 cc->bounds.top = 0;
1711 cc->bounds.width = dev->width;
1712 cc->bounds.height = dev->height;
1713 cc->defrect = cc->bounds;
1714 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1715 cc->pixelaspect.denominator = 59;
1716
1717 return 0;
1718}
1719
1720static int vidioc_streamon(struct file *file, void *priv,
1721 enum v4l2_buf_type type)
1722{
1723 struct em28xx_fh *fh = priv;
1724 struct em28xx *dev = fh->dev;
8c873d31 1725 int rc = -EINVAL;
195a4ef6
MCC
1726
1727 rc = check_dev(dev);
1728 if (rc < 0)
1729 return rc;
1730
8c873d31
DH
1731 if (unlikely(type != fh->type))
1732 return -EINVAL;
195a4ef6 1733
8c873d31
DH
1734 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1735 fh, type, fh->resources, dev->resources);
29b59417 1736
e3ba4d34 1737 if (unlikely(!res_get(fh, get_ressource(fh))))
8c873d31 1738 return -EBUSY;
efc52a94 1739
8c873d31
DH
1740 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1741 rc = videobuf_streamon(&fh->vb_vidq);
1742 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1743 rc = videobuf_streamon(&fh->vb_vbiq);
efc52a94
MCC
1744
1745 return rc;
195a4ef6
MCC
1746}
1747
1748static int vidioc_streamoff(struct file *file, void *priv,
1749 enum v4l2_buf_type type)
1750{
1751 struct em28xx_fh *fh = priv;
1752 struct em28xx *dev = fh->dev;
1753 int rc;
1754
1755 rc = check_dev(dev);
1756 if (rc < 0)
1757 return rc;
1758
28abf083
DH
1759 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1760 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
ad0ebb96
MCC
1761 return -EINVAL;
1762 if (type != fh->type)
195a4ef6
MCC
1763 return -EINVAL;
1764
8c873d31
DH
1765 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1766 fh, type, fh->resources, dev->resources);
efc52a94 1767
8c873d31 1768 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
28abf083 1769 videobuf_streamoff(&fh->vb_vidq);
8c873d31
DH
1770 res_free(fh, EM28XX_RESOURCE_VIDEO);
1771 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
28abf083 1772 videobuf_streamoff(&fh->vb_vbiq);
8c873d31
DH
1773 res_free(fh, EM28XX_RESOURCE_VBI);
1774 }
a6c2ba28 1775
a6c2ba28 1776 return 0;
1777}
1778
195a4ef6
MCC
1779static int vidioc_querycap(struct file *file, void *priv,
1780 struct v4l2_capability *cap)
a6c2ba28 1781{
195a4ef6
MCC
1782 struct em28xx_fh *fh = priv;
1783 struct em28xx *dev = fh->dev;
1784
1785 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1786 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
cb97716f 1787 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
195a4ef6
MCC
1788
1789 cap->version = EM28XX_VERSION_CODE;
1790
1791 cap->capabilities =
1792 V4L2_CAP_SLICED_VBI_CAPTURE |
1793 V4L2_CAP_VIDEO_CAPTURE |
195a4ef6
MCC
1794 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1795
0414614a
DH
1796 if (dev->vbi_dev)
1797 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1798
6c428b57
MCC
1799 if (dev->audio_mode.has_audio)
1800 cap->capabilities |= V4L2_CAP_AUDIO;
1801
ed086314 1802 if (dev->tuner_type != TUNER_ABSENT)
195a4ef6
MCC
1803 cap->capabilities |= V4L2_CAP_TUNER;
1804
1805 return 0;
c0477ad9
MCC
1806}
1807
78b526a4 1808static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
bddcf633 1809 struct v4l2_fmtdesc *f)
a6c2ba28 1810{
bddcf633 1811 if (unlikely(f->index >= ARRAY_SIZE(format)))
c0477ad9 1812 return -EINVAL;
195a4ef6 1813
bddcf633
MCC
1814 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1815 f->pixelformat = format[f->index].fourcc;
195a4ef6
MCC
1816
1817 return 0;
c0477ad9
MCC
1818}
1819
195a4ef6 1820/* Sliced VBI ioctls */
78b526a4 1821static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
195a4ef6 1822 struct v4l2_format *f)
a6c2ba28 1823{
195a4ef6
MCC
1824 struct em28xx_fh *fh = priv;
1825 struct em28xx *dev = fh->dev;
1826 int rc;
a6c2ba28 1827
195a4ef6
MCC
1828 rc = check_dev(dev);
1829 if (rc < 0)
1830 return rc;
a6c2ba28 1831
195a4ef6
MCC
1832 mutex_lock(&dev->lock);
1833
1834 f->fmt.sliced.service_set = 0;
4a61ecbd 1835 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
195a4ef6
MCC
1836
1837 if (f->fmt.sliced.service_set == 0)
1838 rc = -EINVAL;
1839
1840 mutex_unlock(&dev->lock);
f2cf250a 1841
195a4ef6
MCC
1842 return rc;
1843}
1844
78b526a4 1845static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
195a4ef6
MCC
1846 struct v4l2_format *f)
1847{
1848 struct em28xx_fh *fh = priv;
1849 struct em28xx *dev = fh->dev;
1850 int rc;
1851
1852 rc = check_dev(dev);
1853 if (rc < 0)
1854 return rc;
1855
1856 mutex_lock(&dev->lock);
4a61ecbd 1857 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
195a4ef6
MCC
1858 mutex_unlock(&dev->lock);
1859
1860 if (f->fmt.sliced.service_set == 0)
1861 return -EINVAL;
a6c2ba28 1862
1863 return 0;
1864}
1865
28abf083
DH
1866/* RAW VBI ioctls */
1867
1868static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1869 struct v4l2_format *format)
1870{
66d9cbad
DH
1871 struct em28xx_fh *fh = priv;
1872 struct em28xx *dev = fh->dev;
1873
1874 format->fmt.vbi.samples_per_line = dev->vbi_width;
28abf083
DH
1875 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1876 format->fmt.vbi.offset = 0;
1877 format->fmt.vbi.flags = 0;
66d9cbad
DH
1878 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1879 format->fmt.vbi.count[0] = dev->vbi_height;
1880 format->fmt.vbi.count[1] = dev->vbi_height;
28abf083
DH
1881
1882 /* Varies by video standard (NTSC, PAL, etc.) */
66d9cbad
DH
1883 if (dev->norm & V4L2_STD_525_60) {
1884 /* NTSC */
1885 format->fmt.vbi.start[0] = 10;
1886 format->fmt.vbi.start[1] = 273;
1887 } else if (dev->norm & V4L2_STD_625_50) {
1888 /* PAL */
1889 format->fmt.vbi.start[0] = 6;
1890 format->fmt.vbi.start[1] = 318;
1891 }
28abf083
DH
1892
1893 return 0;
1894}
1895
1896static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1897 struct v4l2_format *format)
1898{
66d9cbad
DH
1899 struct em28xx_fh *fh = priv;
1900 struct em28xx *dev = fh->dev;
1901
1902 format->fmt.vbi.samples_per_line = dev->vbi_width;
28abf083
DH
1903 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1904 format->fmt.vbi.offset = 0;
1905 format->fmt.vbi.flags = 0;
66d9cbad
DH
1906 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1907 format->fmt.vbi.count[0] = dev->vbi_height;
1908 format->fmt.vbi.count[1] = dev->vbi_height;
28abf083
DH
1909
1910 /* Varies by video standard (NTSC, PAL, etc.) */
66d9cbad
DH
1911 if (dev->norm & V4L2_STD_525_60) {
1912 /* NTSC */
1913 format->fmt.vbi.start[0] = 10;
1914 format->fmt.vbi.start[1] = 273;
1915 } else if (dev->norm & V4L2_STD_625_50) {
1916 /* PAL */
1917 format->fmt.vbi.start[0] = 6;
1918 format->fmt.vbi.start[1] = 318;
1919 }
28abf083
DH
1920
1921 return 0;
1922}
195a4ef6
MCC
1923
1924static int vidioc_reqbufs(struct file *file, void *priv,
1925 struct v4l2_requestbuffers *rb)
a6c2ba28 1926{
195a4ef6
MCC
1927 struct em28xx_fh *fh = priv;
1928 struct em28xx *dev = fh->dev;
195a4ef6 1929 int rc;
a6c2ba28 1930
195a4ef6
MCC
1931 rc = check_dev(dev);
1932 if (rc < 0)
1933 return rc;
a6c2ba28 1934
28abf083
DH
1935 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1936 return videobuf_reqbufs(&fh->vb_vidq, rb);
1937 else
1938 return videobuf_reqbufs(&fh->vb_vbiq, rb);
a6c2ba28 1939}
1940
195a4ef6
MCC
1941static int vidioc_querybuf(struct file *file, void *priv,
1942 struct v4l2_buffer *b)
e5589bef 1943{
195a4ef6
MCC
1944 struct em28xx_fh *fh = priv;
1945 struct em28xx *dev = fh->dev;
1946 int rc;
e5589bef 1947
195a4ef6
MCC
1948 rc = check_dev(dev);
1949 if (rc < 0)
1950 return rc;
2d50f847 1951
28abf083
DH
1952 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1953 return videobuf_querybuf(&fh->vb_vidq, b);
1954 else {
1955 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1956 the videobuf framework, but we probably shouldn't be
1957 returning a buffer larger than that which was asked for.
1958 At a minimum, it causes a crash in zvbi since it does
1959 a memcpy based on the source buffer length */
1960 int result = videobuf_querybuf(&fh->vb_vbiq, b);
66d9cbad
DH
1961 b->length = dev->vbi_width * dev->vbi_height * 2;
1962
28abf083
DH
1963 return result;
1964 }
195a4ef6
MCC
1965}
1966
1967static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1968{
1969 struct em28xx_fh *fh = priv;
1970 struct em28xx *dev = fh->dev;
195a4ef6
MCC
1971 int rc;
1972
1973 rc = check_dev(dev);
1974 if (rc < 0)
1975 return rc;
1976
28abf083
DH
1977 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1978 return videobuf_qbuf(&fh->vb_vidq, b);
e3ba4d34 1979 else
28abf083 1980 return videobuf_qbuf(&fh->vb_vbiq, b);
e5589bef
MCC
1981}
1982
195a4ef6 1983static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2d50f847 1984{
195a4ef6
MCC
1985 struct em28xx_fh *fh = priv;
1986 struct em28xx *dev = fh->dev;
1987 int rc;
195a4ef6
MCC
1988
1989 rc = check_dev(dev);
1990 if (rc < 0)
1991 return rc;
2d50f847 1992
28abf083
DH
1993 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1994 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1995 O_NONBLOCK);
1996 else
1997 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1998 O_NONBLOCK);
ad0ebb96 1999}
2d50f847 2000
ad0ebb96 2001#ifdef CONFIG_VIDEO_V4L1_COMPAT
f245e549 2002static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
ad0ebb96 2003{
f245e549 2004 struct em28xx_fh *fh = priv;
2d50f847 2005
28abf083
DH
2006 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2007 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
2008 else
2009 return videobuf_cgmbuf(&fh->vb_vbiq, mbuf, 8);
195a4ef6 2010}
ad0ebb96
MCC
2011#endif
2012
2d50f847 2013
0be43754
MCC
2014/* ----------------------------------------------------------- */
2015/* RADIO ESPECIFIC IOCTLS */
2016/* ----------------------------------------------------------- */
2017
2018static int radio_querycap(struct file *file, void *priv,
2019 struct v4l2_capability *cap)
2020{
2021 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2022
2023 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2024 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
cb97716f 2025 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
0be43754
MCC
2026
2027 cap->version = EM28XX_VERSION_CODE;
2028 cap->capabilities = V4L2_CAP_TUNER;
2029 return 0;
2030}
2031
2032static int radio_g_tuner(struct file *file, void *priv,
2033 struct v4l2_tuner *t)
2034{
2035 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2036
2037 if (unlikely(t->index > 0))
2038 return -EINVAL;
2039
2040 strcpy(t->name, "Radio");
2041 t->type = V4L2_TUNER_RADIO;
2042
efc52a94 2043 mutex_lock(&dev->lock);
f2cf250a 2044 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
efc52a94
MCC
2045 mutex_unlock(&dev->lock);
2046
0be43754
MCC
2047 return 0;
2048}
2049
2050static int radio_enum_input(struct file *file, void *priv,
2051 struct v4l2_input *i)
2052{
2053 if (i->index != 0)
2054 return -EINVAL;
2055 strcpy(i->name, "Radio");
2056 i->type = V4L2_INPUT_TYPE_TUNER;
2057
2058 return 0;
2059}
2060
2061static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2062{
2063 if (unlikely(a->index))
2064 return -EINVAL;
2065
2066 strcpy(a->name, "Radio");
2067 return 0;
2068}
2069
2070static int radio_s_tuner(struct file *file, void *priv,
2071 struct v4l2_tuner *t)
2072{
2073 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2074
2075 if (0 != t->index)
2076 return -EINVAL;
2077
efc52a94 2078 mutex_lock(&dev->lock);
f2cf250a 2079 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
efc52a94 2080 mutex_unlock(&dev->lock);
0be43754
MCC
2081
2082 return 0;
2083}
2084
2085static int radio_s_audio(struct file *file, void *fh,
2086 struct v4l2_audio *a)
2087{
2088 return 0;
2089}
2090
2091static int radio_s_input(struct file *file, void *fh, unsigned int i)
2092{
2093 return 0;
2094}
2095
2096static int radio_queryctrl(struct file *file, void *priv,
2097 struct v4l2_queryctrl *qc)
2098{
2099 int i;
2100
2101 if (qc->id < V4L2_CID_BASE ||
2102 qc->id >= V4L2_CID_LASTP1)
2103 return -EINVAL;
2104
ed10daae
MCC
2105 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2106 if (qc->id && qc->id == ac97_qctrl[i].id) {
2107 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
0be43754
MCC
2108 return 0;
2109 }
2110 }
2111
2112 return -EINVAL;
2113}
2114
195a4ef6
MCC
2115/*
2116 * em28xx_v4l2_open()
2117 * inits the device and starts isoc transfer
2118 */
bec43661 2119static int em28xx_v4l2_open(struct file *filp)
195a4ef6 2120{
63b0d5ad
LP
2121 int errCode = 0, radio = 0;
2122 struct video_device *vdev = video_devdata(filp);
2123 struct em28xx *dev = video_drvdata(filp);
2124 enum v4l2_buf_type fh_type = 0;
195a4ef6 2125 struct em28xx_fh *fh;
c2a6b54a 2126 enum v4l2_field field;
2d50f847 2127
63b0d5ad
LP
2128 switch (vdev->vfl_type) {
2129 case VFL_TYPE_GRABBER:
2130 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2131 break;
2132 case VFL_TYPE_VBI:
2133 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2134 break;
2135 case VFL_TYPE_RADIO:
2136 radio = 1;
2137 break;
2138 }
818a557e
MCC
2139
2140 mutex_lock(&dev->lock);
2d50f847 2141
50462eb0
LP
2142 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2143 video_device_node_name(vdev), v4l2_type_names[fh_type],
2144 dev->users);
2d50f847 2145
9aeb4b05 2146
c67ec53f 2147 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
195a4ef6
MCC
2148 if (!fh) {
2149 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
818a557e 2150 mutex_unlock(&dev->lock);
195a4ef6
MCC
2151 return -ENOMEM;
2152 }
195a4ef6 2153 fh->dev = dev;
0be43754 2154 fh->radio = radio;
d7aa8020 2155 fh->type = fh_type;
195a4ef6 2156 filp->private_data = fh;
9aeb4b05 2157
d7aa8020 2158 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
c67ec53f 2159 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
3687e1e6 2160 em28xx_set_alternate(dev);
195a4ef6 2161 em28xx_resolution_set(dev);
2d50f847 2162
c67ec53f
MCC
2163 /* Needed, since GPIO might have disabled power of
2164 some i2c device
2165 */
1a23f81b 2166 em28xx_wake_i2c(dev);
c67ec53f 2167
2d50f847 2168 }
0be43754
MCC
2169 if (fh->radio) {
2170 em28xx_videodbg("video_open: setting radio device\n");
f2cf250a 2171 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
0be43754 2172 }
2d50f847 2173
195a4ef6 2174 dev->users++;
2d50f847 2175
c2a6b54a
MCC
2176 if (dev->progressive)
2177 field = V4L2_FIELD_NONE;
2178 else
2179 field = V4L2_FIELD_INTERLACED;
2180
8c873d31
DH
2181 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2182 NULL, &dev->slock,
2183 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
2184 sizeof(struct em28xx_buffer), fh);
28abf083 2185
8c873d31
DH
2186 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2187 NULL, &dev->slock,
2188 V4L2_BUF_TYPE_VBI_CAPTURE,
2189 V4L2_FIELD_SEQ_TB,
2190 sizeof(struct em28xx_buffer), fh);
ad0ebb96 2191
195a4ef6 2192 mutex_unlock(&dev->lock);
c67ec53f 2193
195a4ef6 2194 return errCode;
2d50f847 2195}
e5589bef 2196
a6c2ba28 2197/*
195a4ef6
MCC
2198 * em28xx_realease_resources()
2199 * unregisters the v4l2,i2c and usb devices
2200 * called when the device gets disconected or at module unload
2201*/
1a23f81b 2202void em28xx_release_analog_resources(struct em28xx *dev)
a6c2ba28 2203{
a6c2ba28 2204
195a4ef6 2205 /*FIXME: I2C IR should be disconnected */
a6c2ba28 2206
0be43754 2207 if (dev->radio_dev) {
f0813b4c 2208 if (video_is_registered(dev->radio_dev))
0be43754
MCC
2209 video_unregister_device(dev->radio_dev);
2210 else
2211 video_device_release(dev->radio_dev);
2212 dev->radio_dev = NULL;
2213 }
2214 if (dev->vbi_dev) {
38c7c036
LP
2215 em28xx_info("V4L2 device %s deregistered\n",
2216 video_device_node_name(dev->vbi_dev));
f0813b4c 2217 if (video_is_registered(dev->vbi_dev))
0be43754
MCC
2218 video_unregister_device(dev->vbi_dev);
2219 else
2220 video_device_release(dev->vbi_dev);
2221 dev->vbi_dev = NULL;
2222 }
2223 if (dev->vdev) {
38c7c036
LP
2224 em28xx_info("V4L2 device %s deregistered\n",
2225 video_device_node_name(dev->vdev));
f0813b4c 2226 if (video_is_registered(dev->vdev))
0be43754
MCC
2227 video_unregister_device(dev->vdev);
2228 else
2229 video_device_release(dev->vdev);
2230 dev->vdev = NULL;
2231 }
195a4ef6 2232}
a6c2ba28 2233
195a4ef6
MCC
2234/*
2235 * em28xx_v4l2_close()
6ea54d93
DSL
2236 * stops streaming and deallocates all resources allocated by the v4l2
2237 * calls and ioctls
195a4ef6 2238 */
bec43661 2239static int em28xx_v4l2_close(struct file *filp)
195a4ef6
MCC
2240{
2241 struct em28xx_fh *fh = filp->private_data;
2242 struct em28xx *dev = fh->dev;
2243 int errCode;
a6c2ba28 2244
195a4ef6 2245 em28xx_videodbg("users=%d\n", dev->users);
a6c2ba28 2246
8c873d31 2247 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
ad0ebb96 2248 videobuf_stop(&fh->vb_vidq);
8c873d31
DH
2249 res_free(fh, EM28XX_RESOURCE_VIDEO);
2250 }
a6c2ba28 2251
8c873d31
DH
2252 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2253 videobuf_stop(&fh->vb_vbiq);
2254 res_free(fh, EM28XX_RESOURCE_VBI);
2255 }
2256
e3ba4d34 2257 if (dev->users == 1) {
195a4ef6
MCC
2258 /* the device is already disconnect,
2259 free the remaining resources */
2260 if (dev->state & DEV_DISCONNECTED) {
2261 em28xx_release_resources(dev);
195a4ef6
MCC
2262 kfree(dev);
2263 return 0;
2264 }
a6c2ba28 2265
eb6c9634 2266 /* Save some power by putting tuner to sleep */
622b828a 2267 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
eb6c9634 2268
d7aa8020
AT
2269 /* do this before setting alternate! */
2270 em28xx_uninit_isoc(dev);
2fe3e2ee 2271 em28xx_set_mode(dev, EM28XX_SUSPEND);
d7aa8020 2272
195a4ef6
MCC
2273 /* set alternate 0 */
2274 dev->alt = 0;
2275 em28xx_videodbg("setting alternate 0\n");
2276 errCode = usb_set_interface(dev->udev, 0, 0);
2277 if (errCode < 0) {
2278 em28xx_errdev("cannot change alternate number to "
2279 "0 (error=%i)\n", errCode);
2280 }
9aeb4b05 2281 }
28abf083 2282
8c873d31
DH
2283 videobuf_mmap_free(&fh->vb_vidq);
2284 videobuf_mmap_free(&fh->vb_vbiq);
195a4ef6
MCC
2285 kfree(fh);
2286 dev->users--;
2287 wake_up_interruptible_nr(&dev->open, 1);
195a4ef6
MCC
2288 return 0;
2289}
a6c2ba28 2290
195a4ef6
MCC
2291/*
2292 * em28xx_v4l2_read()
2293 * will allocate buffers when called for the first time
2294 */
2295static ssize_t
6ea54d93 2296em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
f245e549 2297 loff_t *pos)
195a4ef6 2298{
195a4ef6
MCC
2299 struct em28xx_fh *fh = filp->private_data;
2300 struct em28xx *dev = fh->dev;
ad0ebb96
MCC
2301 int rc;
2302
2303 rc = check_dev(dev);
2304 if (rc < 0)
2305 return rc;
a6c2ba28 2306
195a4ef6
MCC
2307 /* FIXME: read() is not prepared to allow changing the video
2308 resolution while streaming. Seems a bug at em28xx_set_fmt
2309 */
a6c2ba28 2310
ad0ebb96 2311 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
8c873d31
DH
2312 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
2313 return -EBUSY;
195a4ef6 2314
ad0ebb96
MCC
2315 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2316 filp->f_flags & O_NONBLOCK);
195a4ef6 2317 }
28abf083
DH
2318
2319
2320 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
8c873d31
DH
2321 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2322 return -EBUSY;
28abf083
DH
2323
2324 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
2325 filp->f_flags & O_NONBLOCK);
2326 }
2327
ad0ebb96 2328 return 0;
a6c2ba28 2329}
2330
2331/*
195a4ef6
MCC
2332 * em28xx_v4l2_poll()
2333 * will allocate buffers when called for the first time
a6c2ba28 2334 */
a1a6ee74 2335static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
a6c2ba28 2336{
a3a048ce 2337 struct em28xx_fh *fh = filp->private_data;
195a4ef6 2338 struct em28xx *dev = fh->dev;
ad0ebb96
MCC
2339 int rc;
2340
2341 rc = check_dev(dev);
2342 if (rc < 0)
2343 return rc;
a6c2ba28 2344
8c873d31
DH
2345 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2346 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2347 return POLLERR;
28abf083 2348 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
8c873d31
DH
2349 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2350 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2351 return POLLERR;
28abf083 2352 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
8c873d31 2353 } else {
ad0ebb96 2354 return POLLERR;
8c873d31 2355 }
195a4ef6 2356}
a6c2ba28 2357
195a4ef6
MCC
2358/*
2359 * em28xx_v4l2_mmap()
2360 */
2361static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2362{
2363 struct em28xx_fh *fh = filp->private_data;
2364 struct em28xx *dev = fh->dev;
ad0ebb96 2365 int rc;
a6c2ba28 2366
ad0ebb96
MCC
2367 rc = check_dev(dev);
2368 if (rc < 0)
2369 return rc;
a6c2ba28 2370
91f6dcec
DH
2371 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2372 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2373 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2374 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
a6c2ba28 2375
ad0ebb96
MCC
2376 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2377 (unsigned long)vma->vm_start,
2378 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2379 rc);
a6c2ba28 2380
ad0ebb96 2381 return rc;
a6c2ba28 2382}
2383
bec43661 2384static const struct v4l2_file_operations em28xx_v4l_fops = {
195a4ef6
MCC
2385 .owner = THIS_MODULE,
2386 .open = em28xx_v4l2_open,
2387 .release = em28xx_v4l2_close,
2388 .read = em28xx_v4l2_read,
2389 .poll = em28xx_v4l2_poll,
2390 .mmap = em28xx_v4l2_mmap,
2391 .ioctl = video_ioctl2,
195a4ef6 2392};
17cbe2e5 2393
a399810c 2394static const struct v4l2_ioctl_ops video_ioctl_ops = {
195a4ef6 2395 .vidioc_querycap = vidioc_querycap,
78b526a4
HV
2396 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2397 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2398 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2399 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
28abf083
DH
2400 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2401 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
195a4ef6
MCC
2402 .vidioc_g_audio = vidioc_g_audio,
2403 .vidioc_s_audio = vidioc_s_audio,
2404 .vidioc_cropcap = vidioc_cropcap,
2405
78b526a4
HV
2406 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2407 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2408 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
195a4ef6
MCC
2409
2410 .vidioc_reqbufs = vidioc_reqbufs,
2411 .vidioc_querybuf = vidioc_querybuf,
2412 .vidioc_qbuf = vidioc_qbuf,
2413 .vidioc_dqbuf = vidioc_dqbuf,
19bf0038 2414 .vidioc_g_std = vidioc_g_std,
195a4ef6 2415 .vidioc_s_std = vidioc_s_std,
d96ecda6
MCC
2416 .vidioc_g_parm = vidioc_g_parm,
2417 .vidioc_s_parm = vidioc_s_parm,
195a4ef6
MCC
2418 .vidioc_enum_input = vidioc_enum_input,
2419 .vidioc_g_input = vidioc_g_input,
2420 .vidioc_s_input = vidioc_s_input,
2421 .vidioc_queryctrl = vidioc_queryctrl,
2422 .vidioc_g_ctrl = vidioc_g_ctrl,
2423 .vidioc_s_ctrl = vidioc_s_ctrl,
2424 .vidioc_streamon = vidioc_streamon,
2425 .vidioc_streamoff = vidioc_streamoff,
2426 .vidioc_g_tuner = vidioc_g_tuner,
2427 .vidioc_s_tuner = vidioc_s_tuner,
2428 .vidioc_g_frequency = vidioc_g_frequency,
2429 .vidioc_s_frequency = vidioc_s_frequency,
1e7ad56f
MCC
2430#ifdef CONFIG_VIDEO_ADV_DEBUG
2431 .vidioc_g_register = vidioc_g_register,
2432 .vidioc_s_register = vidioc_s_register,
14983d81 2433 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1e7ad56f 2434#endif
ad0ebb96
MCC
2435#ifdef CONFIG_VIDEO_V4L1_COMPAT
2436 .vidiocgmbuf = vidiocgmbuf,
2437#endif
a399810c
HV
2438};
2439
2440static const struct video_device em28xx_video_template = {
2441 .fops = &em28xx_v4l_fops,
2442 .release = video_device_release,
2443 .ioctl_ops = &video_ioctl_ops,
2444
195a4ef6 2445 .tvnorms = V4L2_STD_ALL,
19bf0038 2446 .current_norm = V4L2_STD_PAL,
a6c2ba28 2447};
2448
bec43661 2449static const struct v4l2_file_operations radio_fops = {
a399810c
HV
2450 .owner = THIS_MODULE,
2451 .open = em28xx_v4l2_open,
2452 .release = em28xx_v4l2_close,
2453 .ioctl = video_ioctl2,
a399810c
HV
2454};
2455
2456static const struct v4l2_ioctl_ops radio_ioctl_ops = {
0be43754
MCC
2457 .vidioc_querycap = radio_querycap,
2458 .vidioc_g_tuner = radio_g_tuner,
2459 .vidioc_enum_input = radio_enum_input,
2460 .vidioc_g_audio = radio_g_audio,
2461 .vidioc_s_tuner = radio_s_tuner,
2462 .vidioc_s_audio = radio_s_audio,
2463 .vidioc_s_input = radio_s_input,
2464 .vidioc_queryctrl = radio_queryctrl,
2465 .vidioc_g_ctrl = vidioc_g_ctrl,
2466 .vidioc_s_ctrl = vidioc_s_ctrl,
2467 .vidioc_g_frequency = vidioc_g_frequency,
2468 .vidioc_s_frequency = vidioc_s_frequency,
1e7ad56f
MCC
2469#ifdef CONFIG_VIDEO_ADV_DEBUG
2470 .vidioc_g_register = vidioc_g_register,
2471 .vidioc_s_register = vidioc_s_register,
2472#endif
0be43754
MCC
2473};
2474
a399810c
HV
2475static struct video_device em28xx_radio_template = {
2476 .name = "em28xx-radio",
a399810c
HV
2477 .fops = &radio_fops,
2478 .ioctl_ops = &radio_ioctl_ops,
a399810c
HV
2479};
2480
6ea54d93 2481/******************************** usb interface ******************************/
a6c2ba28 2482
6d79468d 2483
6d79468d 2484
532fe652 2485static struct video_device *em28xx_vdev_init(struct em28xx *dev,
a1a6ee74
NS
2486 const struct video_device *template,
2487 const char *type_name)
0be43754
MCC
2488{
2489 struct video_device *vfd;
2490
2491 vfd = video_device_alloc();
2492 if (NULL == vfd)
2493 return NULL;
f2cf250a
DSL
2494
2495 *vfd = *template;
f2cf250a
DSL
2496 vfd->v4l2_dev = &dev->v4l2_dev;
2497 vfd->release = video_device_release;
2498 vfd->debug = video_debug;
0be43754
MCC
2499
2500 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2501 dev->name, type_name);
2502
63b0d5ad 2503 video_set_drvdata(vfd, dev);
0be43754
MCC
2504 return vfd;
2505}
2506
2e5ef2df 2507int em28xx_register_analog_devices(struct em28xx *dev)
1a23f81b 2508{
6e7b9ea0 2509 u8 val;
2e5ef2df
MCC
2510 int ret;
2511
1a23f81b
MCC
2512 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
2513 dev->name,
2514 (EM28XX_VERSION_CODE >> 16) & 0xff,
2515 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2516
24c3c415
MCC
2517 /* set default norm */
2518 dev->norm = em28xx_video_template.current_norm;
24c3c415 2519 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
24c3c415
MCC
2520 dev->ctl_input = 0;
2521
1a23f81b
MCC
2522 /* Analog specific initialization */
2523 dev->format = &format[0];
ed5f1431
MCC
2524 em28xx_set_video_format(dev, format[0].fourcc,
2525 norm_maxw(dev), norm_maxh(dev));
2526
24c3c415
MCC
2527 video_mux(dev, dev->ctl_input);
2528
2529 /* Audio defaults */
2530 dev->mute = 1;
2531 dev->volume = 0x1f;
1a23f81b 2532
1a23f81b 2533/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
a1a6ee74
NS
2534 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2535 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2536 (EM28XX_XCLK_AUDIO_UNMUTE | val));
1a23f81b 2537
1a23f81b
MCC
2538 em28xx_set_outfmt(dev);
2539 em28xx_colorlevels_set_default(dev);
2540 em28xx_compression_disable(dev);
2541
818a557e
MCC
2542 /* allocate and fill video video_device struct */
2543 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2544 if (!dev->vdev) {
2545 em28xx_errdev("cannot allocate video_device.\n");
2546 return -ENODEV;
2547 }
2548
2549 /* register v4l2 video video_device */
2550 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2551 video_nr[dev->devno]);
2552 if (ret) {
2553 em28xx_errdev("unable to register video device (error=%i).\n",
2554 ret);
2555 return ret;
2556 }
2557
2558 /* Allocate and fill vbi video_device struct */
290c0cfa
DH
2559 if (em28xx_vbi_supported(dev) == 1) {
2560 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2561 "vbi");
818a557e 2562
290c0cfa
DH
2563 /* register v4l2 vbi video_device */
2564 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2565 vbi_nr[dev->devno]);
2566 if (ret < 0) {
2567 em28xx_errdev("unable to register vbi device\n");
2568 return ret;
2569 }
818a557e
MCC
2570 }
2571
2572 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
a1a6ee74
NS
2573 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2574 "radio");
818a557e
MCC
2575 if (!dev->radio_dev) {
2576 em28xx_errdev("cannot allocate video_device.\n");
2577 return -ENODEV;
2578 }
2579 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2580 radio_nr[dev->devno]);
2581 if (ret < 0) {
2582 em28xx_errdev("can't register radio device\n");
2583 return ret;
2584 }
38c7c036
LP
2585 em28xx_info("Registered radio device as %s\n",
2586 video_device_node_name(dev->radio_dev));
818a557e
MCC
2587 }
2588
38c7c036
LP
2589 em28xx_info("V4L2 video device registered as %s\n",
2590 video_device_node_name(dev->vdev));
290c0cfa
DH
2591
2592 if (dev->vbi_dev)
38c7c036
LP
2593 em28xx_info("V4L2 VBI device registered as %s\n",
2594 video_device_node_name(dev->vbi_dev));
818a557e
MCC
2595
2596 return 0;
2597}