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