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