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