V4L/DVB (11370): v4l2-subdev: move s_std from tuner to core.
[linux-2.6-xlnx.git] / drivers / media / video / em28xx / em28xx-video.c
blob96487843a4737e5d40ec54497a0c188f09d7eea9
1 /*
2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
5 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
7 Mauro Carvalho Chehab <mchehab@infradead.org>
8 Sascha Sommer <saschasommer@freenet.de>
10 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
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.
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.
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.
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/bitmap.h>
33 #include <linux/usb.h>
34 #include <linux/i2c.h>
35 #include <linux/version.h>
36 #include <linux/mm.h>
37 #include <linux/mutex.h>
39 #include "em28xx.h"
40 #include <media/v4l2-common.h>
41 #include <media/v4l2-ioctl.h>
42 #include <media/v4l2-chip-ident.h>
43 #include <media/msp3400.h>
44 #include <media/tuner.h>
46 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
47 "Markus Rechberger <mrechberger@gmail.com>, " \
48 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
49 "Sascha Sommer <saschasommer@freenet.de>"
51 #define DRIVER_DESC "Empia em28xx based USB video device driver"
52 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
54 #define em28xx_videodbg(fmt, arg...) do {\
55 if (video_debug) \
56 printk(KERN_INFO "%s %s :"fmt, \
57 dev->name, __func__ , ##arg); } while (0)
59 static unsigned int isoc_debug;
60 module_param(isoc_debug, int, 0644);
61 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
63 #define em28xx_isocdbg(fmt, arg...) \
64 do {\
65 if (isoc_debug) { \
66 printk(KERN_INFO "%s %s :"fmt, \
67 dev->name, __func__ , ##arg); \
68 } \
69 } while (0)
71 MODULE_AUTHOR(DRIVER_AUTHOR);
72 MODULE_DESCRIPTION(DRIVER_DESC);
73 MODULE_LICENSE("GPL");
75 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
76 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
77 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79 module_param_array(video_nr, int, NULL, 0444);
80 module_param_array(vbi_nr, int, NULL, 0444);
81 module_param_array(radio_nr, int, NULL, 0444);
82 MODULE_PARM_DESC(video_nr, "video device numbers");
83 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
84 MODULE_PARM_DESC(radio_nr, "radio device numbers");
86 static unsigned int video_debug;
87 module_param(video_debug, int, 0644);
88 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
90 /* supported video standards */
91 static struct em28xx_fmt format[] = {
93 .name = "16bpp YUY2, 4:2:2, packed",
94 .fourcc = V4L2_PIX_FMT_YUYV,
95 .depth = 16,
96 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
100 /* supported controls */
101 /* Common to all boards */
102 static struct v4l2_queryctrl em28xx_qctrl[] = {
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,
112 }, {
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,
124 /* ------------------------------------------------------------------
125 DMA and thread functions
126 ------------------------------------------------------------------*/
129 * Announces that a buffer were filled and request the next
131 static inline void buffer_filled(struct em28xx *dev,
132 struct em28xx_dmaqueue *dma_q,
133 struct em28xx_buffer *buf)
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);
141 dev->isoc_ctl.buf = NULL;
143 list_del(&buf->vb.queue);
144 wake_up(&buf->vb.done);
148 * Identify the buffer header type and properly handles
150 static 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)
156 void *fieldstart, *startwrite, *startread;
157 int linesdone, currlinedone, offset, lencopy, remain;
158 int bytesperline = dev->width << 1;
160 if (dma_q->pos + len > buf->vb.size)
161 len = buf->vb.size - dma_q->pos;
163 if (p[0] != 0x88 && p[0] != 0x22) {
164 em28xx_isocdbg("frame is not complete\n");
165 len += 4;
166 } else
167 p += 4;
169 startread = p;
170 remain = len;
172 /* Interlaces frame */
173 if (buf->top_field)
174 fieldstart = outp;
175 else
176 fieldstart = outp + bytesperline;
178 linesdone = dma_q->pos / bytesperline;
179 currlinedone = dma_q->pos % bytesperline;
180 offset = linesdone * bytesperline * 2 + currlinedone;
181 startwrite = fieldstart + offset;
182 lencopy = bytesperline - currlinedone;
183 lencopy = lencopy > remain ? remain : lencopy;
185 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
186 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
187 ((char *)startwrite + lencopy) -
188 ((char *)outp + buf->vb.size));
189 remain = (char *)outp + buf->vb.size - (char *)startwrite;
190 lencopy = remain;
192 if (lencopy <= 0)
193 return;
194 memcpy(startwrite, startread, lencopy);
196 remain -= lencopy;
198 while (remain > 0) {
199 startwrite += lencopy + bytesperline;
200 startread += lencopy;
201 if (bytesperline > remain)
202 lencopy = remain;
203 else
204 lencopy = bytesperline;
206 if ((char *)startwrite + lencopy > (char *)outp +
207 buf->vb.size) {
208 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
209 ((char *)startwrite + lencopy) -
210 ((char *)outp + buf->vb.size));
211 lencopy = remain = (char *)outp + buf->vb.size -
212 (char *)startwrite;
214 if (lencopy <= 0)
215 break;
217 memcpy(startwrite, startread, lencopy);
219 remain -= lencopy;
222 dma_q->pos += len;
225 static inline void print_err_status(struct em28xx *dev,
226 int packet, int status)
228 char *errmsg = "Unknown";
230 switch (status) {
231 case -ENOENT:
232 errmsg = "unlinked synchronuously";
233 break;
234 case -ECONNRESET:
235 errmsg = "unlinked asynchronuously";
236 break;
237 case -ENOSR:
238 errmsg = "Buffer error (overrun)";
239 break;
240 case -EPIPE:
241 errmsg = "Stalled (device not responding)";
242 break;
243 case -EOVERFLOW:
244 errmsg = "Babble (bad cable?)";
245 break;
246 case -EPROTO:
247 errmsg = "Bit-stuff error (bad cable?)";
248 break;
249 case -EILSEQ:
250 errmsg = "CRC/Timeout (could be anything)";
251 break;
252 case -ETIME:
253 errmsg = "Device does not respond";
254 break;
256 if (packet < 0) {
257 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
258 } else {
259 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
260 packet, status, errmsg);
265 * video-buf generic routine to get the next available buffer
267 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
268 struct em28xx_buffer **buf)
270 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
271 char *outp;
273 if (list_empty(&dma_q->active)) {
274 em28xx_isocdbg("No active queue to serve\n");
275 dev->isoc_ctl.buf = NULL;
276 *buf = NULL;
277 return;
280 /* Get the next buffer */
281 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
283 /* Cleans up buffer - Usefull for testing for frame/URB loss */
284 outp = videobuf_to_vmalloc(&(*buf)->vb);
285 memset(outp, 0, (*buf)->vb.size);
287 dev->isoc_ctl.buf = *buf;
289 return;
293 * Controls the isoc copy of each urb packet
295 static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
297 struct em28xx_buffer *buf;
298 struct em28xx_dmaqueue *dma_q = urb->context;
299 unsigned char *outp = NULL;
300 int i, len = 0, rc = 1;
301 unsigned char *p;
303 if (!dev)
304 return 0;
306 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
307 return 0;
309 if (urb->status < 0) {
310 print_err_status(dev, -1, urb->status);
311 if (urb->status == -ENOENT)
312 return 0;
315 buf = dev->isoc_ctl.buf;
316 if (buf != NULL)
317 outp = videobuf_to_vmalloc(&buf->vb);
319 for (i = 0; i < urb->number_of_packets; i++) {
320 int status = urb->iso_frame_desc[i].status;
322 if (status < 0) {
323 print_err_status(dev, i, status);
324 if (urb->iso_frame_desc[i].status != -EPROTO)
325 continue;
328 len = urb->iso_frame_desc[i].actual_length - 4;
330 if (urb->iso_frame_desc[i].actual_length <= 0) {
331 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
332 continue;
334 if (urb->iso_frame_desc[i].actual_length >
335 dev->max_pkt_size) {
336 em28xx_isocdbg("packet bigger than packet size");
337 continue;
340 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
342 /* FIXME: incomplete buffer checks where removed to make
343 logic simpler. Impacts of those changes should be evaluated
345 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
346 em28xx_isocdbg("VBI HEADER!!!\n");
347 /* FIXME: Should add vbi copy */
348 continue;
350 if (p[0] == 0x22 && p[1] == 0x5a) {
351 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
352 len, (p[2] & 1) ? "odd" : "even");
354 if (!(p[2] & 1)) {
355 if (buf != NULL)
356 buffer_filled(dev, dma_q, buf);
357 get_next_buf(dma_q, &buf);
358 if (buf == NULL)
359 outp = NULL;
360 else
361 outp = videobuf_to_vmalloc(&buf->vb);
364 if (buf != NULL) {
365 if (p[2] & 1)
366 buf->top_field = 0;
367 else
368 buf->top_field = 1;
371 dma_q->pos = 0;
373 if (buf != NULL)
374 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
376 return rc;
379 /* ------------------------------------------------------------------
380 Videobuf operations
381 ------------------------------------------------------------------*/
383 static int
384 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
386 struct em28xx_fh *fh = vq->priv_data;
387 struct em28xx *dev = fh->dev;
388 struct v4l2_frequency f;
390 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
392 if (0 == *count)
393 *count = EM28XX_DEF_BUF;
395 if (*count < EM28XX_MIN_BUF)
396 *count = EM28XX_MIN_BUF;
398 /* Ask tuner to go to analog or radio mode */
399 memset(&f, 0, sizeof(f));
400 f.frequency = dev->ctl_freq;
401 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
403 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
405 return 0;
408 /* This is called *without* dev->slock held; please keep it that way */
409 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
411 struct em28xx_fh *fh = vq->priv_data;
412 struct em28xx *dev = fh->dev;
413 unsigned long flags = 0;
414 if (in_interrupt())
415 BUG();
417 /* We used to wait for the buffer to finish here, but this didn't work
418 because, as we were keeping the state as VIDEOBUF_QUEUED,
419 videobuf_queue_cancel marked it as finished for us.
420 (Also, it could wedge forever if the hardware was misconfigured.)
422 This should be safe; by the time we get here, the buffer isn't
423 queued anymore. If we ever start marking the buffers as
424 VIDEOBUF_ACTIVE, it won't be, though.
426 spin_lock_irqsave(&dev->slock, flags);
427 if (dev->isoc_ctl.buf == buf)
428 dev->isoc_ctl.buf = NULL;
429 spin_unlock_irqrestore(&dev->slock, flags);
431 videobuf_vmalloc_free(&buf->vb);
432 buf->vb.state = VIDEOBUF_NEEDS_INIT;
435 static int
436 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
437 enum v4l2_field field)
439 struct em28xx_fh *fh = vq->priv_data;
440 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
441 struct em28xx *dev = fh->dev;
442 int rc = 0, urb_init = 0;
444 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
446 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
447 return -EINVAL;
449 buf->vb.width = dev->width;
450 buf->vb.height = dev->height;
451 buf->vb.field = field;
453 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
454 rc = videobuf_iolock(vq, &buf->vb, NULL);
455 if (rc < 0)
456 goto fail;
459 if (!dev->isoc_ctl.num_bufs)
460 urb_init = 1;
462 if (urb_init) {
463 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
464 EM28XX_NUM_BUFS, dev->max_pkt_size,
465 em28xx_isoc_copy);
466 if (rc < 0)
467 goto fail;
470 buf->vb.state = VIDEOBUF_PREPARED;
471 return 0;
473 fail:
474 free_buffer(vq, buf);
475 return rc;
478 static void
479 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
481 struct em28xx_buffer *buf = container_of(vb,
482 struct em28xx_buffer,
483 vb);
484 struct em28xx_fh *fh = vq->priv_data;
485 struct em28xx *dev = fh->dev;
486 struct em28xx_dmaqueue *vidq = &dev->vidq;
488 buf->vb.state = VIDEOBUF_QUEUED;
489 list_add_tail(&buf->vb.queue, &vidq->active);
493 static void buffer_release(struct videobuf_queue *vq,
494 struct videobuf_buffer *vb)
496 struct em28xx_buffer *buf = container_of(vb,
497 struct em28xx_buffer,
498 vb);
499 struct em28xx_fh *fh = vq->priv_data;
500 struct em28xx *dev = (struct em28xx *)fh->dev;
502 em28xx_isocdbg("em28xx: called buffer_release\n");
504 free_buffer(vq, buf);
507 static struct videobuf_queue_ops em28xx_video_qops = {
508 .buf_setup = buffer_setup,
509 .buf_prepare = buffer_prepare,
510 .buf_queue = buffer_queue,
511 .buf_release = buffer_release,
514 /********************* v4l2 interface **************************************/
516 static void video_mux(struct em28xx *dev, int index)
518 struct v4l2_routing route;
520 route.input = INPUT(index)->vmux;
521 route.output = 0;
522 dev->ctl_input = index;
523 dev->ctl_ainput = INPUT(index)->amux;
524 dev->ctl_aoutput = INPUT(index)->aout;
526 if (!dev->ctl_aoutput)
527 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
529 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing, &route);
531 if (dev->board.has_msp34xx) {
532 if (dev->i2s_speed) {
533 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
534 s_i2s_clock_freq, dev->i2s_speed);
536 route.input = dev->ctl_ainput;
537 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
539 /* Note: this is msp3400 specific */
540 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing, &route);
543 if (dev->board.adecoder != EM28XX_NOADECODER) {
544 route.input = dev->ctl_ainput;
545 route.output = dev->ctl_aoutput;
547 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing, &route);
550 em28xx_audio_analog_set(dev);
553 /* Usage lock check functions */
554 static int res_get(struct em28xx_fh *fh)
556 struct em28xx *dev = fh->dev;
557 int rc = 0;
559 /* This instance already has stream_on */
560 if (fh->stream_on)
561 return rc;
563 if (dev->stream_on)
564 return -EBUSY;
566 dev->stream_on = 1;
567 fh->stream_on = 1;
568 return rc;
571 static int res_check(struct em28xx_fh *fh)
573 return fh->stream_on;
576 static void res_free(struct em28xx_fh *fh)
578 struct em28xx *dev = fh->dev;
580 fh->stream_on = 0;
581 dev->stream_on = 0;
585 * em28xx_get_ctrl()
586 * return the current saturation, brightness or contrast, mute state
588 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
590 switch (ctrl->id) {
591 case V4L2_CID_AUDIO_MUTE:
592 ctrl->value = dev->mute;
593 return 0;
594 case V4L2_CID_AUDIO_VOLUME:
595 ctrl->value = dev->volume;
596 return 0;
597 default:
598 return -EINVAL;
603 * em28xx_set_ctrl()
604 * mute or set new saturation, brightness or contrast
606 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
608 switch (ctrl->id) {
609 case V4L2_CID_AUDIO_MUTE:
610 if (ctrl->value != dev->mute) {
611 dev->mute = ctrl->value;
612 return em28xx_audio_analog_set(dev);
614 return 0;
615 case V4L2_CID_AUDIO_VOLUME:
616 dev->volume = ctrl->value;
617 return em28xx_audio_analog_set(dev);
618 default:
619 return -EINVAL;
623 static int check_dev(struct em28xx *dev)
625 if (dev->state & DEV_DISCONNECTED) {
626 em28xx_errdev("v4l2 ioctl: device not present\n");
627 return -ENODEV;
630 if (dev->state & DEV_MISCONFIGURED) {
631 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
632 "close and open it again\n");
633 return -EIO;
635 return 0;
638 static void get_scale(struct em28xx *dev,
639 unsigned int width, unsigned int height,
640 unsigned int *hscale, unsigned int *vscale)
642 unsigned int maxw = norm_maxw(dev);
643 unsigned int maxh = norm_maxh(dev);
645 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
646 if (*hscale >= 0x4000)
647 *hscale = 0x3fff;
649 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
650 if (*vscale >= 0x4000)
651 *vscale = 0x3fff;
654 /* ------------------------------------------------------------------
655 IOCTL vidioc handling
656 ------------------------------------------------------------------*/
658 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
659 struct v4l2_format *f)
661 struct em28xx_fh *fh = priv;
662 struct em28xx *dev = fh->dev;
664 mutex_lock(&dev->lock);
666 f->fmt.pix.width = dev->width;
667 f->fmt.pix.height = dev->height;
668 f->fmt.pix.pixelformat = dev->format->fourcc;
669 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
670 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
671 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
673 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
674 f->fmt.pix.field = dev->interlaced ?
675 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
677 mutex_unlock(&dev->lock);
678 return 0;
681 static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
683 unsigned int i;
685 for (i = 0; i < ARRAY_SIZE(format); i++)
686 if (format[i].fourcc == fourcc)
687 return &format[i];
689 return NULL;
692 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
693 struct v4l2_format *f)
695 struct em28xx_fh *fh = priv;
696 struct em28xx *dev = fh->dev;
697 int width = f->fmt.pix.width;
698 int height = f->fmt.pix.height;
699 unsigned int maxw = norm_maxw(dev);
700 unsigned int maxh = norm_maxh(dev);
701 unsigned int hscale, vscale;
702 struct em28xx_fmt *fmt;
704 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
705 if (!fmt) {
706 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
707 f->fmt.pix.pixelformat);
708 return -EINVAL;
711 /* width must even because of the YUYV format
712 height must be even because of interlacing */
713 height &= 0xfffe;
714 width &= 0xfffe;
716 if (unlikely(height < 32))
717 height = 32;
718 if (unlikely(height > maxh))
719 height = maxh;
720 if (unlikely(width < 48))
721 width = 48;
722 if (unlikely(width > maxw))
723 width = maxw;
725 if (dev->board.is_em2800) {
726 /* the em2800 can only scale down to 50% */
727 if (height % (maxh / 2))
728 height = maxh;
729 if (width % (maxw / 2))
730 width = maxw;
731 /* according to empiatech support */
732 /* the MaxPacketSize is to small to support */
733 /* framesizes larger than 640x480 @ 30 fps */
734 /* or 640x576 @ 25 fps. As this would cut */
735 /* of a part of the image we prefer */
736 /* 360x576 or 360x480 for now */
737 if (width == maxw && height == maxh)
738 width /= 2;
741 get_scale(dev, width, height, &hscale, &vscale);
743 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
744 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
746 f->fmt.pix.width = width;
747 f->fmt.pix.height = height;
748 f->fmt.pix.pixelformat = fmt->fourcc;
749 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
750 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
751 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
752 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
754 return 0;
757 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
758 struct v4l2_format *f)
760 struct em28xx_fh *fh = priv;
761 struct em28xx *dev = fh->dev;
762 int rc;
763 struct em28xx_fmt *fmt;
765 rc = check_dev(dev);
766 if (rc < 0)
767 return rc;
769 mutex_lock(&dev->lock);
771 vidioc_try_fmt_vid_cap(file, priv, f);
773 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
774 if (!fmt) {
775 rc = -EINVAL;
776 goto out;
779 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
780 em28xx_errdev("%s queue busy\n", __func__);
781 rc = -EBUSY;
782 goto out;
785 if (dev->stream_on && !fh->stream_on) {
786 em28xx_errdev("%s device in use by another fh\n", __func__);
787 rc = -EBUSY;
788 goto out;
791 /* set new image size */
792 dev->width = f->fmt.pix.width;
793 dev->height = f->fmt.pix.height;
794 dev->format = fmt;
795 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
797 em28xx_set_alternate(dev);
798 em28xx_resolution_set(dev);
800 rc = 0;
802 out:
803 mutex_unlock(&dev->lock);
804 return rc;
807 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
809 struct em28xx_fh *fh = priv;
810 struct em28xx *dev = fh->dev;
811 struct v4l2_format f;
812 int rc;
814 rc = check_dev(dev);
815 if (rc < 0)
816 return rc;
818 mutex_lock(&dev->lock);
819 dev->norm = *norm;
821 /* Adjusts width/height, if needed */
822 f.fmt.pix.width = dev->width;
823 f.fmt.pix.height = dev->height;
824 vidioc_try_fmt_vid_cap(file, priv, &f);
826 /* set new image size */
827 dev->width = f.fmt.pix.width;
828 dev->height = f.fmt.pix.height;
829 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
831 em28xx_resolution_set(dev);
832 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
834 mutex_unlock(&dev->lock);
835 return 0;
838 static const char *iname[] = {
839 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
840 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
841 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
842 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
843 [EM28XX_VMUX_SVIDEO] = "S-Video",
844 [EM28XX_VMUX_TELEVISION] = "Television",
845 [EM28XX_VMUX_CABLE] = "Cable TV",
846 [EM28XX_VMUX_DVB] = "DVB",
847 [EM28XX_VMUX_DEBUG] = "for debug only",
850 static int vidioc_enum_input(struct file *file, void *priv,
851 struct v4l2_input *i)
853 struct em28xx_fh *fh = priv;
854 struct em28xx *dev = fh->dev;
855 unsigned int n;
857 n = i->index;
858 if (n >= MAX_EM28XX_INPUT)
859 return -EINVAL;
860 if (0 == INPUT(n)->type)
861 return -EINVAL;
863 i->index = n;
864 i->type = V4L2_INPUT_TYPE_CAMERA;
866 strcpy(i->name, iname[INPUT(n)->type]);
868 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
869 (EM28XX_VMUX_CABLE == INPUT(n)->type))
870 i->type = V4L2_INPUT_TYPE_TUNER;
872 i->std = dev->vdev->tvnorms;
874 return 0;
877 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
879 struct em28xx_fh *fh = priv;
880 struct em28xx *dev = fh->dev;
882 *i = dev->ctl_input;
884 return 0;
887 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
889 struct em28xx_fh *fh = priv;
890 struct em28xx *dev = fh->dev;
891 int rc;
893 rc = check_dev(dev);
894 if (rc < 0)
895 return rc;
897 if (i >= MAX_EM28XX_INPUT)
898 return -EINVAL;
899 if (0 == INPUT(i)->type)
900 return -EINVAL;
902 dev->ctl_input = i;
904 mutex_lock(&dev->lock);
905 video_mux(dev, dev->ctl_input);
906 mutex_unlock(&dev->lock);
907 return 0;
910 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
912 struct em28xx_fh *fh = priv;
913 struct em28xx *dev = fh->dev;
915 switch (a->index) {
916 case EM28XX_AMUX_VIDEO:
917 strcpy(a->name, "Television");
918 break;
919 case EM28XX_AMUX_LINE_IN:
920 strcpy(a->name, "Line In");
921 break;
922 case EM28XX_AMUX_VIDEO2:
923 strcpy(a->name, "Television alt");
924 break;
925 case EM28XX_AMUX_PHONE:
926 strcpy(a->name, "Phone");
927 break;
928 case EM28XX_AMUX_MIC:
929 strcpy(a->name, "Mic");
930 break;
931 case EM28XX_AMUX_CD:
932 strcpy(a->name, "CD");
933 break;
934 case EM28XX_AMUX_AUX:
935 strcpy(a->name, "Aux");
936 break;
937 case EM28XX_AMUX_PCM_OUT:
938 strcpy(a->name, "PCM");
939 break;
940 default:
941 return -EINVAL;
944 a->index = dev->ctl_ainput;
945 a->capability = V4L2_AUDCAP_STEREO;
947 return 0;
950 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
952 struct em28xx_fh *fh = priv;
953 struct em28xx *dev = fh->dev;
956 if (a->index >= MAX_EM28XX_INPUT)
957 return -EINVAL;
958 if (0 == INPUT(a->index)->type)
959 return -EINVAL;
961 mutex_lock(&dev->lock);
963 dev->ctl_ainput = INPUT(a->index)->amux;
964 dev->ctl_aoutput = INPUT(a->index)->aout;
966 if (!dev->ctl_aoutput)
967 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
969 mutex_unlock(&dev->lock);
970 return 0;
973 static int vidioc_queryctrl(struct file *file, void *priv,
974 struct v4l2_queryctrl *qc)
976 struct em28xx_fh *fh = priv;
977 struct em28xx *dev = fh->dev;
978 int id = qc->id;
979 int i;
980 int rc;
982 rc = check_dev(dev);
983 if (rc < 0)
984 return rc;
986 memset(qc, 0, sizeof(*qc));
988 qc->id = id;
990 if (!dev->board.has_msp34xx) {
991 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
992 if (qc->id && qc->id == em28xx_qctrl[i].id) {
993 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
994 return 0;
999 mutex_lock(&dev->lock);
1000 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
1001 mutex_unlock(&dev->lock);
1003 if (qc->type)
1004 return 0;
1005 else
1006 return -EINVAL;
1009 static int vidioc_g_ctrl(struct file *file, void *priv,
1010 struct v4l2_control *ctrl)
1012 struct em28xx_fh *fh = priv;
1013 struct em28xx *dev = fh->dev;
1014 int rc;
1016 rc = check_dev(dev);
1017 if (rc < 0)
1018 return rc;
1019 rc = 0;
1021 mutex_lock(&dev->lock);
1023 if (dev->board.has_msp34xx)
1024 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1025 else {
1026 rc = em28xx_get_ctrl(dev, ctrl);
1027 if (rc < 0) {
1028 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1029 rc = 0;
1033 mutex_unlock(&dev->lock);
1034 return rc;
1037 static int vidioc_s_ctrl(struct file *file, void *priv,
1038 struct v4l2_control *ctrl)
1040 struct em28xx_fh *fh = priv;
1041 struct em28xx *dev = fh->dev;
1042 u8 i;
1043 int rc;
1045 rc = check_dev(dev);
1046 if (rc < 0)
1047 return rc;
1049 mutex_lock(&dev->lock);
1051 if (dev->board.has_msp34xx)
1052 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
1053 else {
1054 rc = 1;
1055 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1056 if (ctrl->id == em28xx_qctrl[i].id) {
1057 if (ctrl->value < em28xx_qctrl[i].minimum ||
1058 ctrl->value > em28xx_qctrl[i].maximum) {
1059 rc = -ERANGE;
1060 break;
1063 rc = em28xx_set_ctrl(dev, ctrl);
1064 break;
1069 /* Control not found - try to send it to the attached devices */
1070 if (rc == 1) {
1071 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
1072 rc = 0;
1075 mutex_unlock(&dev->lock);
1076 return rc;
1079 static int vidioc_g_tuner(struct file *file, void *priv,
1080 struct v4l2_tuner *t)
1082 struct em28xx_fh *fh = priv;
1083 struct em28xx *dev = fh->dev;
1084 int rc;
1086 rc = check_dev(dev);
1087 if (rc < 0)
1088 return rc;
1090 if (0 != t->index)
1091 return -EINVAL;
1093 strcpy(t->name, "Tuner");
1095 mutex_lock(&dev->lock);
1096 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1097 mutex_unlock(&dev->lock);
1099 return 0;
1102 static int vidioc_s_tuner(struct file *file, void *priv,
1103 struct v4l2_tuner *t)
1105 struct em28xx_fh *fh = priv;
1106 struct em28xx *dev = fh->dev;
1107 int rc;
1109 rc = check_dev(dev);
1110 if (rc < 0)
1111 return rc;
1113 if (0 != t->index)
1114 return -EINVAL;
1116 mutex_lock(&dev->lock);
1117 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1118 mutex_unlock(&dev->lock);
1120 return 0;
1123 static int vidioc_g_frequency(struct file *file, void *priv,
1124 struct v4l2_frequency *f)
1126 struct em28xx_fh *fh = priv;
1127 struct em28xx *dev = fh->dev;
1129 mutex_lock(&dev->lock);
1130 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1131 f->frequency = dev->ctl_freq;
1132 mutex_unlock(&dev->lock);
1134 return 0;
1137 static int vidioc_s_frequency(struct file *file, void *priv,
1138 struct v4l2_frequency *f)
1140 struct em28xx_fh *fh = priv;
1141 struct em28xx *dev = fh->dev;
1142 int rc;
1144 rc = check_dev(dev);
1145 if (rc < 0)
1146 return rc;
1148 if (0 != f->tuner)
1149 return -EINVAL;
1151 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1152 return -EINVAL;
1153 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1154 return -EINVAL;
1156 mutex_lock(&dev->lock);
1158 dev->ctl_freq = f->frequency;
1159 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1161 mutex_unlock(&dev->lock);
1163 return 0;
1166 #ifdef CONFIG_VIDEO_ADV_DEBUG
1167 static int em28xx_reg_len(int reg)
1169 switch (reg) {
1170 case EM28XX_R40_AC97LSB:
1171 case EM28XX_R30_HSCALELOW:
1172 case EM28XX_R32_VSCALELOW:
1173 return 2;
1174 default:
1175 return 1;
1179 static int vidioc_g_chip_ident(struct file *file, void *priv,
1180 struct v4l2_dbg_chip_ident *chip)
1182 struct em28xx_fh *fh = priv;
1183 struct em28xx *dev = fh->dev;
1185 chip->ident = V4L2_IDENT_NONE;
1186 chip->revision = 0;
1188 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
1190 return 0;
1194 static int vidioc_g_register(struct file *file, void *priv,
1195 struct v4l2_dbg_register *reg)
1197 struct em28xx_fh *fh = priv;
1198 struct em28xx *dev = fh->dev;
1199 int ret;
1201 switch (reg->match.type) {
1202 case V4L2_CHIP_MATCH_AC97:
1203 mutex_lock(&dev->lock);
1204 ret = em28xx_read_ac97(dev, reg->reg);
1205 mutex_unlock(&dev->lock);
1206 if (ret < 0)
1207 return ret;
1209 reg->val = ret;
1210 reg->size = 1;
1211 return 0;
1212 case V4L2_CHIP_MATCH_I2C_DRIVER:
1213 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1214 return 0;
1215 case V4L2_CHIP_MATCH_I2C_ADDR:
1216 /* Not supported yet */
1217 return -EINVAL;
1218 default:
1219 if (!v4l2_chip_match_host(&reg->match))
1220 return -EINVAL;
1223 /* Match host */
1224 reg->size = em28xx_reg_len(reg->reg);
1225 if (reg->size == 1) {
1226 mutex_lock(&dev->lock);
1227 ret = em28xx_read_reg(dev, reg->reg);
1228 mutex_unlock(&dev->lock);
1230 if (ret < 0)
1231 return ret;
1233 reg->val = ret;
1234 } else {
1235 __le16 val = 0;
1236 mutex_lock(&dev->lock);
1237 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1238 reg->reg, (char *)&val, 2);
1239 mutex_unlock(&dev->lock);
1240 if (ret < 0)
1241 return ret;
1243 reg->val = le16_to_cpu(val);
1246 return 0;
1249 static int vidioc_s_register(struct file *file, void *priv,
1250 struct v4l2_dbg_register *reg)
1252 struct em28xx_fh *fh = priv;
1253 struct em28xx *dev = fh->dev;
1254 __le16 buf;
1255 int rc;
1257 switch (reg->match.type) {
1258 case V4L2_CHIP_MATCH_AC97:
1259 mutex_lock(&dev->lock);
1260 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1261 mutex_unlock(&dev->lock);
1263 return rc;
1264 case V4L2_CHIP_MATCH_I2C_DRIVER:
1265 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1266 return 0;
1267 case V4L2_CHIP_MATCH_I2C_ADDR:
1268 /* Not supported yet */
1269 return -EINVAL;
1270 default:
1271 if (!v4l2_chip_match_host(&reg->match))
1272 return -EINVAL;
1275 /* Match host */
1276 buf = cpu_to_le16(reg->val);
1278 mutex_lock(&dev->lock);
1279 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1280 em28xx_reg_len(reg->reg));
1281 mutex_unlock(&dev->lock);
1283 return rc;
1285 #endif
1288 static int vidioc_cropcap(struct file *file, void *priv,
1289 struct v4l2_cropcap *cc)
1291 struct em28xx_fh *fh = priv;
1292 struct em28xx *dev = fh->dev;
1294 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1295 return -EINVAL;
1297 cc->bounds.left = 0;
1298 cc->bounds.top = 0;
1299 cc->bounds.width = dev->width;
1300 cc->bounds.height = dev->height;
1301 cc->defrect = cc->bounds;
1302 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1303 cc->pixelaspect.denominator = 59;
1305 return 0;
1308 static int vidioc_streamon(struct file *file, void *priv,
1309 enum v4l2_buf_type type)
1311 struct em28xx_fh *fh = priv;
1312 struct em28xx *dev = fh->dev;
1313 int rc;
1315 rc = check_dev(dev);
1316 if (rc < 0)
1317 return rc;
1320 mutex_lock(&dev->lock);
1321 rc = res_get(fh);
1323 if (likely(rc >= 0))
1324 rc = videobuf_streamon(&fh->vb_vidq);
1326 mutex_unlock(&dev->lock);
1328 return rc;
1331 static int vidioc_streamoff(struct file *file, void *priv,
1332 enum v4l2_buf_type type)
1334 struct em28xx_fh *fh = priv;
1335 struct em28xx *dev = fh->dev;
1336 int rc;
1338 rc = check_dev(dev);
1339 if (rc < 0)
1340 return rc;
1342 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1343 return -EINVAL;
1344 if (type != fh->type)
1345 return -EINVAL;
1347 mutex_lock(&dev->lock);
1349 videobuf_streamoff(&fh->vb_vidq);
1350 res_free(fh);
1352 mutex_unlock(&dev->lock);
1354 return 0;
1357 static int vidioc_querycap(struct file *file, void *priv,
1358 struct v4l2_capability *cap)
1360 struct em28xx_fh *fh = priv;
1361 struct em28xx *dev = fh->dev;
1363 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1364 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1365 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1367 cap->version = EM28XX_VERSION_CODE;
1369 cap->capabilities =
1370 V4L2_CAP_SLICED_VBI_CAPTURE |
1371 V4L2_CAP_VIDEO_CAPTURE |
1372 V4L2_CAP_AUDIO |
1373 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1375 if (dev->tuner_type != TUNER_ABSENT)
1376 cap->capabilities |= V4L2_CAP_TUNER;
1378 return 0;
1381 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1382 struct v4l2_fmtdesc *f)
1384 if (unlikely(f->index >= ARRAY_SIZE(format)))
1385 return -EINVAL;
1387 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1388 f->pixelformat = format[f->index].fourcc;
1390 return 0;
1393 /* Sliced VBI ioctls */
1394 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1395 struct v4l2_format *f)
1397 struct em28xx_fh *fh = priv;
1398 struct em28xx *dev = fh->dev;
1399 int rc;
1401 rc = check_dev(dev);
1402 if (rc < 0)
1403 return rc;
1405 mutex_lock(&dev->lock);
1407 f->fmt.sliced.service_set = 0;
1408 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
1410 if (f->fmt.sliced.service_set == 0)
1411 rc = -EINVAL;
1413 mutex_unlock(&dev->lock);
1415 return rc;
1418 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1419 struct v4l2_format *f)
1421 struct em28xx_fh *fh = priv;
1422 struct em28xx *dev = fh->dev;
1423 int rc;
1425 rc = check_dev(dev);
1426 if (rc < 0)
1427 return rc;
1429 mutex_lock(&dev->lock);
1430 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
1431 mutex_unlock(&dev->lock);
1433 if (f->fmt.sliced.service_set == 0)
1434 return -EINVAL;
1436 return 0;
1440 static int vidioc_reqbufs(struct file *file, void *priv,
1441 struct v4l2_requestbuffers *rb)
1443 struct em28xx_fh *fh = priv;
1444 struct em28xx *dev = fh->dev;
1445 int rc;
1447 rc = check_dev(dev);
1448 if (rc < 0)
1449 return rc;
1451 return videobuf_reqbufs(&fh->vb_vidq, rb);
1454 static int vidioc_querybuf(struct file *file, void *priv,
1455 struct v4l2_buffer *b)
1457 struct em28xx_fh *fh = priv;
1458 struct em28xx *dev = fh->dev;
1459 int rc;
1461 rc = check_dev(dev);
1462 if (rc < 0)
1463 return rc;
1465 return videobuf_querybuf(&fh->vb_vidq, b);
1468 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1470 struct em28xx_fh *fh = priv;
1471 struct em28xx *dev = fh->dev;
1472 int rc;
1474 rc = check_dev(dev);
1475 if (rc < 0)
1476 return rc;
1478 return videobuf_qbuf(&fh->vb_vidq, b);
1481 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1483 struct em28xx_fh *fh = priv;
1484 struct em28xx *dev = fh->dev;
1485 int rc;
1487 rc = check_dev(dev);
1488 if (rc < 0)
1489 return rc;
1491 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1494 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1495 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1497 struct em28xx_fh *fh = priv;
1499 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1501 #endif
1504 /* ----------------------------------------------------------- */
1505 /* RADIO ESPECIFIC IOCTLS */
1506 /* ----------------------------------------------------------- */
1508 static int radio_querycap(struct file *file, void *priv,
1509 struct v4l2_capability *cap)
1511 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1513 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1514 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1515 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1517 cap->version = EM28XX_VERSION_CODE;
1518 cap->capabilities = V4L2_CAP_TUNER;
1519 return 0;
1522 static int radio_g_tuner(struct file *file, void *priv,
1523 struct v4l2_tuner *t)
1525 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1527 if (unlikely(t->index > 0))
1528 return -EINVAL;
1530 strcpy(t->name, "Radio");
1531 t->type = V4L2_TUNER_RADIO;
1533 mutex_lock(&dev->lock);
1534 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1535 mutex_unlock(&dev->lock);
1537 return 0;
1540 static int radio_enum_input(struct file *file, void *priv,
1541 struct v4l2_input *i)
1543 if (i->index != 0)
1544 return -EINVAL;
1545 strcpy(i->name, "Radio");
1546 i->type = V4L2_INPUT_TYPE_TUNER;
1548 return 0;
1551 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1553 if (unlikely(a->index))
1554 return -EINVAL;
1556 strcpy(a->name, "Radio");
1557 return 0;
1560 static int radio_s_tuner(struct file *file, void *priv,
1561 struct v4l2_tuner *t)
1563 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1565 if (0 != t->index)
1566 return -EINVAL;
1568 mutex_lock(&dev->lock);
1569 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1570 mutex_unlock(&dev->lock);
1572 return 0;
1575 static int radio_s_audio(struct file *file, void *fh,
1576 struct v4l2_audio *a)
1578 return 0;
1581 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1583 return 0;
1586 static int radio_queryctrl(struct file *file, void *priv,
1587 struct v4l2_queryctrl *qc)
1589 int i;
1591 if (qc->id < V4L2_CID_BASE ||
1592 qc->id >= V4L2_CID_LASTP1)
1593 return -EINVAL;
1595 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1596 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1597 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1598 return 0;
1602 return -EINVAL;
1606 * em28xx_v4l2_open()
1607 * inits the device and starts isoc transfer
1609 static int em28xx_v4l2_open(struct file *filp)
1611 int minor = video_devdata(filp)->minor;
1612 int errCode = 0, radio;
1613 struct em28xx *dev;
1614 enum v4l2_buf_type fh_type;
1615 struct em28xx_fh *fh;
1617 dev = em28xx_get_device(minor, &fh_type, &radio);
1619 if (NULL == dev)
1620 return -ENODEV;
1622 mutex_lock(&dev->lock);
1624 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1625 minor, v4l2_type_names[fh_type], dev->users);
1628 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1629 if (!fh) {
1630 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1631 mutex_unlock(&dev->lock);
1632 return -ENOMEM;
1634 fh->dev = dev;
1635 fh->radio = radio;
1636 fh->type = fh_type;
1637 filp->private_data = fh;
1639 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1640 dev->width = norm_maxw(dev);
1641 dev->height = norm_maxh(dev);
1642 dev->hscale = 0;
1643 dev->vscale = 0;
1645 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
1646 em28xx_set_alternate(dev);
1647 em28xx_resolution_set(dev);
1649 /* Needed, since GPIO might have disabled power of
1650 some i2c device
1652 em28xx_wake_i2c(dev);
1655 if (fh->radio) {
1656 em28xx_videodbg("video_open: setting radio device\n");
1657 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1660 dev->users++;
1662 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1663 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1664 sizeof(struct em28xx_buffer), fh);
1666 mutex_unlock(&dev->lock);
1668 return errCode;
1672 * em28xx_realease_resources()
1673 * unregisters the v4l2,i2c and usb devices
1674 * called when the device gets disconected or at module unload
1676 void em28xx_release_analog_resources(struct em28xx *dev)
1679 /*FIXME: I2C IR should be disconnected */
1681 if (dev->radio_dev) {
1682 if (-1 != dev->radio_dev->minor)
1683 video_unregister_device(dev->radio_dev);
1684 else
1685 video_device_release(dev->radio_dev);
1686 dev->radio_dev = NULL;
1688 if (dev->vbi_dev) {
1689 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1690 dev->vbi_dev->num);
1691 if (-1 != dev->vbi_dev->minor)
1692 video_unregister_device(dev->vbi_dev);
1693 else
1694 video_device_release(dev->vbi_dev);
1695 dev->vbi_dev = NULL;
1697 if (dev->vdev) {
1698 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1699 dev->vdev->num);
1700 if (-1 != dev->vdev->minor)
1701 video_unregister_device(dev->vdev);
1702 else
1703 video_device_release(dev->vdev);
1704 dev->vdev = NULL;
1709 * em28xx_v4l2_close()
1710 * stops streaming and deallocates all resources allocated by the v4l2
1711 * calls and ioctls
1713 static int em28xx_v4l2_close(struct file *filp)
1715 struct em28xx_fh *fh = filp->private_data;
1716 struct em28xx *dev = fh->dev;
1717 int errCode;
1719 em28xx_videodbg("users=%d\n", dev->users);
1722 mutex_lock(&dev->lock);
1723 if (res_check(fh))
1724 res_free(fh);
1726 if (dev->users == 1) {
1727 videobuf_stop(&fh->vb_vidq);
1728 videobuf_mmap_free(&fh->vb_vidq);
1730 /* the device is already disconnect,
1731 free the remaining resources */
1732 if (dev->state & DEV_DISCONNECTED) {
1733 em28xx_release_resources(dev);
1734 mutex_unlock(&dev->lock);
1735 kfree(dev);
1736 return 0;
1739 /* Save some power by putting tuner to sleep */
1740 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_standby);
1742 /* do this before setting alternate! */
1743 em28xx_uninit_isoc(dev);
1744 em28xx_set_mode(dev, EM28XX_SUSPEND);
1746 /* set alternate 0 */
1747 dev->alt = 0;
1748 em28xx_videodbg("setting alternate 0\n");
1749 errCode = usb_set_interface(dev->udev, 0, 0);
1750 if (errCode < 0) {
1751 em28xx_errdev("cannot change alternate number to "
1752 "0 (error=%i)\n", errCode);
1755 kfree(fh);
1756 dev->users--;
1757 wake_up_interruptible_nr(&dev->open, 1);
1758 mutex_unlock(&dev->lock);
1759 return 0;
1763 * em28xx_v4l2_read()
1764 * will allocate buffers when called for the first time
1766 static ssize_t
1767 em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1768 loff_t *pos)
1770 struct em28xx_fh *fh = filp->private_data;
1771 struct em28xx *dev = fh->dev;
1772 int rc;
1774 rc = check_dev(dev);
1775 if (rc < 0)
1776 return rc;
1778 /* FIXME: read() is not prepared to allow changing the video
1779 resolution while streaming. Seems a bug at em28xx_set_fmt
1782 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1783 mutex_lock(&dev->lock);
1784 rc = res_get(fh);
1785 mutex_unlock(&dev->lock);
1787 if (unlikely(rc < 0))
1788 return rc;
1790 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1791 filp->f_flags & O_NONBLOCK);
1793 return 0;
1797 * em28xx_v4l2_poll()
1798 * will allocate buffers when called for the first time
1800 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
1802 struct em28xx_fh *fh = filp->private_data;
1803 struct em28xx *dev = fh->dev;
1804 int rc;
1806 rc = check_dev(dev);
1807 if (rc < 0)
1808 return rc;
1810 mutex_lock(&dev->lock);
1811 rc = res_get(fh);
1812 mutex_unlock(&dev->lock);
1814 if (unlikely(rc < 0))
1815 return POLLERR;
1817 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1818 return POLLERR;
1820 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1824 * em28xx_v4l2_mmap()
1826 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1828 struct em28xx_fh *fh = filp->private_data;
1829 struct em28xx *dev = fh->dev;
1830 int rc;
1832 rc = check_dev(dev);
1833 if (rc < 0)
1834 return rc;
1836 mutex_lock(&dev->lock);
1837 rc = res_get(fh);
1838 mutex_unlock(&dev->lock);
1840 if (unlikely(rc < 0))
1841 return rc;
1843 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1845 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1846 (unsigned long)vma->vm_start,
1847 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1848 rc);
1850 return rc;
1853 static const struct v4l2_file_operations em28xx_v4l_fops = {
1854 .owner = THIS_MODULE,
1855 .open = em28xx_v4l2_open,
1856 .release = em28xx_v4l2_close,
1857 .read = em28xx_v4l2_read,
1858 .poll = em28xx_v4l2_poll,
1859 .mmap = em28xx_v4l2_mmap,
1860 .ioctl = video_ioctl2,
1863 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1864 .vidioc_querycap = vidioc_querycap,
1865 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1866 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1867 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1868 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1869 .vidioc_g_audio = vidioc_g_audio,
1870 .vidioc_s_audio = vidioc_s_audio,
1871 .vidioc_cropcap = vidioc_cropcap,
1873 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1874 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1875 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1877 .vidioc_reqbufs = vidioc_reqbufs,
1878 .vidioc_querybuf = vidioc_querybuf,
1879 .vidioc_qbuf = vidioc_qbuf,
1880 .vidioc_dqbuf = vidioc_dqbuf,
1881 .vidioc_s_std = vidioc_s_std,
1882 .vidioc_enum_input = vidioc_enum_input,
1883 .vidioc_g_input = vidioc_g_input,
1884 .vidioc_s_input = vidioc_s_input,
1885 .vidioc_queryctrl = vidioc_queryctrl,
1886 .vidioc_g_ctrl = vidioc_g_ctrl,
1887 .vidioc_s_ctrl = vidioc_s_ctrl,
1888 .vidioc_streamon = vidioc_streamon,
1889 .vidioc_streamoff = vidioc_streamoff,
1890 .vidioc_g_tuner = vidioc_g_tuner,
1891 .vidioc_s_tuner = vidioc_s_tuner,
1892 .vidioc_g_frequency = vidioc_g_frequency,
1893 .vidioc_s_frequency = vidioc_s_frequency,
1894 #ifdef CONFIG_VIDEO_ADV_DEBUG
1895 .vidioc_g_register = vidioc_g_register,
1896 .vidioc_s_register = vidioc_s_register,
1897 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1898 #endif
1899 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1900 .vidiocgmbuf = vidiocgmbuf,
1901 #endif
1904 static const struct video_device em28xx_video_template = {
1905 .fops = &em28xx_v4l_fops,
1906 .release = video_device_release,
1907 .ioctl_ops = &video_ioctl_ops,
1909 .minor = -1,
1911 .tvnorms = V4L2_STD_ALL,
1912 .current_norm = V4L2_STD_PAL,
1915 static const struct v4l2_file_operations radio_fops = {
1916 .owner = THIS_MODULE,
1917 .open = em28xx_v4l2_open,
1918 .release = em28xx_v4l2_close,
1919 .ioctl = video_ioctl2,
1922 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1923 .vidioc_querycap = radio_querycap,
1924 .vidioc_g_tuner = radio_g_tuner,
1925 .vidioc_enum_input = radio_enum_input,
1926 .vidioc_g_audio = radio_g_audio,
1927 .vidioc_s_tuner = radio_s_tuner,
1928 .vidioc_s_audio = radio_s_audio,
1929 .vidioc_s_input = radio_s_input,
1930 .vidioc_queryctrl = radio_queryctrl,
1931 .vidioc_g_ctrl = vidioc_g_ctrl,
1932 .vidioc_s_ctrl = vidioc_s_ctrl,
1933 .vidioc_g_frequency = vidioc_g_frequency,
1934 .vidioc_s_frequency = vidioc_s_frequency,
1935 #ifdef CONFIG_VIDEO_ADV_DEBUG
1936 .vidioc_g_register = vidioc_g_register,
1937 .vidioc_s_register = vidioc_s_register,
1938 #endif
1941 static struct video_device em28xx_radio_template = {
1942 .name = "em28xx-radio",
1943 .fops = &radio_fops,
1944 .ioctl_ops = &radio_ioctl_ops,
1945 .minor = -1,
1948 /******************************** usb interface ******************************/
1952 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1953 const struct video_device *template,
1954 const char *type_name)
1956 struct video_device *vfd;
1958 vfd = video_device_alloc();
1959 if (NULL == vfd)
1960 return NULL;
1962 *vfd = *template;
1963 vfd->minor = -1;
1964 vfd->v4l2_dev = &dev->v4l2_dev;
1965 vfd->release = video_device_release;
1966 vfd->debug = video_debug;
1968 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1969 dev->name, type_name);
1971 return vfd;
1974 int em28xx_register_analog_devices(struct em28xx *dev)
1976 u8 val;
1977 int ret;
1979 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
1980 dev->name,
1981 (EM28XX_VERSION_CODE >> 16) & 0xff,
1982 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
1984 /* set default norm */
1985 dev->norm = em28xx_video_template.current_norm;
1986 dev->width = norm_maxw(dev);
1987 dev->height = norm_maxh(dev);
1988 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1989 dev->hscale = 0;
1990 dev->vscale = 0;
1991 dev->ctl_input = 0;
1993 /* Analog specific initialization */
1994 dev->format = &format[0];
1995 video_mux(dev, dev->ctl_input);
1997 /* Audio defaults */
1998 dev->mute = 1;
1999 dev->volume = 0x1f;
2001 /* enable vbi capturing */
2003 /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
2004 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2005 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2006 (EM28XX_XCLK_AUDIO_UNMUTE | val));
2007 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
2009 em28xx_set_outfmt(dev);
2010 em28xx_colorlevels_set_default(dev);
2011 em28xx_compression_disable(dev);
2013 /* allocate and fill video video_device struct */
2014 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2015 if (!dev->vdev) {
2016 em28xx_errdev("cannot allocate video_device.\n");
2017 return -ENODEV;
2020 /* register v4l2 video video_device */
2021 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2022 video_nr[dev->devno]);
2023 if (ret) {
2024 em28xx_errdev("unable to register video device (error=%i).\n",
2025 ret);
2026 return ret;
2029 /* Allocate and fill vbi video_device struct */
2030 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2032 /* register v4l2 vbi video_device */
2033 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2034 vbi_nr[dev->devno]);
2035 if (ret < 0) {
2036 em28xx_errdev("unable to register vbi device\n");
2037 return ret;
2040 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2041 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2042 "radio");
2043 if (!dev->radio_dev) {
2044 em28xx_errdev("cannot allocate video_device.\n");
2045 return -ENODEV;
2047 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2048 radio_nr[dev->devno]);
2049 if (ret < 0) {
2050 em28xx_errdev("can't register radio device\n");
2051 return ret;
2053 em28xx_info("Registered radio device as /dev/radio%d\n",
2054 dev->radio_dev->num);
2057 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2058 dev->vdev->num, dev->vbi_dev->num);
2060 return 0;