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