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