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