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