]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - drivers/media/video/em28xx/em28xx-video.c
V4L/DVB (12169): em28xx-video: fix VIDIOC_G_FMT and VIDIOC_ENUMFMT with webcams
[mirror_ubuntu-kernels.git] / drivers / media / video / em28xx / em28xx-video.c
CommitLineData
a6c2ba28 1/*
6ea54d93
DSL
2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
a6c2ba28 4
f7abcd38
MCC
5 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
2e7c6dc3 7 Mauro Carvalho Chehab <mchehab@infradead.org>
f7abcd38 8 Sascha Sommer <saschasommer@freenet.de>
a6c2ba28 9
439090d7
MCC
10 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
12
a6c2ba28 13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
31#include <linux/kernel.h>
e5589bef 32#include <linux/bitmap.h>
a6c2ba28 33#include <linux/usb.h>
a6c2ba28 34#include <linux/i2c.h>
b296fc60 35#include <linux/version.h>
6d35c8f6 36#include <linux/mm.h>
1e4baed3 37#include <linux/mutex.h>
a6c2ba28 38
f7abcd38 39#include "em28xx.h"
c0477ad9 40#include <media/v4l2-common.h>
35ea11ff 41#include <media/v4l2-ioctl.h>
14983d81 42#include <media/v4l2-chip-ident.h>
2474ed44 43#include <media/msp3400.h>
ed086314 44#include <media/tuner.h>
a6c2ba28 45
f7abcd38
MCC
46#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
47 "Markus Rechberger <mrechberger@gmail.com>, " \
2e7c6dc3 48 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
f7abcd38 49 "Sascha Sommer <saschasommer@freenet.de>"
a6c2ba28 50
f7abcd38 51#define DRIVER_DESC "Empia em28xx based USB video device driver"
f2cf250a 52#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
a6c2ba28 53
3acf2809 54#define em28xx_videodbg(fmt, arg...) do {\
4ac97914
MCC
55 if (video_debug) \
56 printk(KERN_INFO "%s %s :"fmt, \
d80e134d 57 dev->name, __func__ , ##arg); } while (0)
a6c2ba28 58
ad0ebb96 59static unsigned int isoc_debug;
f245e549
MCC
60module_param(isoc_debug, int, 0644);
61MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
ad0ebb96 62
6ea54d93
DSL
63#define em28xx_isocdbg(fmt, arg...) \
64do {\
65 if (isoc_debug) { \
ad0ebb96 66 printk(KERN_INFO "%s %s :"fmt, \
6ea54d93
DSL
67 dev->name, __func__ , ##arg); \
68 } \
69 } while (0)
ad0ebb96 70
a6c2ba28 71MODULE_AUTHOR(DRIVER_AUTHOR);
72MODULE_DESCRIPTION(DRIVER_DESC);
73MODULE_LICENSE("GPL");
74
e5589bef 75static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
0be43754
MCC
76static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
77static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
78
e5589bef
MCC
79module_param_array(video_nr, int, NULL, 0444);
80module_param_array(vbi_nr, int, NULL, 0444);
0be43754 81module_param_array(radio_nr, int, NULL, 0444);
0be43754
MCC
82MODULE_PARM_DESC(video_nr, "video device numbers");
83MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
84MODULE_PARM_DESC(radio_nr, "radio device numbers");
596d92d5 85
ff699e6b 86static unsigned int video_debug;
6ea54d93
DSL
87module_param(video_debug, int, 0644);
88MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
a6c2ba28 89
bddcf633
MCC
90/* supported video standards */
91static struct em28xx_fmt format[] = {
92 {
93 .name = "16bpp YUY2, 4:2:2, packed",
94 .fourcc = V4L2_PIX_FMT_YUYV,
95 .depth = 16,
3fbf9309 96 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
43cb9fe3
MCC
97 }, {
98 .name = "16 bpp RGB, le",
99 .fourcc = V4L2_PIX_FMT_RGB565,
100 .depth = 16,
101 .reg = EM28XX_OUTFMT_YUV211,
bddcf633
MCC
102 },
103};
104
a6c2ba28 105/* supported controls */
c0477ad9 106/* Common to all boards */
3acf2809 107static struct v4l2_queryctrl em28xx_qctrl[] = {
c0477ad9
MCC
108 {
109 .id = V4L2_CID_AUDIO_VOLUME,
110 .type = V4L2_CTRL_TYPE_INTEGER,
111 .name = "Volume",
112 .minimum = 0x0,
113 .maximum = 0x1f,
114 .step = 0x1,
115 .default_value = 0x1f,
116 .flags = 0,
6ea54d93 117 }, {
c0477ad9
MCC
118 .id = V4L2_CID_AUDIO_MUTE,
119 .type = V4L2_CTRL_TYPE_BOOLEAN,
120 .name = "Mute",
121 .minimum = 0,
122 .maximum = 1,
123 .step = 1,
124 .default_value = 1,
125 .flags = 0,
126 }
127};
128
ad0ebb96
MCC
129/* ------------------------------------------------------------------
130 DMA and thread functions
131 ------------------------------------------------------------------*/
132
133/*
134 * Announces that a buffer were filled and request the next
135 */
f245e549 136static inline void buffer_filled(struct em28xx *dev,
ad0ebb96
MCC
137 struct em28xx_dmaqueue *dma_q,
138 struct em28xx_buffer *buf)
139{
ad0ebb96
MCC
140 /* Advice that buffer was filled */
141 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
142 buf->vb.state = VIDEOBUF_DONE;
143 buf->vb.field_count++;
144 do_gettimeofday(&buf->vb.ts);
145
cb784724
MCC
146 dev->isoc_ctl.buf = NULL;
147
ad0ebb96
MCC
148 list_del(&buf->vb.queue);
149 wake_up(&buf->vb.done);
150}
151
152/*
153 * Identify the buffer header type and properly handles
154 */
155static void em28xx_copy_video(struct em28xx *dev,
156 struct em28xx_dmaqueue *dma_q,
157 struct em28xx_buffer *buf,
158 unsigned char *p,
159 unsigned char *outp, unsigned long len)
160{
161 void *fieldstart, *startwrite, *startread;
f245e549 162 int linesdone, currlinedone, offset, lencopy, remain;
44dc733c 163 int bytesperline = dev->width << 1;
ad0ebb96
MCC
164
165 if (dma_q->pos + len > buf->vb.size)
166 len = buf->vb.size - dma_q->pos;
167
d7aa8020 168 if (p[0] != 0x88 && p[0] != 0x22) {
ad0ebb96
MCC
169 em28xx_isocdbg("frame is not complete\n");
170 len += 4;
171 } else
f245e549 172 p += 4;
ad0ebb96
MCC
173
174 startread = p;
175 remain = len;
176
177 /* Interlaces frame */
178 if (buf->top_field)
179 fieldstart = outp;
180 else
44dc733c 181 fieldstart = outp + bytesperline;
ad0ebb96 182
44dc733c
MCC
183 linesdone = dma_q->pos / bytesperline;
184 currlinedone = dma_q->pos % bytesperline;
185 offset = linesdone * bytesperline * 2 + currlinedone;
ad0ebb96 186 startwrite = fieldstart + offset;
44dc733c 187 lencopy = bytesperline - currlinedone;
ad0ebb96
MCC
188 lencopy = lencopy > remain ? remain : lencopy;
189
f245e549 190 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
ea8df7e0 191 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
f245e549
MCC
192 ((char *)startwrite + lencopy) -
193 ((char *)outp + buf->vb.size));
a1a6ee74
NS
194 remain = (char *)outp + buf->vb.size - (char *)startwrite;
195 lencopy = remain;
d7aa8020 196 }
e0fadfd3
AT
197 if (lencopy <= 0)
198 return;
d7aa8020 199 memcpy(startwrite, startread, lencopy);
ad0ebb96
MCC
200
201 remain -= lencopy;
202
203 while (remain > 0) {
44dc733c 204 startwrite += lencopy + bytesperline;
ad0ebb96 205 startread += lencopy;
44dc733c 206 if (bytesperline > remain)
ad0ebb96
MCC
207 lencopy = remain;
208 else
44dc733c 209 lencopy = bytesperline;
ad0ebb96 210
a1a6ee74
NS
211 if ((char *)startwrite + lencopy > (char *)outp +
212 buf->vb.size) {
ea8df7e0 213 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
f245e549
MCC
214 ((char *)startwrite + lencopy) -
215 ((char *)outp + buf->vb.size));
216 lencopy = remain = (char *)outp + buf->vb.size -
217 (char *)startwrite;
d7aa8020 218 }
f245e549
MCC
219 if (lencopy <= 0)
220 break;
d7aa8020
AT
221
222 memcpy(startwrite, startread, lencopy);
ad0ebb96
MCC
223
224 remain -= lencopy;
225 }
226
227 dma_q->pos += len;
228}
229
f245e549 230static inline void print_err_status(struct em28xx *dev,
ad0ebb96
MCC
231 int packet, int status)
232{
233 char *errmsg = "Unknown";
234
f245e549 235 switch (status) {
ad0ebb96
MCC
236 case -ENOENT:
237 errmsg = "unlinked synchronuously";
238 break;
239 case -ECONNRESET:
240 errmsg = "unlinked asynchronuously";
241 break;
242 case -ENOSR:
243 errmsg = "Buffer error (overrun)";
244 break;
245 case -EPIPE:
246 errmsg = "Stalled (device not responding)";
247 break;
248 case -EOVERFLOW:
249 errmsg = "Babble (bad cable?)";
250 break;
251 case -EPROTO:
252 errmsg = "Bit-stuff error (bad cable?)";
253 break;
254 case -EILSEQ:
255 errmsg = "CRC/Timeout (could be anything)";
256 break;
257 case -ETIME:
258 errmsg = "Device does not respond";
259 break;
260 }
f245e549 261 if (packet < 0) {
ad0ebb96
MCC
262 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
263 } else {
264 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
265 packet, status, errmsg);
266 }
267}
268
269/*
270 * video-buf generic routine to get the next available buffer
271 */
3b5fa928 272static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
ad0ebb96
MCC
273 struct em28xx_buffer **buf)
274{
275 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
dbecb44c 276 char *outp;
ad0ebb96 277
dbecb44c
MCC
278 if (list_empty(&dma_q->active)) {
279 em28xx_isocdbg("No active queue to serve\n");
dbecb44c
MCC
280 dev->isoc_ctl.buf = NULL;
281 *buf = NULL;
3b5fa928 282 return;
dbecb44c
MCC
283 }
284
dbecb44c
MCC
285 /* Get the next buffer */
286 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
287
dbecb44c
MCC
288 /* Cleans up buffer - Usefull for testing for frame/URB loss */
289 outp = videobuf_to_vmalloc(&(*buf)->vb);
290 memset(outp, 0, (*buf)->vb.size);
cb784724
MCC
291
292 dev->isoc_ctl.buf = *buf;
293
3b5fa928 294 return;
ad0ebb96
MCC
295}
296
297/*
298 * Controls the isoc copy of each urb packet
299 */
579f72e4 300static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
ad0ebb96 301{
d7aa8020 302 struct em28xx_buffer *buf;
ad0ebb96 303 struct em28xx_dmaqueue *dma_q = urb->context;
3b5fa928 304 unsigned char *outp = NULL;
ad0ebb96 305 int i, len = 0, rc = 1;
d7aa8020 306 unsigned char *p;
ad0ebb96
MCC
307
308 if (!dev)
309 return 0;
310
311 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
312 return 0;
313
f245e549
MCC
314 if (urb->status < 0) {
315 print_err_status(dev, -1, urb->status);
ad0ebb96
MCC
316 if (urb->status == -ENOENT)
317 return 0;
318 }
319
3b5fa928
AT
320 buf = dev->isoc_ctl.buf;
321 if (buf != NULL)
322 outp = videobuf_to_vmalloc(&buf->vb);
d7aa8020 323
ad0ebb96
MCC
324 for (i = 0; i < urb->number_of_packets; i++) {
325 int status = urb->iso_frame_desc[i].status;
326
f245e549
MCC
327 if (status < 0) {
328 print_err_status(dev, i, status);
ad0ebb96
MCC
329 if (urb->iso_frame_desc[i].status != -EPROTO)
330 continue;
331 }
332
f245e549 333 len = urb->iso_frame_desc[i].actual_length - 4;
ad0ebb96
MCC
334
335 if (urb->iso_frame_desc[i].actual_length <= 0) {
d7aa8020 336 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
ad0ebb96
MCC
337 continue;
338 }
339 if (urb->iso_frame_desc[i].actual_length >
340 dev->max_pkt_size) {
341 em28xx_isocdbg("packet bigger than packet size");
342 continue;
343 }
344
345 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
ad0ebb96
MCC
346
347 /* FIXME: incomplete buffer checks where removed to make
348 logic simpler. Impacts of those changes should be evaluated
349 */
b4916f8c
MCC
350 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
351 em28xx_isocdbg("VBI HEADER!!!\n");
352 /* FIXME: Should add vbi copy */
353 continue;
354 }
d7aa8020 355 if (p[0] == 0x22 && p[1] == 0x5a) {
78bb3949 356 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
a1a6ee74 357 len, (p[2] & 1) ? "odd" : "even");
d7aa8020 358
3b5fa928
AT
359 if (!(p[2] & 1)) {
360 if (buf != NULL)
361 buffer_filled(dev, dma_q, buf);
362 get_next_buf(dma_q, &buf);
363 if (buf == NULL)
364 outp = NULL;
365 else
366 outp = videobuf_to_vmalloc(&buf->vb);
367 }
368
369 if (buf != NULL) {
370 if (p[2] & 1)
371 buf->top_field = 0;
372 else
373 buf->top_field = 1;
e0fadfd3 374 }
b4916f8c 375
ad0ebb96 376 dma_q->pos = 0;
ad0ebb96 377 }
3b5fa928
AT
378 if (buf != NULL)
379 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
ad0ebb96
MCC
380 }
381 return rc;
382}
383
ad0ebb96
MCC
384/* ------------------------------------------------------------------
385 Videobuf operations
386 ------------------------------------------------------------------*/
387
388static int
389buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
390{
391 struct em28xx_fh *fh = vq->priv_data;
d2d9fbfd
MCC
392 struct em28xx *dev = fh->dev;
393 struct v4l2_frequency f;
ad0ebb96 394
bddcf633
MCC
395 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
396
ad0ebb96
MCC
397 if (0 == *count)
398 *count = EM28XX_DEF_BUF;
399
f245e549
MCC
400 if (*count < EM28XX_MIN_BUF)
401 *count = EM28XX_MIN_BUF;
ad0ebb96 402
381aaba9 403 /* Ask tuner to go to analog or radio mode */
6ea54d93 404 memset(&f, 0, sizeof(f));
d2d9fbfd 405 f.frequency = dev->ctl_freq;
381aaba9 406 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
d2d9fbfd 407
f2cf250a 408 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
d2d9fbfd 409
ad0ebb96
MCC
410 return 0;
411}
412
3b5fa928 413/* This is called *without* dev->slock held; please keep it that way */
ad0ebb96
MCC
414static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
415{
3b5fa928
AT
416 struct em28xx_fh *fh = vq->priv_data;
417 struct em28xx *dev = fh->dev;
418 unsigned long flags = 0;
ad0ebb96
MCC
419 if (in_interrupt())
420 BUG();
421
3b5fa928
AT
422 /* We used to wait for the buffer to finish here, but this didn't work
423 because, as we were keeping the state as VIDEOBUF_QUEUED,
424 videobuf_queue_cancel marked it as finished for us.
425 (Also, it could wedge forever if the hardware was misconfigured.)
426
427 This should be safe; by the time we get here, the buffer isn't
428 queued anymore. If we ever start marking the buffers as
429 VIDEOBUF_ACTIVE, it won't be, though.
430 */
431 spin_lock_irqsave(&dev->slock, flags);
432 if (dev->isoc_ctl.buf == buf)
433 dev->isoc_ctl.buf = NULL;
434 spin_unlock_irqrestore(&dev->slock, flags);
435
ad0ebb96
MCC
436 videobuf_vmalloc_free(&buf->vb);
437 buf->vb.state = VIDEOBUF_NEEDS_INIT;
438}
439
440static int
441buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
442 enum v4l2_field field)
443{
444 struct em28xx_fh *fh = vq->priv_data;
f245e549 445 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
ad0ebb96
MCC
446 struct em28xx *dev = fh->dev;
447 int rc = 0, urb_init = 0;
ad0ebb96 448
bddcf633 449 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
ad0ebb96
MCC
450
451 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
452 return -EINVAL;
453
05612975
BP
454 buf->vb.width = dev->width;
455 buf->vb.height = dev->height;
456 buf->vb.field = field;
ad0ebb96
MCC
457
458 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
d7aa8020
AT
459 rc = videobuf_iolock(vq, &buf->vb, NULL);
460 if (rc < 0)
ad0ebb96 461 goto fail;
ad0ebb96
MCC
462 }
463
ad0ebb96 464 if (!dev->isoc_ctl.num_bufs)
f245e549 465 urb_init = 1;
ad0ebb96
MCC
466
467 if (urb_init) {
579f72e4
AT
468 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
469 EM28XX_NUM_BUFS, dev->max_pkt_size,
c67ec53f 470 em28xx_isoc_copy);
f245e549 471 if (rc < 0)
ad0ebb96
MCC
472 goto fail;
473 }
474
475 buf->vb.state = VIDEOBUF_PREPARED;
476 return 0;
477
478fail:
f245e549 479 free_buffer(vq, buf);
ad0ebb96
MCC
480 return rc;
481}
482
483static void
484buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
485{
a1a6ee74
NS
486 struct em28xx_buffer *buf = container_of(vb,
487 struct em28xx_buffer,
488 vb);
ad0ebb96 489 struct em28xx_fh *fh = vq->priv_data;
f245e549 490 struct em28xx *dev = fh->dev;
ad0ebb96 491 struct em28xx_dmaqueue *vidq = &dev->vidq;
ad0ebb96 492
d7aa8020
AT
493 buf->vb.state = VIDEOBUF_QUEUED;
494 list_add_tail(&buf->vb.queue, &vidq->active);
495
ad0ebb96
MCC
496}
497
6ea54d93
DSL
498static void buffer_release(struct videobuf_queue *vq,
499 struct videobuf_buffer *vb)
ad0ebb96 500{
a1a6ee74
NS
501 struct em28xx_buffer *buf = container_of(vb,
502 struct em28xx_buffer,
503 vb);
ad0ebb96 504 struct em28xx_fh *fh = vq->priv_data;
f245e549 505 struct em28xx *dev = (struct em28xx *)fh->dev;
ad0ebb96 506
d7aa8020 507 em28xx_isocdbg("em28xx: called buffer_release\n");
ad0ebb96 508
f245e549 509 free_buffer(vq, buf);
ad0ebb96
MCC
510}
511
512static struct videobuf_queue_ops em28xx_video_qops = {
513 .buf_setup = buffer_setup,
514 .buf_prepare = buffer_prepare,
515 .buf_queue = buffer_queue,
516 .buf_release = buffer_release,
517};
a6c2ba28 518
6ea54d93 519/********************* v4l2 interface **************************************/
a6c2ba28 520
eac94356
MCC
521static void video_mux(struct em28xx *dev, int index)
522{
eac94356
MCC
523 dev->ctl_input = index;
524 dev->ctl_ainput = INPUT(index)->amux;
35ae6f04 525 dev->ctl_aoutput = INPUT(index)->aout;
eac94356 526
e879b8eb
MCC
527 if (!dev->ctl_aoutput)
528 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
529
5325b427
HV
530 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
531 INPUT(index)->vmux, 0, 0);
eac94356 532
505b6d0b 533 if (dev->board.has_msp34xx) {
6ea54d93 534 if (dev->i2s_speed) {
f2cf250a
DSL
535 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
536 s_i2s_clock_freq, dev->i2s_speed);
6ea54d93 537 }
2474ed44 538 /* Note: this is msp3400 specific */
5325b427
HV
539 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
540 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
eac94356 541 }
539c96d0 542
2bd1d9eb 543 if (dev->board.adecoder != EM28XX_NOADECODER) {
5325b427
HV
544 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
545 dev->ctl_ainput, dev->ctl_aoutput, 0);
2bd1d9eb
VW
546 }
547
00b8730f 548 em28xx_audio_analog_set(dev);
eac94356
MCC
549}
550
a225452e
MCC
551/* Usage lock check functions */
552static int res_get(struct em28xx_fh *fh)
553{
554 struct em28xx *dev = fh->dev;
555 int rc = 0;
556
557 /* This instance already has stream_on */
558 if (fh->stream_on)
559 return rc;
560
a225452e 561 if (dev->stream_on)
29b59417 562 return -EBUSY;
a225452e 563
e74153d4
MCC
564 dev->stream_on = 1;
565 fh->stream_on = 1;
a225452e
MCC
566 return rc;
567}
568
569static int res_check(struct em28xx_fh *fh)
570{
a1a6ee74 571 return fh->stream_on;
a225452e
MCC
572}
573
574static void res_free(struct em28xx_fh *fh)
575{
576 struct em28xx *dev = fh->dev;
577
a225452e
MCC
578 fh->stream_on = 0;
579 dev->stream_on = 0;
a225452e
MCC
580}
581
195a4ef6
MCC
582/*
583 * em28xx_get_ctrl()
584 * return the current saturation, brightness or contrast, mute state
585 */
586static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
587{
588 switch (ctrl->id) {
589 case V4L2_CID_AUDIO_MUTE:
590 ctrl->value = dev->mute;
591 return 0;
592 case V4L2_CID_AUDIO_VOLUME:
593 ctrl->value = dev->volume;
594 return 0;
595 default:
596 return -EINVAL;
a6c2ba28 597 }
195a4ef6 598}
a6c2ba28 599
195a4ef6
MCC
600/*
601 * em28xx_set_ctrl()
602 * mute or set new saturation, brightness or contrast
603 */
604static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
605{
606 switch (ctrl->id) {
607 case V4L2_CID_AUDIO_MUTE:
608 if (ctrl->value != dev->mute) {
609 dev->mute = ctrl->value;
195a4ef6
MCC
610 return em28xx_audio_analog_set(dev);
611 }
612 return 0;
613 case V4L2_CID_AUDIO_VOLUME:
614 dev->volume = ctrl->value;
615 return em28xx_audio_analog_set(dev);
616 default:
617 return -EINVAL;
618 }
619}
a6c2ba28 620
195a4ef6
MCC
621static int check_dev(struct em28xx *dev)
622{
623 if (dev->state & DEV_DISCONNECTED) {
624 em28xx_errdev("v4l2 ioctl: device not present\n");
625 return -ENODEV;
e5589bef 626 }
a6c2ba28 627
195a4ef6
MCC
628 if (dev->state & DEV_MISCONFIGURED) {
629 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
630 "close and open it again\n");
631 return -EIO;
632 }
633 return 0;
634}
a6c2ba28 635
195a4ef6
MCC
636static void get_scale(struct em28xx *dev,
637 unsigned int width, unsigned int height,
638 unsigned int *hscale, unsigned int *vscale)
639{
640 unsigned int maxw = norm_maxw(dev);
641 unsigned int maxh = norm_maxh(dev);
642
643 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
644 if (*hscale >= 0x4000)
645 *hscale = 0x3fff;
646
647 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
648 if (*vscale >= 0x4000)
649 *vscale = 0x3fff;
a6c2ba28 650}
651
195a4ef6
MCC
652/* ------------------------------------------------------------------
653 IOCTL vidioc handling
654 ------------------------------------------------------------------*/
655
78b526a4 656static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
195a4ef6 657 struct v4l2_format *f)
a6c2ba28 658{
195a4ef6
MCC
659 struct em28xx_fh *fh = priv;
660 struct em28xx *dev = fh->dev;
a6c2ba28 661
195a4ef6 662 mutex_lock(&dev->lock);
e5589bef 663
195a4ef6
MCC
664 f->fmt.pix.width = dev->width;
665 f->fmt.pix.height = dev->height;
bddcf633
MCC
666 f->fmt.pix.pixelformat = dev->format->fourcc;
667 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
44dc733c 668 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
195a4ef6 669 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
e5589bef 670
195a4ef6
MCC
671 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
672 f->fmt.pix.field = dev->interlaced ?
673 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
e5589bef 674
195a4ef6
MCC
675 mutex_unlock(&dev->lock);
676 return 0;
a6c2ba28 677}
678
bddcf633
MCC
679static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
680{
681 unsigned int i;
682
683 for (i = 0; i < ARRAY_SIZE(format); i++)
684 if (format[i].fourcc == fourcc)
685 return &format[i];
686
687 return NULL;
688}
689
78b526a4 690static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
195a4ef6 691 struct v4l2_format *f)
a6c2ba28 692{
195a4ef6
MCC
693 struct em28xx_fh *fh = priv;
694 struct em28xx *dev = fh->dev;
ccb83408
TP
695 unsigned int width = f->fmt.pix.width;
696 unsigned int height = f->fmt.pix.height;
195a4ef6
MCC
697 unsigned int maxw = norm_maxw(dev);
698 unsigned int maxh = norm_maxh(dev);
699 unsigned int hscale, vscale;
bddcf633
MCC
700 struct em28xx_fmt *fmt;
701
43cb9fe3
MCC
702 /* FIXME: This is the only supported fmt */
703 if (dev->board.is_27xx)
704 f->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB565;
705
bddcf633
MCC
706 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
707 if (!fmt) {
708 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
709 f->fmt.pix.pixelformat);
710 return -EINVAL;
711 }
195a4ef6 712
43cb9fe3
MCC
713 if (dev->board.is_27xx) {
714 /* FIXME: This is the only supported fmt */
715 width = 640;
716 height = 480;
717 } else if (dev->board.is_em2800) {
195a4ef6 718 /* the em2800 can only scale down to 50% */
ccb83408
TP
719 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
720 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
721 /* According to empiatech support the MaxPacketSize is too small
722 * to support framesizes larger than 640x480 @ 30 fps or 640x576
723 * @ 25 fps. As this would cut of a part of the image we prefer
724 * 360x576 or 360x480 for now */
195a4ef6
MCC
725 if (width == maxw && height == maxh)
726 width /= 2;
ccb83408
TP
727 } else {
728 /* width must even because of the YUYV format
729 height must be even because of interlacing */
730 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
195a4ef6 731 }
a225452e 732
195a4ef6 733 get_scale(dev, width, height, &hscale, &vscale);
a6c2ba28 734
195a4ef6
MCC
735 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
736 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
a6c2ba28 737
195a4ef6
MCC
738 f->fmt.pix.width = width;
739 f->fmt.pix.height = height;
bddcf633
MCC
740 f->fmt.pix.pixelformat = fmt->fourcc;
741 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
742 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
195a4ef6
MCC
743 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
744 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
a6c2ba28 745
a6c2ba28 746 return 0;
747}
748
ed5f1431
MCC
749static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
750 unsigned width, unsigned height)
751{
752 struct em28xx_fmt *fmt;
753
754 /* FIXME: This is the only supported fmt */
755 if (dev->board.is_27xx) {
756 fourcc = V4L2_PIX_FMT_RGB565;
757 width = 640;
758 height = 480;
759 }
760
761 fmt = format_by_fourcc(fourcc);
762 if (!fmt)
763 return -EINVAL;
764
765 dev->format = fmt;
766 dev->width = width;
767 dev->height = height;
768
769 /* set new image size */
770 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
771
772 em28xx_set_alternate(dev);
773 em28xx_resolution_set(dev);
774
775 return 0;
776}
777
78b526a4 778static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
195a4ef6 779 struct v4l2_format *f)
a6c2ba28 780{
195a4ef6
MCC
781 struct em28xx_fh *fh = priv;
782 struct em28xx *dev = fh->dev;
ad0ebb96 783 int rc;
a6c2ba28 784
195a4ef6
MCC
785 rc = check_dev(dev);
786 if (rc < 0)
787 return rc;
a225452e 788
5a80415b
SS
789 mutex_lock(&dev->lock);
790
efc52a94
MCC
791 vidioc_try_fmt_vid_cap(file, priv, f);
792
05612975
BP
793 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
794 em28xx_errdev("%s queue busy\n", __func__);
795 rc = -EBUSY;
796 goto out;
797 }
798
0ea13e6e
AT
799 if (dev->stream_on && !fh->stream_on) {
800 em28xx_errdev("%s device in use by another fh\n", __func__);
801 rc = -EBUSY;
802 goto out;
803 }
804
ed5f1431
MCC
805 rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
806 f->fmt.pix.width, f->fmt.pix.height);
05612975
BP
807
808out:
195a4ef6 809 mutex_unlock(&dev->lock);
05612975 810 return rc;
195a4ef6
MCC
811}
812
a1a6ee74 813static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
195a4ef6
MCC
814{
815 struct em28xx_fh *fh = priv;
816 struct em28xx *dev = fh->dev;
817 struct v4l2_format f;
195a4ef6
MCC
818 int rc;
819
820 rc = check_dev(dev);
821 if (rc < 0)
822 return rc;
823
195a4ef6 824 mutex_lock(&dev->lock);
7d497f8a 825 dev->norm = *norm;
a6c2ba28 826
195a4ef6
MCC
827 /* Adjusts width/height, if needed */
828 f.fmt.pix.width = dev->width;
829 f.fmt.pix.height = dev->height;
78b526a4 830 vidioc_try_fmt_vid_cap(file, priv, &f);
a6c2ba28 831
195a4ef6
MCC
832 /* set new image size */
833 dev->width = f.fmt.pix.width;
834 dev->height = f.fmt.pix.height;
195a4ef6 835 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
a6c2ba28 836
195a4ef6 837 em28xx_resolution_set(dev);
f41737ec 838 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
a6c2ba28 839
195a4ef6
MCC
840 mutex_unlock(&dev->lock);
841 return 0;
842}
9e31ced8 843
195a4ef6
MCC
844static const char *iname[] = {
845 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
846 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
847 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
848 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
849 [EM28XX_VMUX_SVIDEO] = "S-Video",
850 [EM28XX_VMUX_TELEVISION] = "Television",
851 [EM28XX_VMUX_CABLE] = "Cable TV",
852 [EM28XX_VMUX_DVB] = "DVB",
853 [EM28XX_VMUX_DEBUG] = "for debug only",
854};
9e31ced8 855
195a4ef6
MCC
856static int vidioc_enum_input(struct file *file, void *priv,
857 struct v4l2_input *i)
858{
859 struct em28xx_fh *fh = priv;
860 struct em28xx *dev = fh->dev;
861 unsigned int n;
9e31ced8 862
195a4ef6
MCC
863 n = i->index;
864 if (n >= MAX_EM28XX_INPUT)
865 return -EINVAL;
866 if (0 == INPUT(n)->type)
867 return -EINVAL;
9e31ced8 868
195a4ef6
MCC
869 i->index = n;
870 i->type = V4L2_INPUT_TYPE_CAMERA;
a6c2ba28 871
195a4ef6 872 strcpy(i->name, iname[INPUT(n)->type]);
a6c2ba28 873
195a4ef6
MCC
874 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
875 (EM28XX_VMUX_CABLE == INPUT(n)->type))
876 i->type = V4L2_INPUT_TYPE_TUNER;
877
7d497f8a 878 i->std = dev->vdev->tvnorms;
195a4ef6
MCC
879
880 return 0;
a6c2ba28 881}
882
195a4ef6 883static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
a6c2ba28 884{
195a4ef6
MCC
885 struct em28xx_fh *fh = priv;
886 struct em28xx *dev = fh->dev;
a6c2ba28 887
195a4ef6
MCC
888 *i = dev->ctl_input;
889
890 return 0;
891}
892
893static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
894{
895 struct em28xx_fh *fh = priv;
896 struct em28xx *dev = fh->dev;
897 int rc;
898
899 rc = check_dev(dev);
900 if (rc < 0)
901 return rc;
902
903 if (i >= MAX_EM28XX_INPUT)
904 return -EINVAL;
905 if (0 == INPUT(i)->type)
906 return -EINVAL;
a225452e 907
24c3c415 908 dev->ctl_input = i;
195a4ef6 909
24c3c415
MCC
910 mutex_lock(&dev->lock);
911 video_mux(dev, dev->ctl_input);
195a4ef6
MCC
912 mutex_unlock(&dev->lock);
913 return 0;
914}
915
916static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
917{
918 struct em28xx_fh *fh = priv;
919 struct em28xx *dev = fh->dev;
195a4ef6 920
35ae6f04
MCC
921 switch (a->index) {
922 case EM28XX_AMUX_VIDEO:
195a4ef6 923 strcpy(a->name, "Television");
35ae6f04
MCC
924 break;
925 case EM28XX_AMUX_LINE_IN:
195a4ef6 926 strcpy(a->name, "Line In");
35ae6f04
MCC
927 break;
928 case EM28XX_AMUX_VIDEO2:
929 strcpy(a->name, "Television alt");
930 break;
931 case EM28XX_AMUX_PHONE:
932 strcpy(a->name, "Phone");
933 break;
934 case EM28XX_AMUX_MIC:
935 strcpy(a->name, "Mic");
936 break;
937 case EM28XX_AMUX_CD:
938 strcpy(a->name, "CD");
939 break;
940 case EM28XX_AMUX_AUX:
941 strcpy(a->name, "Aux");
942 break;
943 case EM28XX_AMUX_PCM_OUT:
944 strcpy(a->name, "PCM");
945 break;
946 default:
947 return -EINVAL;
948 }
6ea54d93 949
35ae6f04 950 a->index = dev->ctl_ainput;
195a4ef6 951 a->capability = V4L2_AUDCAP_STEREO;
195a4ef6
MCC
952
953 return 0;
954}
955
956static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
957{
958 struct em28xx_fh *fh = priv;
959 struct em28xx *dev = fh->dev;
960
24c3c415
MCC
961
962 if (a->index >= MAX_EM28XX_INPUT)
963 return -EINVAL;
964 if (0 == INPUT(a->index)->type)
965 return -EINVAL;
966
efc52a94
MCC
967 mutex_lock(&dev->lock);
968
35ae6f04
MCC
969 dev->ctl_ainput = INPUT(a->index)->amux;
970 dev->ctl_aoutput = INPUT(a->index)->aout;
e879b8eb
MCC
971
972 if (!dev->ctl_aoutput)
973 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
efc52a94
MCC
974
975 mutex_unlock(&dev->lock);
195a4ef6
MCC
976 return 0;
977}
978
979static int vidioc_queryctrl(struct file *file, void *priv,
980 struct v4l2_queryctrl *qc)
981{
982 struct em28xx_fh *fh = priv;
983 struct em28xx *dev = fh->dev;
984 int id = qc->id;
985 int i;
986 int rc;
987
988 rc = check_dev(dev);
989 if (rc < 0)
990 return rc;
991
992 memset(qc, 0, sizeof(*qc));
993
994 qc->id = id;
995
505b6d0b 996 if (!dev->board.has_msp34xx) {
195a4ef6
MCC
997 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
998 if (qc->id && qc->id == em28xx_qctrl[i].id) {
999 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1000 return 0;
a6c2ba28 1001 }
1002 }
195a4ef6 1003 }
f2cf250a 1004
195a4ef6 1005 mutex_lock(&dev->lock);
f2cf250a 1006 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
195a4ef6 1007 mutex_unlock(&dev->lock);
a6c2ba28 1008
195a4ef6
MCC
1009 if (qc->type)
1010 return 0;
1011 else
1012 return -EINVAL;
1013}
a6c2ba28 1014
195a4ef6
MCC
1015static int vidioc_g_ctrl(struct file *file, void *priv,
1016 struct v4l2_control *ctrl)
1017{
1018 struct em28xx_fh *fh = priv;
1019 struct em28xx *dev = fh->dev;
1020 int rc;
a6c2ba28 1021
195a4ef6
MCC
1022 rc = check_dev(dev);
1023 if (rc < 0)
1024 return rc;
b6070f07 1025 rc = 0;
efc52a94 1026
195a4ef6 1027 mutex_lock(&dev->lock);
a6c2ba28 1028
b6070f07 1029 if (dev->board.has_msp34xx)
f2cf250a 1030 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
07f7db4c 1031 else {
b6070f07 1032 rc = em28xx_get_ctrl(dev, ctrl);
07f7db4c 1033 if (rc < 0) {
f2cf250a 1034 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
07f7db4c
HV
1035 rc = 0;
1036 }
1037 }
195a4ef6
MCC
1038
1039 mutex_unlock(&dev->lock);
1040 return rc;
1041}
1042
1043static int vidioc_s_ctrl(struct file *file, void *priv,
1044 struct v4l2_control *ctrl)
1045{
1046 struct em28xx_fh *fh = priv;
1047 struct em28xx *dev = fh->dev;
1048 u8 i;
1049 int rc;
1050
1051 rc = check_dev(dev);
1052 if (rc < 0)
1053 return rc;
1054
1055 mutex_lock(&dev->lock);
1056
505b6d0b 1057 if (dev->board.has_msp34xx)
f2cf250a 1058 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
195a4ef6
MCC
1059 else {
1060 rc = 1;
1061 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1062 if (ctrl->id == em28xx_qctrl[i].id) {
1063 if (ctrl->value < em28xx_qctrl[i].minimum ||
1064 ctrl->value > em28xx_qctrl[i].maximum) {
1065 rc = -ERANGE;
1066 break;
1067 }
1068
1069 rc = em28xx_set_ctrl(dev, ctrl);
1070 break;
1071 }
a6c2ba28 1072 }
1073 }
1074
195a4ef6
MCC
1075 /* Control not found - try to send it to the attached devices */
1076 if (rc == 1) {
f2cf250a 1077 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
195a4ef6
MCC
1078 rc = 0;
1079 }
1080
5a80415b 1081 mutex_unlock(&dev->lock);
195a4ef6 1082 return rc;
a6c2ba28 1083}
1084
195a4ef6
MCC
1085static int vidioc_g_tuner(struct file *file, void *priv,
1086 struct v4l2_tuner *t)
a6c2ba28 1087{
195a4ef6
MCC
1088 struct em28xx_fh *fh = priv;
1089 struct em28xx *dev = fh->dev;
1090 int rc;
1091
1092 rc = check_dev(dev);
1093 if (rc < 0)
1094 return rc;
1095
1096 if (0 != t->index)
1097 return -EINVAL;
1098
1099 strcpy(t->name, "Tuner");
1100
1101 mutex_lock(&dev->lock);
f2cf250a 1102 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
195a4ef6 1103 mutex_unlock(&dev->lock);
f2cf250a 1104
195a4ef6 1105 return 0;
a6c2ba28 1106}
1107
195a4ef6
MCC
1108static int vidioc_s_tuner(struct file *file, void *priv,
1109 struct v4l2_tuner *t)
a6c2ba28 1110{
195a4ef6
MCC
1111 struct em28xx_fh *fh = priv;
1112 struct em28xx *dev = fh->dev;
1113 int rc;
63337dd3 1114
195a4ef6
MCC
1115 rc = check_dev(dev);
1116 if (rc < 0)
1117 return rc;
1118
1119 if (0 != t->index)
1120 return -EINVAL;
1121
1122 mutex_lock(&dev->lock);
f2cf250a 1123 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
195a4ef6 1124 mutex_unlock(&dev->lock);
f2cf250a 1125
195a4ef6 1126 return 0;
a6c2ba28 1127}
1128
195a4ef6
MCC
1129static int vidioc_g_frequency(struct file *file, void *priv,
1130 struct v4l2_frequency *f)
1131{
1132 struct em28xx_fh *fh = priv;
1133 struct em28xx *dev = fh->dev;
a6c2ba28 1134
efc52a94 1135 mutex_lock(&dev->lock);
0be43754 1136 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
195a4ef6 1137 f->frequency = dev->ctl_freq;
efc52a94 1138 mutex_unlock(&dev->lock);
195a4ef6
MCC
1139
1140 return 0;
1141}
1142
1143static int vidioc_s_frequency(struct file *file, void *priv,
1144 struct v4l2_frequency *f)
a6c2ba28 1145{
195a4ef6
MCC
1146 struct em28xx_fh *fh = priv;
1147 struct em28xx *dev = fh->dev;
1148 int rc;
9c75541f 1149
195a4ef6
MCC
1150 rc = check_dev(dev);
1151 if (rc < 0)
1152 return rc;
1153
1154 if (0 != f->tuner)
1155 return -EINVAL;
1156
0be43754
MCC
1157 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1158 return -EINVAL;
1159 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
195a4ef6 1160 return -EINVAL;
a225452e
MCC
1161
1162 mutex_lock(&dev->lock);
a3a048ce 1163
195a4ef6 1164 dev->ctl_freq = f->frequency;
f2cf250a 1165 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
a6c2ba28 1166
195a4ef6 1167 mutex_unlock(&dev->lock);
efc52a94 1168
195a4ef6
MCC
1169 return 0;
1170}
a6c2ba28 1171
1e7ad56f
MCC
1172#ifdef CONFIG_VIDEO_ADV_DEBUG
1173static int em28xx_reg_len(int reg)
1174{
1175 switch (reg) {
41facaa4
MCC
1176 case EM28XX_R40_AC97LSB:
1177 case EM28XX_R30_HSCALELOW:
1178 case EM28XX_R32_VSCALELOW:
1e7ad56f
MCC
1179 return 2;
1180 default:
1181 return 1;
1182 }
1183}
1184
14983d81 1185static int vidioc_g_chip_ident(struct file *file, void *priv,
aecde8b5 1186 struct v4l2_dbg_chip_ident *chip)
14983d81
MCC
1187{
1188 struct em28xx_fh *fh = priv;
1189 struct em28xx *dev = fh->dev;
1190
1191 chip->ident = V4L2_IDENT_NONE;
1192 chip->revision = 0;
1193
f2cf250a 1194 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
14983d81
MCC
1195
1196 return 0;
1197}
1198
1199
1e7ad56f 1200static int vidioc_g_register(struct file *file, void *priv,
aecde8b5 1201 struct v4l2_dbg_register *reg)
1e7ad56f
MCC
1202{
1203 struct em28xx_fh *fh = priv;
1204 struct em28xx *dev = fh->dev;
1205 int ret;
1206
aecde8b5 1207 switch (reg->match.type) {
14983d81 1208 case V4L2_CHIP_MATCH_AC97:
531c98e7
MCC
1209 mutex_lock(&dev->lock);
1210 ret = em28xx_read_ac97(dev, reg->reg);
1211 mutex_unlock(&dev->lock);
1212 if (ret < 0)
1213 return ret;
1214
1215 reg->val = ret;
aecde8b5 1216 reg->size = 1;
531c98e7 1217 return 0;
14983d81 1218 case V4L2_CHIP_MATCH_I2C_DRIVER:
f2cf250a 1219 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
14983d81
MCC
1220 return 0;
1221 case V4L2_CHIP_MATCH_I2C_ADDR:
1222 /* Not supported yet */
1e7ad56f 1223 return -EINVAL;
14983d81 1224 default:
aecde8b5 1225 if (!v4l2_chip_match_host(&reg->match))
14983d81
MCC
1226 return -EINVAL;
1227 }
1e7ad56f 1228
14983d81 1229 /* Match host */
aecde8b5
HV
1230 reg->size = em28xx_reg_len(reg->reg);
1231 if (reg->size == 1) {
efc52a94 1232 mutex_lock(&dev->lock);
1e7ad56f 1233 ret = em28xx_read_reg(dev, reg->reg);
efc52a94
MCC
1234 mutex_unlock(&dev->lock);
1235
1e7ad56f
MCC
1236 if (ret < 0)
1237 return ret;
1238
1239 reg->val = ret;
1240 } else {
aecde8b5 1241 __le16 val = 0;
efc52a94 1242 mutex_lock(&dev->lock);
1e7ad56f
MCC
1243 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1244 reg->reg, (char *)&val, 2);
efc52a94 1245 mutex_unlock(&dev->lock);
1e7ad56f
MCC
1246 if (ret < 0)
1247 return ret;
1248
aecde8b5 1249 reg->val = le16_to_cpu(val);
1e7ad56f
MCC
1250 }
1251
1252 return 0;
1253}
1254
1255static int vidioc_s_register(struct file *file, void *priv,
aecde8b5 1256 struct v4l2_dbg_register *reg)
1e7ad56f
MCC
1257{
1258 struct em28xx_fh *fh = priv;
1259 struct em28xx *dev = fh->dev;
aecde8b5 1260 __le16 buf;
efc52a94 1261 int rc;
1e7ad56f 1262
aecde8b5 1263 switch (reg->match.type) {
14983d81 1264 case V4L2_CHIP_MATCH_AC97:
531c98e7
MCC
1265 mutex_lock(&dev->lock);
1266 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1267 mutex_unlock(&dev->lock);
1268
1269 return rc;
14983d81 1270 case V4L2_CHIP_MATCH_I2C_DRIVER:
f2cf250a 1271 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
14983d81
MCC
1272 return 0;
1273 case V4L2_CHIP_MATCH_I2C_ADDR:
1274 /* Not supported yet */
1275 return -EINVAL;
1276 default:
aecde8b5 1277 if (!v4l2_chip_match_host(&reg->match))
14983d81 1278 return -EINVAL;
531c98e7
MCC
1279 }
1280
14983d81 1281 /* Match host */
aecde8b5 1282 buf = cpu_to_le16(reg->val);
1e7ad56f 1283
efc52a94
MCC
1284 mutex_lock(&dev->lock);
1285 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1286 em28xx_reg_len(reg->reg));
1287 mutex_unlock(&dev->lock);
1288
1289 return rc;
1e7ad56f
MCC
1290}
1291#endif
1292
1293
195a4ef6
MCC
1294static int vidioc_cropcap(struct file *file, void *priv,
1295 struct v4l2_cropcap *cc)
1296{
1297 struct em28xx_fh *fh = priv;
1298 struct em28xx *dev = fh->dev;
1299
1300 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
a6c2ba28 1301 return -EINVAL;
a6c2ba28 1302
195a4ef6
MCC
1303 cc->bounds.left = 0;
1304 cc->bounds.top = 0;
1305 cc->bounds.width = dev->width;
1306 cc->bounds.height = dev->height;
1307 cc->defrect = cc->bounds;
1308 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1309 cc->pixelaspect.denominator = 59;
1310
1311 return 0;
1312}
1313
1314static int vidioc_streamon(struct file *file, void *priv,
1315 enum v4l2_buf_type type)
1316{
1317 struct em28xx_fh *fh = priv;
1318 struct em28xx *dev = fh->dev;
1319 int rc;
1320
1321 rc = check_dev(dev);
1322 if (rc < 0)
1323 return rc;
1324
195a4ef6 1325
29b59417
MCC
1326 mutex_lock(&dev->lock);
1327 rc = res_get(fh);
29b59417 1328
5db0b5e1
MCC
1329 if (likely(rc >= 0))
1330 rc = videobuf_streamon(&fh->vb_vidq);
efc52a94
MCC
1331
1332 mutex_unlock(&dev->lock);
1333
1334 return rc;
195a4ef6
MCC
1335}
1336
1337static int vidioc_streamoff(struct file *file, void *priv,
1338 enum v4l2_buf_type type)
1339{
1340 struct em28xx_fh *fh = priv;
1341 struct em28xx *dev = fh->dev;
1342 int rc;
1343
1344 rc = check_dev(dev);
1345 if (rc < 0)
1346 return rc;
1347
ad0ebb96
MCC
1348 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1349 return -EINVAL;
1350 if (type != fh->type)
195a4ef6
MCC
1351 return -EINVAL;
1352
7831364f 1353 mutex_lock(&dev->lock);
efc52a94
MCC
1354
1355 videobuf_streamoff(&fh->vb_vidq);
ad0ebb96 1356 res_free(fh);
efc52a94 1357
7831364f 1358 mutex_unlock(&dev->lock);
a6c2ba28 1359
a6c2ba28 1360 return 0;
1361}
1362
195a4ef6
MCC
1363static int vidioc_querycap(struct file *file, void *priv,
1364 struct v4l2_capability *cap)
a6c2ba28 1365{
195a4ef6
MCC
1366 struct em28xx_fh *fh = priv;
1367 struct em28xx *dev = fh->dev;
1368
1369 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1370 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
cb97716f 1371 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
195a4ef6
MCC
1372
1373 cap->version = EM28XX_VERSION_CODE;
1374
1375 cap->capabilities =
1376 V4L2_CAP_SLICED_VBI_CAPTURE |
1377 V4L2_CAP_VIDEO_CAPTURE |
1378 V4L2_CAP_AUDIO |
1379 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1380
ed086314 1381 if (dev->tuner_type != TUNER_ABSENT)
195a4ef6
MCC
1382 cap->capabilities |= V4L2_CAP_TUNER;
1383
1384 return 0;
c0477ad9
MCC
1385}
1386
78b526a4 1387static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
bddcf633 1388 struct v4l2_fmtdesc *f)
a6c2ba28 1389{
ed5f1431
MCC
1390 struct em28xx_fh *fh = priv;
1391 struct em28xx *dev = fh->dev;
1392
bddcf633 1393 if (unlikely(f->index >= ARRAY_SIZE(format)))
c0477ad9 1394 return -EINVAL;
195a4ef6 1395
ed5f1431
MCC
1396 if (dev->board.is_27xx) {
1397 struct em28xx_fmt *fmt;
1398 if (f->index)
1399 return -EINVAL;
1400
1401 f->pixelformat = V4L2_PIX_FMT_RGB565;
1402 fmt = format_by_fourcc(f->pixelformat);
1403 strlcpy(f->description, fmt->name, sizeof(f->description));
1404
1405 return 0;
1406 }
1407
bddcf633
MCC
1408 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1409 f->pixelformat = format[f->index].fourcc;
195a4ef6
MCC
1410
1411 return 0;
c0477ad9
MCC
1412}
1413
195a4ef6 1414/* Sliced VBI ioctls */
78b526a4 1415static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
195a4ef6 1416 struct v4l2_format *f)
a6c2ba28 1417{
195a4ef6
MCC
1418 struct em28xx_fh *fh = priv;
1419 struct em28xx *dev = fh->dev;
1420 int rc;
a6c2ba28 1421
195a4ef6
MCC
1422 rc = check_dev(dev);
1423 if (rc < 0)
1424 return rc;
a6c2ba28 1425
195a4ef6
MCC
1426 mutex_lock(&dev->lock);
1427
1428 f->fmt.sliced.service_set = 0;
f2cf250a 1429 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
195a4ef6
MCC
1430
1431 if (f->fmt.sliced.service_set == 0)
1432 rc = -EINVAL;
1433
1434 mutex_unlock(&dev->lock);
f2cf250a 1435
195a4ef6
MCC
1436 return rc;
1437}
1438
78b526a4 1439static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
195a4ef6
MCC
1440 struct v4l2_format *f)
1441{
1442 struct em28xx_fh *fh = priv;
1443 struct em28xx *dev = fh->dev;
1444 int rc;
1445
1446 rc = check_dev(dev);
1447 if (rc < 0)
1448 return rc;
1449
1450 mutex_lock(&dev->lock);
f2cf250a 1451 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
195a4ef6
MCC
1452 mutex_unlock(&dev->lock);
1453
1454 if (f->fmt.sliced.service_set == 0)
1455 return -EINVAL;
a6c2ba28 1456
1457 return 0;
1458}
1459
195a4ef6
MCC
1460
1461static int vidioc_reqbufs(struct file *file, void *priv,
1462 struct v4l2_requestbuffers *rb)
a6c2ba28 1463{
195a4ef6
MCC
1464 struct em28xx_fh *fh = priv;
1465 struct em28xx *dev = fh->dev;
195a4ef6 1466 int rc;
a6c2ba28 1467
195a4ef6
MCC
1468 rc = check_dev(dev);
1469 if (rc < 0)
1470 return rc;
a6c2ba28 1471
a1a6ee74 1472 return videobuf_reqbufs(&fh->vb_vidq, rb);
a6c2ba28 1473}
1474
195a4ef6
MCC
1475static int vidioc_querybuf(struct file *file, void *priv,
1476 struct v4l2_buffer *b)
e5589bef 1477{
195a4ef6
MCC
1478 struct em28xx_fh *fh = priv;
1479 struct em28xx *dev = fh->dev;
1480 int rc;
e5589bef 1481
195a4ef6
MCC
1482 rc = check_dev(dev);
1483 if (rc < 0)
1484 return rc;
2d50f847 1485
a1a6ee74 1486 return videobuf_querybuf(&fh->vb_vidq, b);
195a4ef6
MCC
1487}
1488
1489static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1490{
1491 struct em28xx_fh *fh = priv;
1492 struct em28xx *dev = fh->dev;
195a4ef6
MCC
1493 int rc;
1494
1495 rc = check_dev(dev);
1496 if (rc < 0)
1497 return rc;
1498
a1a6ee74 1499 return videobuf_qbuf(&fh->vb_vidq, b);
e5589bef
MCC
1500}
1501
195a4ef6 1502static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2d50f847 1503{
195a4ef6
MCC
1504 struct em28xx_fh *fh = priv;
1505 struct em28xx *dev = fh->dev;
1506 int rc;
195a4ef6
MCC
1507
1508 rc = check_dev(dev);
1509 if (rc < 0)
1510 return rc;
2d50f847 1511
a1a6ee74 1512 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
ad0ebb96 1513}
2d50f847 1514
ad0ebb96 1515#ifdef CONFIG_VIDEO_V4L1_COMPAT
f245e549 1516static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
ad0ebb96 1517{
f245e549 1518 struct em28xx_fh *fh = priv;
2d50f847 1519
f245e549 1520 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
195a4ef6 1521}
ad0ebb96
MCC
1522#endif
1523
2d50f847 1524
0be43754
MCC
1525/* ----------------------------------------------------------- */
1526/* RADIO ESPECIFIC IOCTLS */
1527/* ----------------------------------------------------------- */
1528
1529static int radio_querycap(struct file *file, void *priv,
1530 struct v4l2_capability *cap)
1531{
1532 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1533
1534 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1535 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
cb97716f 1536 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
0be43754
MCC
1537
1538 cap->version = EM28XX_VERSION_CODE;
1539 cap->capabilities = V4L2_CAP_TUNER;
1540 return 0;
1541}
1542
1543static int radio_g_tuner(struct file *file, void *priv,
1544 struct v4l2_tuner *t)
1545{
1546 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1547
1548 if (unlikely(t->index > 0))
1549 return -EINVAL;
1550
1551 strcpy(t->name, "Radio");
1552 t->type = V4L2_TUNER_RADIO;
1553
efc52a94 1554 mutex_lock(&dev->lock);
f2cf250a 1555 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
efc52a94
MCC
1556 mutex_unlock(&dev->lock);
1557
0be43754
MCC
1558 return 0;
1559}
1560
1561static int radio_enum_input(struct file *file, void *priv,
1562 struct v4l2_input *i)
1563{
1564 if (i->index != 0)
1565 return -EINVAL;
1566 strcpy(i->name, "Radio");
1567 i->type = V4L2_INPUT_TYPE_TUNER;
1568
1569 return 0;
1570}
1571
1572static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1573{
1574 if (unlikely(a->index))
1575 return -EINVAL;
1576
1577 strcpy(a->name, "Radio");
1578 return 0;
1579}
1580
1581static int radio_s_tuner(struct file *file, void *priv,
1582 struct v4l2_tuner *t)
1583{
1584 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1585
1586 if (0 != t->index)
1587 return -EINVAL;
1588
efc52a94 1589 mutex_lock(&dev->lock);
f2cf250a 1590 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
efc52a94 1591 mutex_unlock(&dev->lock);
0be43754
MCC
1592
1593 return 0;
1594}
1595
1596static int radio_s_audio(struct file *file, void *fh,
1597 struct v4l2_audio *a)
1598{
1599 return 0;
1600}
1601
1602static int radio_s_input(struct file *file, void *fh, unsigned int i)
1603{
1604 return 0;
1605}
1606
1607static int radio_queryctrl(struct file *file, void *priv,
1608 struct v4l2_queryctrl *qc)
1609{
1610 int i;
1611
1612 if (qc->id < V4L2_CID_BASE ||
1613 qc->id >= V4L2_CID_LASTP1)
1614 return -EINVAL;
1615
1616 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1617 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1618 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1619 return 0;
1620 }
1621 }
1622
1623 return -EINVAL;
1624}
1625
195a4ef6
MCC
1626/*
1627 * em28xx_v4l2_open()
1628 * inits the device and starts isoc transfer
1629 */
bec43661 1630static int em28xx_v4l2_open(struct file *filp)
195a4ef6 1631{
bec43661 1632 int minor = video_devdata(filp)->minor;
1a23f81b
MCC
1633 int errCode = 0, radio;
1634 struct em28xx *dev;
1635 enum v4l2_buf_type fh_type;
195a4ef6 1636 struct em28xx_fh *fh;
2d50f847 1637
bec43661 1638 dev = em28xx_get_device(minor, &fh_type, &radio);
efc52a94 1639
818a557e 1640 if (NULL == dev)
195a4ef6 1641 return -ENODEV;
818a557e
MCC
1642
1643 mutex_lock(&dev->lock);
2d50f847 1644
195a4ef6 1645 em28xx_videodbg("open minor=%d type=%s users=%d\n",
d7aa8020 1646 minor, v4l2_type_names[fh_type], dev->users);
2d50f847 1647
9aeb4b05 1648
c67ec53f 1649 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
195a4ef6
MCC
1650 if (!fh) {
1651 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
818a557e 1652 mutex_unlock(&dev->lock);
195a4ef6
MCC
1653 return -ENOMEM;
1654 }
195a4ef6 1655 fh->dev = dev;
0be43754 1656 fh->radio = radio;
d7aa8020 1657 fh->type = fh_type;
195a4ef6 1658 filp->private_data = fh;
9aeb4b05 1659
d7aa8020 1660 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
c67ec53f 1661 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
3687e1e6 1662 em28xx_set_alternate(dev);
195a4ef6 1663 em28xx_resolution_set(dev);
2d50f847 1664
c67ec53f
MCC
1665 /* Needed, since GPIO might have disabled power of
1666 some i2c device
1667 */
1a23f81b 1668 em28xx_wake_i2c(dev);
c67ec53f 1669
2d50f847 1670 }
0be43754
MCC
1671 if (fh->radio) {
1672 em28xx_videodbg("video_open: setting radio device\n");
f2cf250a 1673 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
0be43754 1674 }
2d50f847 1675
195a4ef6 1676 dev->users++;
2d50f847 1677
ad0ebb96
MCC
1678 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1679 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1680 sizeof(struct em28xx_buffer), fh);
1681
195a4ef6 1682 mutex_unlock(&dev->lock);
c67ec53f 1683
195a4ef6 1684 return errCode;
2d50f847 1685}
e5589bef 1686
a6c2ba28 1687/*
195a4ef6
MCC
1688 * em28xx_realease_resources()
1689 * unregisters the v4l2,i2c and usb devices
1690 * called when the device gets disconected or at module unload
1691*/
1a23f81b 1692void em28xx_release_analog_resources(struct em28xx *dev)
a6c2ba28 1693{
a6c2ba28 1694
195a4ef6 1695 /*FIXME: I2C IR should be disconnected */
a6c2ba28 1696
0be43754
MCC
1697 if (dev->radio_dev) {
1698 if (-1 != dev->radio_dev->minor)
1699 video_unregister_device(dev->radio_dev);
1700 else
1701 video_device_release(dev->radio_dev);
1702 dev->radio_dev = NULL;
1703 }
1704 if (dev->vbi_dev) {
49240444
DH
1705 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1706 dev->vbi_dev->num);
0be43754
MCC
1707 if (-1 != dev->vbi_dev->minor)
1708 video_unregister_device(dev->vbi_dev);
1709 else
1710 video_device_release(dev->vbi_dev);
1711 dev->vbi_dev = NULL;
1712 }
1713 if (dev->vdev) {
49240444
DH
1714 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1715 dev->vdev->num);
0be43754
MCC
1716 if (-1 != dev->vdev->minor)
1717 video_unregister_device(dev->vdev);
1718 else
1719 video_device_release(dev->vdev);
1720 dev->vdev = NULL;
1721 }
195a4ef6 1722}
a6c2ba28 1723
195a4ef6
MCC
1724/*
1725 * em28xx_v4l2_close()
6ea54d93
DSL
1726 * stops streaming and deallocates all resources allocated by the v4l2
1727 * calls and ioctls
195a4ef6 1728 */
bec43661 1729static int em28xx_v4l2_close(struct file *filp)
195a4ef6
MCC
1730{
1731 struct em28xx_fh *fh = filp->private_data;
1732 struct em28xx *dev = fh->dev;
1733 int errCode;
a6c2ba28 1734
195a4ef6 1735 em28xx_videodbg("users=%d\n", dev->users);
a6c2ba28 1736
a6c2ba28 1737
7831364f 1738 mutex_lock(&dev->lock);
195a4ef6
MCC
1739 if (res_check(fh))
1740 res_free(fh);
a6c2ba28 1741
195a4ef6 1742 if (dev->users == 1) {
ad0ebb96
MCC
1743 videobuf_stop(&fh->vb_vidq);
1744 videobuf_mmap_free(&fh->vb_vidq);
a6c2ba28 1745
195a4ef6
MCC
1746 /* the device is already disconnect,
1747 free the remaining resources */
1748 if (dev->state & DEV_DISCONNECTED) {
1749 em28xx_release_resources(dev);
1750 mutex_unlock(&dev->lock);
1751 kfree(dev);
1752 return 0;
1753 }
a6c2ba28 1754
eb6c9634 1755 /* Save some power by putting tuner to sleep */
7c9fc9d5 1756 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_standby);
eb6c9634 1757
d7aa8020
AT
1758 /* do this before setting alternate! */
1759 em28xx_uninit_isoc(dev);
2fe3e2ee 1760 em28xx_set_mode(dev, EM28XX_SUSPEND);
d7aa8020 1761
195a4ef6
MCC
1762 /* set alternate 0 */
1763 dev->alt = 0;
1764 em28xx_videodbg("setting alternate 0\n");
1765 errCode = usb_set_interface(dev->udev, 0, 0);
1766 if (errCode < 0) {
1767 em28xx_errdev("cannot change alternate number to "
1768 "0 (error=%i)\n", errCode);
1769 }
9aeb4b05 1770 }
195a4ef6
MCC
1771 kfree(fh);
1772 dev->users--;
1773 wake_up_interruptible_nr(&dev->open, 1);
1774 mutex_unlock(&dev->lock);
1775 return 0;
1776}
a6c2ba28 1777
195a4ef6
MCC
1778/*
1779 * em28xx_v4l2_read()
1780 * will allocate buffers when called for the first time
1781 */
1782static ssize_t
6ea54d93 1783em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
f245e549 1784 loff_t *pos)
195a4ef6 1785{
195a4ef6
MCC
1786 struct em28xx_fh *fh = filp->private_data;
1787 struct em28xx *dev = fh->dev;
ad0ebb96
MCC
1788 int rc;
1789
1790 rc = check_dev(dev);
1791 if (rc < 0)
1792 return rc;
a6c2ba28 1793
195a4ef6
MCC
1794 /* FIXME: read() is not prepared to allow changing the video
1795 resolution while streaming. Seems a bug at em28xx_set_fmt
1796 */
a6c2ba28 1797
ad0ebb96 1798 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
29b59417
MCC
1799 mutex_lock(&dev->lock);
1800 rc = res_get(fh);
1801 mutex_unlock(&dev->lock);
1802
1803 if (unlikely(rc < 0))
1804 return rc;
195a4ef6 1805
ad0ebb96
MCC
1806 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1807 filp->f_flags & O_NONBLOCK);
195a4ef6 1808 }
ad0ebb96 1809 return 0;
a6c2ba28 1810}
1811
1812/*
195a4ef6
MCC
1813 * em28xx_v4l2_poll()
1814 * will allocate buffers when called for the first time
a6c2ba28 1815 */
a1a6ee74 1816static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
a6c2ba28 1817{
a3a048ce 1818 struct em28xx_fh *fh = filp->private_data;
195a4ef6 1819 struct em28xx *dev = fh->dev;
ad0ebb96
MCC
1820 int rc;
1821
1822 rc = check_dev(dev);
1823 if (rc < 0)
1824 return rc;
a6c2ba28 1825
29b59417
MCC
1826 mutex_lock(&dev->lock);
1827 rc = res_get(fh);
1828 mutex_unlock(&dev->lock);
1829
1830 if (unlikely(rc < 0))
195a4ef6 1831 return POLLERR;
a6c2ba28 1832
ad0ebb96
MCC
1833 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1834 return POLLERR;
a6c2ba28 1835
ad0ebb96 1836 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
195a4ef6 1837}
a6c2ba28 1838
195a4ef6
MCC
1839/*
1840 * em28xx_v4l2_mmap()
1841 */
1842static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1843{
1844 struct em28xx_fh *fh = filp->private_data;
1845 struct em28xx *dev = fh->dev;
ad0ebb96 1846 int rc;
a6c2ba28 1847
ad0ebb96
MCC
1848 rc = check_dev(dev);
1849 if (rc < 0)
1850 return rc;
a6c2ba28 1851
29b59417
MCC
1852 mutex_lock(&dev->lock);
1853 rc = res_get(fh);
1854 mutex_unlock(&dev->lock);
1855
1856 if (unlikely(rc < 0))
1857 return rc;
1858
ad0ebb96 1859 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
a6c2ba28 1860
ad0ebb96
MCC
1861 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1862 (unsigned long)vma->vm_start,
1863 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1864 rc);
a6c2ba28 1865
ad0ebb96 1866 return rc;
a6c2ba28 1867}
1868
bec43661 1869static const struct v4l2_file_operations em28xx_v4l_fops = {
195a4ef6
MCC
1870 .owner = THIS_MODULE,
1871 .open = em28xx_v4l2_open,
1872 .release = em28xx_v4l2_close,
1873 .read = em28xx_v4l2_read,
1874 .poll = em28xx_v4l2_poll,
1875 .mmap = em28xx_v4l2_mmap,
1876 .ioctl = video_ioctl2,
195a4ef6 1877};
17cbe2e5 1878
a399810c 1879static const struct v4l2_ioctl_ops video_ioctl_ops = {
195a4ef6 1880 .vidioc_querycap = vidioc_querycap,
78b526a4
HV
1881 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1882 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1883 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1884 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
195a4ef6
MCC
1885 .vidioc_g_audio = vidioc_g_audio,
1886 .vidioc_s_audio = vidioc_s_audio,
1887 .vidioc_cropcap = vidioc_cropcap,
1888
78b526a4
HV
1889 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1890 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1891 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
195a4ef6
MCC
1892
1893 .vidioc_reqbufs = vidioc_reqbufs,
1894 .vidioc_querybuf = vidioc_querybuf,
1895 .vidioc_qbuf = vidioc_qbuf,
1896 .vidioc_dqbuf = vidioc_dqbuf,
1897 .vidioc_s_std = vidioc_s_std,
1898 .vidioc_enum_input = vidioc_enum_input,
1899 .vidioc_g_input = vidioc_g_input,
1900 .vidioc_s_input = vidioc_s_input,
1901 .vidioc_queryctrl = vidioc_queryctrl,
1902 .vidioc_g_ctrl = vidioc_g_ctrl,
1903 .vidioc_s_ctrl = vidioc_s_ctrl,
1904 .vidioc_streamon = vidioc_streamon,
1905 .vidioc_streamoff = vidioc_streamoff,
1906 .vidioc_g_tuner = vidioc_g_tuner,
1907 .vidioc_s_tuner = vidioc_s_tuner,
1908 .vidioc_g_frequency = vidioc_g_frequency,
1909 .vidioc_s_frequency = vidioc_s_frequency,
1e7ad56f
MCC
1910#ifdef CONFIG_VIDEO_ADV_DEBUG
1911 .vidioc_g_register = vidioc_g_register,
1912 .vidioc_s_register = vidioc_s_register,
14983d81 1913 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1e7ad56f 1914#endif
ad0ebb96
MCC
1915#ifdef CONFIG_VIDEO_V4L1_COMPAT
1916 .vidiocgmbuf = vidiocgmbuf,
1917#endif
a399810c
HV
1918};
1919
1920static const struct video_device em28xx_video_template = {
1921 .fops = &em28xx_v4l_fops,
1922 .release = video_device_release,
1923 .ioctl_ops = &video_ioctl_ops,
1924
1925 .minor = -1,
195a4ef6
MCC
1926
1927 .tvnorms = V4L2_STD_ALL,
7d497f8a 1928 .current_norm = V4L2_STD_PAL,
a6c2ba28 1929};
1930
bec43661 1931static const struct v4l2_file_operations radio_fops = {
a399810c
HV
1932 .owner = THIS_MODULE,
1933 .open = em28xx_v4l2_open,
1934 .release = em28xx_v4l2_close,
1935 .ioctl = video_ioctl2,
a399810c
HV
1936};
1937
1938static const struct v4l2_ioctl_ops radio_ioctl_ops = {
0be43754
MCC
1939 .vidioc_querycap = radio_querycap,
1940 .vidioc_g_tuner = radio_g_tuner,
1941 .vidioc_enum_input = radio_enum_input,
1942 .vidioc_g_audio = radio_g_audio,
1943 .vidioc_s_tuner = radio_s_tuner,
1944 .vidioc_s_audio = radio_s_audio,
1945 .vidioc_s_input = radio_s_input,
1946 .vidioc_queryctrl = radio_queryctrl,
1947 .vidioc_g_ctrl = vidioc_g_ctrl,
1948 .vidioc_s_ctrl = vidioc_s_ctrl,
1949 .vidioc_g_frequency = vidioc_g_frequency,
1950 .vidioc_s_frequency = vidioc_s_frequency,
1e7ad56f
MCC
1951#ifdef CONFIG_VIDEO_ADV_DEBUG
1952 .vidioc_g_register = vidioc_g_register,
1953 .vidioc_s_register = vidioc_s_register,
1954#endif
0be43754
MCC
1955};
1956
a399810c
HV
1957static struct video_device em28xx_radio_template = {
1958 .name = "em28xx-radio",
a399810c
HV
1959 .fops = &radio_fops,
1960 .ioctl_ops = &radio_ioctl_ops,
1961 .minor = -1,
1962};
1963
6ea54d93 1964/******************************** usb interface ******************************/
a6c2ba28 1965
6d79468d 1966
6d79468d 1967
532fe652 1968static struct video_device *em28xx_vdev_init(struct em28xx *dev,
a1a6ee74
NS
1969 const struct video_device *template,
1970 const char *type_name)
0be43754
MCC
1971{
1972 struct video_device *vfd;
1973
1974 vfd = video_device_alloc();
1975 if (NULL == vfd)
1976 return NULL;
f2cf250a
DSL
1977
1978 *vfd = *template;
1979 vfd->minor = -1;
1980 vfd->v4l2_dev = &dev->v4l2_dev;
1981 vfd->release = video_device_release;
1982 vfd->debug = video_debug;
0be43754
MCC
1983
1984 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1985 dev->name, type_name);
1986
1987 return vfd;
1988}
1989
2e5ef2df 1990int em28xx_register_analog_devices(struct em28xx *dev)
1a23f81b 1991{
6e7b9ea0 1992 u8 val;
2e5ef2df
MCC
1993 int ret;
1994
1a23f81b
MCC
1995 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
1996 dev->name,
1997 (EM28XX_VERSION_CODE >> 16) & 0xff,
1998 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
1999
24c3c415
MCC
2000 /* set default norm */
2001 dev->norm = em28xx_video_template.current_norm;
24c3c415 2002 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
24c3c415
MCC
2003 dev->ctl_input = 0;
2004
1a23f81b
MCC
2005 /* Analog specific initialization */
2006 dev->format = &format[0];
ed5f1431
MCC
2007 em28xx_set_video_format(dev, format[0].fourcc,
2008 norm_maxw(dev), norm_maxh(dev));
2009
24c3c415
MCC
2010 video_mux(dev, dev->ctl_input);
2011
2012 /* Audio defaults */
2013 dev->mute = 1;
2014 dev->volume = 0x1f;
1a23f81b
MCC
2015
2016 /* enable vbi capturing */
2017
2018/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
a1a6ee74
NS
2019 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2020 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2021 (EM28XX_XCLK_AUDIO_UNMUTE | val));
1a23f81b
MCC
2022 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
2023
1a23f81b
MCC
2024 em28xx_set_outfmt(dev);
2025 em28xx_colorlevels_set_default(dev);
2026 em28xx_compression_disable(dev);
2027
818a557e
MCC
2028 /* allocate and fill video video_device struct */
2029 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2030 if (!dev->vdev) {
2031 em28xx_errdev("cannot allocate video_device.\n");
2032 return -ENODEV;
2033 }
2034
2035 /* register v4l2 video video_device */
2036 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2037 video_nr[dev->devno]);
2038 if (ret) {
2039 em28xx_errdev("unable to register video device (error=%i).\n",
2040 ret);
2041 return ret;
2042 }
2043
2044 /* Allocate and fill vbi video_device struct */
2045 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2046
2047 /* register v4l2 vbi video_device */
2048 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2049 vbi_nr[dev->devno]);
2050 if (ret < 0) {
2051 em28xx_errdev("unable to register vbi device\n");
2052 return ret;
2053 }
2054
2055 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
a1a6ee74
NS
2056 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2057 "radio");
818a557e
MCC
2058 if (!dev->radio_dev) {
2059 em28xx_errdev("cannot allocate video_device.\n");
2060 return -ENODEV;
2061 }
2062 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2063 radio_nr[dev->devno]);
2064 if (ret < 0) {
2065 em28xx_errdev("can't register radio device\n");
2066 return ret;
2067 }
2068 em28xx_info("Registered radio device as /dev/radio%d\n",
2069 dev->radio_dev->num);
2070 }
2071
2072 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2073 dev->vdev->num, dev->vbi_dev->num);
2074
2075 return 0;
2076}