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