2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
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>
37 #include <linux/mutex.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 {\
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...) \
66 printk(KERN_INFO "%s %s :"fmt, \
67 dev->name, __func__ , ##arg); \
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 = "16 bpp YUY2, 4:2:2, packed",
94 .fourcc = V4L2_PIX_FMT_YUYV,
96 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
98 .name = "16 bpp RGB 565, LE",
99 .fourcc = V4L2_PIX_FMT_RGB565,
101 .reg = EM28XX_OUTFMT_RGB_16_656,
103 .name = "8 bpp Bayer BGBG..GRGR",
104 .fourcc = V4L2_PIX_FMT_SBGGR8,
106 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
108 .name = "8 bpp Bayer GRGR..BGBG",
109 .fourcc = V4L2_PIX_FMT_SGRBG8,
111 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
113 .name = "8 bpp Bayer GBGB..RGRG",
114 .fourcc = V4L2_PIX_FMT_SGBRG8,
116 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
118 .name = "12 bpp YUV411",
119 .fourcc = V4L2_PIX_FMT_YUV411P,
121 .reg = EM28XX_OUTFMT_YUV411,
125 /* supported controls */
126 /* Common to all boards */
127 static struct v4l2_queryctrl em28xx_qctrl[] = {
129 .id = V4L2_CID_AUDIO_VOLUME,
130 .type = V4L2_CTRL_TYPE_INTEGER,
135 .default_value = 0x1f,
138 .id = V4L2_CID_AUDIO_MUTE,
139 .type = V4L2_CTRL_TYPE_BOOLEAN,
149 /* ------------------------------------------------------------------
150 DMA and thread functions
151 ------------------------------------------------------------------*/
154 * Announces that a buffer were filled and request the next
156 static inline void buffer_filled(struct em28xx *dev,
157 struct em28xx_dmaqueue *dma_q,
158 struct em28xx_buffer *buf)
160 /* Advice that buffer was filled */
161 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
162 buf->vb.state = VIDEOBUF_DONE;
163 buf->vb.field_count++;
164 do_gettimeofday(&buf->vb.ts);
166 dev->isoc_ctl.buf = NULL;
168 list_del(&buf->vb.queue);
169 wake_up(&buf->vb.done);
173 * Identify the buffer header type and properly handles
175 static void em28xx_copy_video(struct em28xx *dev,
176 struct em28xx_dmaqueue *dma_q,
177 struct em28xx_buffer *buf,
179 unsigned char *outp, unsigned long len)
181 void *fieldstart, *startwrite, *startread;
182 int linesdone, currlinedone, offset, lencopy, remain;
183 int bytesperline = dev->width << 1;
185 if (dma_q->pos + len > buf->vb.size)
186 len = buf->vb.size - dma_q->pos;
188 if (p[0] != 0x88 && p[0] != 0x22) {
189 em28xx_isocdbg("frame is not complete\n");
197 /* Interlaces frame */
201 fieldstart = outp + bytesperline;
203 linesdone = dma_q->pos / bytesperline;
204 currlinedone = dma_q->pos % bytesperline;
205 offset = linesdone * bytesperline * 2 + currlinedone;
206 startwrite = fieldstart + offset;
207 lencopy = bytesperline - currlinedone;
208 lencopy = lencopy > remain ? remain : lencopy;
210 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
211 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
212 ((char *)startwrite + lencopy) -
213 ((char *)outp + buf->vb.size));
214 remain = (char *)outp + buf->vb.size - (char *)startwrite;
219 memcpy(startwrite, startread, lencopy);
224 startwrite += lencopy + bytesperline;
225 startread += lencopy;
226 if (bytesperline > remain)
229 lencopy = bytesperline;
231 if ((char *)startwrite + lencopy > (char *)outp +
233 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
234 ((char *)startwrite + lencopy) -
235 ((char *)outp + buf->vb.size));
236 lencopy = remain = (char *)outp + buf->vb.size -
242 memcpy(startwrite, startread, lencopy);
250 static inline void print_err_status(struct em28xx *dev,
251 int packet, int status)
253 char *errmsg = "Unknown";
257 errmsg = "unlinked synchronuously";
260 errmsg = "unlinked asynchronuously";
263 errmsg = "Buffer error (overrun)";
266 errmsg = "Stalled (device not responding)";
269 errmsg = "Babble (bad cable?)";
272 errmsg = "Bit-stuff error (bad cable?)";
275 errmsg = "CRC/Timeout (could be anything)";
278 errmsg = "Device does not respond";
282 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
284 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
285 packet, status, errmsg);
290 * video-buf generic routine to get the next available buffer
292 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
293 struct em28xx_buffer **buf)
295 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
298 if (list_empty(&dma_q->active)) {
299 em28xx_isocdbg("No active queue to serve\n");
300 dev->isoc_ctl.buf = NULL;
305 /* Get the next buffer */
306 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
308 /* Cleans up buffer - Usefull for testing for frame/URB loss */
309 outp = videobuf_to_vmalloc(&(*buf)->vb);
310 memset(outp, 0, (*buf)->vb.size);
312 dev->isoc_ctl.buf = *buf;
318 * Controls the isoc copy of each urb packet
320 static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
322 struct em28xx_buffer *buf;
323 struct em28xx_dmaqueue *dma_q = urb->context;
324 unsigned char *outp = NULL;
325 int i, len = 0, rc = 1;
331 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
334 if (urb->status < 0) {
335 print_err_status(dev, -1, urb->status);
336 if (urb->status == -ENOENT)
340 buf = dev->isoc_ctl.buf;
342 outp = videobuf_to_vmalloc(&buf->vb);
344 for (i = 0; i < urb->number_of_packets; i++) {
345 int status = urb->iso_frame_desc[i].status;
348 print_err_status(dev, i, status);
349 if (urb->iso_frame_desc[i].status != -EPROTO)
353 len = urb->iso_frame_desc[i].actual_length - 4;
355 if (urb->iso_frame_desc[i].actual_length <= 0) {
356 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
359 if (urb->iso_frame_desc[i].actual_length >
361 em28xx_isocdbg("packet bigger than packet size");
365 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
367 /* FIXME: incomplete buffer checks where removed to make
368 logic simpler. Impacts of those changes should be evaluated
370 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
371 em28xx_isocdbg("VBI HEADER!!!\n");
372 /* FIXME: Should add vbi copy */
375 if (p[0] == 0x22 && p[1] == 0x5a) {
376 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
377 len, (p[2] & 1) ? "odd" : "even");
381 buffer_filled(dev, dma_q, buf);
382 get_next_buf(dma_q, &buf);
386 outp = videobuf_to_vmalloc(&buf->vb);
399 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
404 /* ------------------------------------------------------------------
406 ------------------------------------------------------------------*/
409 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
411 struct em28xx_fh *fh = vq->priv_data;
412 struct em28xx *dev = fh->dev;
413 struct v4l2_frequency f;
415 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
418 *count = EM28XX_DEF_BUF;
420 if (*count < EM28XX_MIN_BUF)
421 *count = EM28XX_MIN_BUF;
423 /* Ask tuner to go to analog or radio mode */
424 memset(&f, 0, sizeof(f));
425 f.frequency = dev->ctl_freq;
426 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
428 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
433 /* This is called *without* dev->slock held; please keep it that way */
434 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
436 struct em28xx_fh *fh = vq->priv_data;
437 struct em28xx *dev = fh->dev;
438 unsigned long flags = 0;
442 /* We used to wait for the buffer to finish here, but this didn't work
443 because, as we were keeping the state as VIDEOBUF_QUEUED,
444 videobuf_queue_cancel marked it as finished for us.
445 (Also, it could wedge forever if the hardware was misconfigured.)
447 This should be safe; by the time we get here, the buffer isn't
448 queued anymore. If we ever start marking the buffers as
449 VIDEOBUF_ACTIVE, it won't be, though.
451 spin_lock_irqsave(&dev->slock, flags);
452 if (dev->isoc_ctl.buf == buf)
453 dev->isoc_ctl.buf = NULL;
454 spin_unlock_irqrestore(&dev->slock, flags);
456 videobuf_vmalloc_free(&buf->vb);
457 buf->vb.state = VIDEOBUF_NEEDS_INIT;
461 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
462 enum v4l2_field field)
464 struct em28xx_fh *fh = vq->priv_data;
465 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
466 struct em28xx *dev = fh->dev;
467 int rc = 0, urb_init = 0;
469 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
471 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
474 buf->vb.width = dev->width;
475 buf->vb.height = dev->height;
476 buf->vb.field = field;
478 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
479 rc = videobuf_iolock(vq, &buf->vb, NULL);
484 if (!dev->isoc_ctl.num_bufs)
488 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
489 EM28XX_NUM_BUFS, dev->max_pkt_size,
495 buf->vb.state = VIDEOBUF_PREPARED;
499 free_buffer(vq, buf);
504 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
506 struct em28xx_buffer *buf = container_of(vb,
507 struct em28xx_buffer,
509 struct em28xx_fh *fh = vq->priv_data;
510 struct em28xx *dev = fh->dev;
511 struct em28xx_dmaqueue *vidq = &dev->vidq;
513 buf->vb.state = VIDEOBUF_QUEUED;
514 list_add_tail(&buf->vb.queue, &vidq->active);
518 static void buffer_release(struct videobuf_queue *vq,
519 struct videobuf_buffer *vb)
521 struct em28xx_buffer *buf = container_of(vb,
522 struct em28xx_buffer,
524 struct em28xx_fh *fh = vq->priv_data;
525 struct em28xx *dev = (struct em28xx *)fh->dev;
527 em28xx_isocdbg("em28xx: called buffer_release\n");
529 free_buffer(vq, buf);
532 static struct videobuf_queue_ops em28xx_video_qops = {
533 .buf_setup = buffer_setup,
534 .buf_prepare = buffer_prepare,
535 .buf_queue = buffer_queue,
536 .buf_release = buffer_release,
539 /********************* v4l2 interface **************************************/
541 static void video_mux(struct em28xx *dev, int index)
543 dev->ctl_input = index;
544 dev->ctl_ainput = INPUT(index)->amux;
545 dev->ctl_aoutput = INPUT(index)->aout;
547 if (!dev->ctl_aoutput)
548 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
550 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
551 INPUT(index)->vmux, 0, 0);
553 if (dev->board.has_msp34xx) {
554 if (dev->i2s_speed) {
555 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
556 s_i2s_clock_freq, dev->i2s_speed);
558 /* Note: this is msp3400 specific */
559 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
560 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
563 if (dev->board.adecoder != EM28XX_NOADECODER) {
564 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
565 dev->ctl_ainput, dev->ctl_aoutput, 0);
568 em28xx_audio_analog_set(dev);
571 /* Usage lock check functions */
572 static int res_get(struct em28xx_fh *fh)
574 struct em28xx *dev = fh->dev;
577 /* This instance already has stream_on */
589 static int res_check(struct em28xx_fh *fh)
591 return fh->stream_on;
594 static void res_free(struct em28xx_fh *fh)
596 struct em28xx *dev = fh->dev;
604 * return the current saturation, brightness or contrast, mute state
606 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
609 case V4L2_CID_AUDIO_MUTE:
610 ctrl->value = dev->mute;
612 case V4L2_CID_AUDIO_VOLUME:
613 ctrl->value = dev->volume;
622 * mute or set new saturation, brightness or contrast
624 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
627 case V4L2_CID_AUDIO_MUTE:
628 if (ctrl->value != dev->mute) {
629 dev->mute = ctrl->value;
630 return em28xx_audio_analog_set(dev);
633 case V4L2_CID_AUDIO_VOLUME:
634 dev->volume = ctrl->value;
635 return em28xx_audio_analog_set(dev);
641 static int check_dev(struct em28xx *dev)
643 if (dev->state & DEV_DISCONNECTED) {
644 em28xx_errdev("v4l2 ioctl: device not present\n");
648 if (dev->state & DEV_MISCONFIGURED) {
649 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
650 "close and open it again\n");
656 static void get_scale(struct em28xx *dev,
657 unsigned int width, unsigned int height,
658 unsigned int *hscale, unsigned int *vscale)
660 unsigned int maxw = norm_maxw(dev);
661 unsigned int maxh = norm_maxh(dev);
663 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
664 if (*hscale >= 0x4000)
667 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
668 if (*vscale >= 0x4000)
672 /* ------------------------------------------------------------------
673 IOCTL vidioc handling
674 ------------------------------------------------------------------*/
676 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
677 struct v4l2_format *f)
679 struct em28xx_fh *fh = priv;
680 struct em28xx *dev = fh->dev;
682 mutex_lock(&dev->lock);
684 f->fmt.pix.width = dev->width;
685 f->fmt.pix.height = dev->height;
686 f->fmt.pix.pixelformat = dev->format->fourcc;
687 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
688 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
689 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
691 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
692 f->fmt.pix.field = dev->interlaced ?
693 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
695 mutex_unlock(&dev->lock);
699 static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
703 for (i = 0; i < ARRAY_SIZE(format); i++)
704 if (format[i].fourcc == fourcc)
710 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
711 struct v4l2_format *f)
713 struct em28xx_fh *fh = priv;
714 struct em28xx *dev = fh->dev;
715 unsigned int width = f->fmt.pix.width;
716 unsigned int height = f->fmt.pix.height;
717 unsigned int maxw = norm_maxw(dev);
718 unsigned int maxh = norm_maxh(dev);
719 unsigned int hscale, vscale;
720 struct em28xx_fmt *fmt;
722 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
724 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
725 f->fmt.pix.pixelformat);
729 if (dev->board.is_em2800) {
730 /* the em2800 can only scale down to 50% */
731 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
732 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
733 /* According to empiatech support the MaxPacketSize is too small
734 * to support framesizes larger than 640x480 @ 30 fps or 640x576
735 * @ 25 fps. As this would cut of a part of the image we prefer
736 * 360x576 or 360x480 for now */
737 if (width == maxw && height == maxh)
740 /* width must even because of the YUYV format
741 height must be even because of interlacing */
742 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
745 get_scale(dev, width, height, &hscale, &vscale);
747 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
748 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
750 f->fmt.pix.width = width;
751 f->fmt.pix.height = height;
752 f->fmt.pix.pixelformat = fmt->fourcc;
753 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
754 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
755 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
756 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
761 static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
762 unsigned width, unsigned height)
764 struct em28xx_fmt *fmt;
766 fmt = format_by_fourcc(fourcc);
772 dev->height = height;
774 /* set new image size */
775 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
777 em28xx_set_alternate(dev);
778 em28xx_resolution_set(dev);
783 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
784 struct v4l2_format *f)
786 struct em28xx_fh *fh = priv;
787 struct em28xx *dev = fh->dev;
794 mutex_lock(&dev->lock);
796 vidioc_try_fmt_vid_cap(file, priv, f);
798 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
799 em28xx_errdev("%s queue busy\n", __func__);
804 if (dev->stream_on && !fh->stream_on) {
805 em28xx_errdev("%s device in use by another fh\n", __func__);
810 rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
811 f->fmt.pix.width, f->fmt.pix.height);
814 mutex_unlock(&dev->lock);
818 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
820 struct em28xx_fh *fh = priv;
821 struct em28xx *dev = fh->dev;
822 struct v4l2_format f;
829 mutex_lock(&dev->lock);
832 /* Adjusts width/height, if needed */
833 f.fmt.pix.width = dev->width;
834 f.fmt.pix.height = dev->height;
835 vidioc_try_fmt_vid_cap(file, priv, &f);
837 /* set new image size */
838 dev->width = f.fmt.pix.width;
839 dev->height = f.fmt.pix.height;
840 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
842 em28xx_resolution_set(dev);
843 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
845 mutex_unlock(&dev->lock);
849 static int vidioc_g_parm(struct file *file, void *priv,
850 struct v4l2_streamparm *p)
852 struct em28xx_fh *fh = priv;
853 struct em28xx *dev = fh->dev;
856 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
859 if (dev->board.is_webcam)
860 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
863 v4l2_video_std_frame_period(dev->norm,
864 &p->parm.capture.timeperframe);
869 static int vidioc_s_parm(struct file *file, void *priv,
870 struct v4l2_streamparm *p)
872 struct em28xx_fh *fh = priv;
873 struct em28xx *dev = fh->dev;
875 if (!dev->board.is_webcam)
878 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
881 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
884 static const char *iname[] = {
885 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
886 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
887 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
888 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
889 [EM28XX_VMUX_SVIDEO] = "S-Video",
890 [EM28XX_VMUX_TELEVISION] = "Television",
891 [EM28XX_VMUX_CABLE] = "Cable TV",
892 [EM28XX_VMUX_DVB] = "DVB",
893 [EM28XX_VMUX_DEBUG] = "for debug only",
896 static int vidioc_enum_input(struct file *file, void *priv,
897 struct v4l2_input *i)
899 struct em28xx_fh *fh = priv;
900 struct em28xx *dev = fh->dev;
904 if (n >= MAX_EM28XX_INPUT)
906 if (0 == INPUT(n)->type)
910 i->type = V4L2_INPUT_TYPE_CAMERA;
912 strcpy(i->name, iname[INPUT(n)->type]);
914 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
915 (EM28XX_VMUX_CABLE == INPUT(n)->type))
916 i->type = V4L2_INPUT_TYPE_TUNER;
918 i->std = dev->vdev->tvnorms;
923 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
925 struct em28xx_fh *fh = priv;
926 struct em28xx *dev = fh->dev;
933 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
935 struct em28xx_fh *fh = priv;
936 struct em28xx *dev = fh->dev;
943 if (i >= MAX_EM28XX_INPUT)
945 if (0 == INPUT(i)->type)
950 mutex_lock(&dev->lock);
951 video_mux(dev, dev->ctl_input);
952 mutex_unlock(&dev->lock);
956 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
958 struct em28xx_fh *fh = priv;
959 struct em28xx *dev = fh->dev;
962 case EM28XX_AMUX_VIDEO:
963 strcpy(a->name, "Television");
965 case EM28XX_AMUX_LINE_IN:
966 strcpy(a->name, "Line In");
968 case EM28XX_AMUX_VIDEO2:
969 strcpy(a->name, "Television alt");
971 case EM28XX_AMUX_PHONE:
972 strcpy(a->name, "Phone");
974 case EM28XX_AMUX_MIC:
975 strcpy(a->name, "Mic");
978 strcpy(a->name, "CD");
980 case EM28XX_AMUX_AUX:
981 strcpy(a->name, "Aux");
983 case EM28XX_AMUX_PCM_OUT:
984 strcpy(a->name, "PCM");
990 a->index = dev->ctl_ainput;
991 a->capability = V4L2_AUDCAP_STEREO;
996 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
998 struct em28xx_fh *fh = priv;
999 struct em28xx *dev = fh->dev;
1002 if (a->index >= MAX_EM28XX_INPUT)
1004 if (0 == INPUT(a->index)->type)
1007 mutex_lock(&dev->lock);
1009 dev->ctl_ainput = INPUT(a->index)->amux;
1010 dev->ctl_aoutput = INPUT(a->index)->aout;
1012 if (!dev->ctl_aoutput)
1013 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
1015 mutex_unlock(&dev->lock);
1019 static int vidioc_queryctrl(struct file *file, void *priv,
1020 struct v4l2_queryctrl *qc)
1022 struct em28xx_fh *fh = priv;
1023 struct em28xx *dev = fh->dev;
1028 rc = check_dev(dev);
1032 memset(qc, 0, sizeof(*qc));
1036 if (!dev->board.has_msp34xx) {
1037 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1038 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1039 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1045 mutex_lock(&dev->lock);
1046 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
1047 mutex_unlock(&dev->lock);
1055 static int vidioc_g_ctrl(struct file *file, void *priv,
1056 struct v4l2_control *ctrl)
1058 struct em28xx_fh *fh = priv;
1059 struct em28xx *dev = fh->dev;
1062 rc = check_dev(dev);
1067 mutex_lock(&dev->lock);
1069 if (dev->board.has_msp34xx)
1070 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1072 rc = em28xx_get_ctrl(dev, ctrl);
1074 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1079 mutex_unlock(&dev->lock);
1083 static int vidioc_s_ctrl(struct file *file, void *priv,
1084 struct v4l2_control *ctrl)
1086 struct em28xx_fh *fh = priv;
1087 struct em28xx *dev = fh->dev;
1091 rc = check_dev(dev);
1095 mutex_lock(&dev->lock);
1097 if (dev->board.has_msp34xx)
1098 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
1101 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1102 if (ctrl->id == em28xx_qctrl[i].id) {
1103 if (ctrl->value < em28xx_qctrl[i].minimum ||
1104 ctrl->value > em28xx_qctrl[i].maximum) {
1109 rc = em28xx_set_ctrl(dev, ctrl);
1115 /* Control not found - try to send it to the attached devices */
1117 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
1121 mutex_unlock(&dev->lock);
1125 static int vidioc_g_tuner(struct file *file, void *priv,
1126 struct v4l2_tuner *t)
1128 struct em28xx_fh *fh = priv;
1129 struct em28xx *dev = fh->dev;
1132 rc = check_dev(dev);
1139 strcpy(t->name, "Tuner");
1141 mutex_lock(&dev->lock);
1142 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1143 mutex_unlock(&dev->lock);
1148 static int vidioc_s_tuner(struct file *file, void *priv,
1149 struct v4l2_tuner *t)
1151 struct em28xx_fh *fh = priv;
1152 struct em28xx *dev = fh->dev;
1155 rc = check_dev(dev);
1162 mutex_lock(&dev->lock);
1163 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1164 mutex_unlock(&dev->lock);
1169 static int vidioc_g_frequency(struct file *file, void *priv,
1170 struct v4l2_frequency *f)
1172 struct em28xx_fh *fh = priv;
1173 struct em28xx *dev = fh->dev;
1175 mutex_lock(&dev->lock);
1176 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1177 f->frequency = dev->ctl_freq;
1178 mutex_unlock(&dev->lock);
1183 static int vidioc_s_frequency(struct file *file, void *priv,
1184 struct v4l2_frequency *f)
1186 struct em28xx_fh *fh = priv;
1187 struct em28xx *dev = fh->dev;
1190 rc = check_dev(dev);
1197 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1199 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1202 mutex_lock(&dev->lock);
1204 dev->ctl_freq = f->frequency;
1205 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1207 mutex_unlock(&dev->lock);
1212 #ifdef CONFIG_VIDEO_ADV_DEBUG
1213 static int em28xx_reg_len(int reg)
1216 case EM28XX_R40_AC97LSB:
1217 case EM28XX_R30_HSCALELOW:
1218 case EM28XX_R32_VSCALELOW:
1225 static int vidioc_g_chip_ident(struct file *file, void *priv,
1226 struct v4l2_dbg_chip_ident *chip)
1228 struct em28xx_fh *fh = priv;
1229 struct em28xx *dev = fh->dev;
1231 chip->ident = V4L2_IDENT_NONE;
1234 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
1240 static int vidioc_g_register(struct file *file, void *priv,
1241 struct v4l2_dbg_register *reg)
1243 struct em28xx_fh *fh = priv;
1244 struct em28xx *dev = fh->dev;
1247 switch (reg->match.type) {
1248 case V4L2_CHIP_MATCH_AC97:
1249 mutex_lock(&dev->lock);
1250 ret = em28xx_read_ac97(dev, reg->reg);
1251 mutex_unlock(&dev->lock);
1258 case V4L2_CHIP_MATCH_I2C_DRIVER:
1259 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1261 case V4L2_CHIP_MATCH_I2C_ADDR:
1262 /* Not supported yet */
1265 if (!v4l2_chip_match_host(®->match))
1270 reg->size = em28xx_reg_len(reg->reg);
1271 if (reg->size == 1) {
1272 mutex_lock(&dev->lock);
1273 ret = em28xx_read_reg(dev, reg->reg);
1274 mutex_unlock(&dev->lock);
1282 mutex_lock(&dev->lock);
1283 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1284 reg->reg, (char *)&val, 2);
1285 mutex_unlock(&dev->lock);
1289 reg->val = le16_to_cpu(val);
1295 static int vidioc_s_register(struct file *file, void *priv,
1296 struct v4l2_dbg_register *reg)
1298 struct em28xx_fh *fh = priv;
1299 struct em28xx *dev = fh->dev;
1303 switch (reg->match.type) {
1304 case V4L2_CHIP_MATCH_AC97:
1305 mutex_lock(&dev->lock);
1306 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1307 mutex_unlock(&dev->lock);
1310 case V4L2_CHIP_MATCH_I2C_DRIVER:
1311 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1313 case V4L2_CHIP_MATCH_I2C_ADDR:
1314 /* Not supported yet */
1317 if (!v4l2_chip_match_host(®->match))
1322 buf = cpu_to_le16(reg->val);
1324 mutex_lock(&dev->lock);
1325 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1326 em28xx_reg_len(reg->reg));
1327 mutex_unlock(&dev->lock);
1334 static int vidioc_cropcap(struct file *file, void *priv,
1335 struct v4l2_cropcap *cc)
1337 struct em28xx_fh *fh = priv;
1338 struct em28xx *dev = fh->dev;
1340 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1343 cc->bounds.left = 0;
1345 cc->bounds.width = dev->width;
1346 cc->bounds.height = dev->height;
1347 cc->defrect = cc->bounds;
1348 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1349 cc->pixelaspect.denominator = 59;
1354 static int vidioc_streamon(struct file *file, void *priv,
1355 enum v4l2_buf_type type)
1357 struct em28xx_fh *fh = priv;
1358 struct em28xx *dev = fh->dev;
1361 rc = check_dev(dev);
1366 mutex_lock(&dev->lock);
1369 if (likely(rc >= 0))
1370 rc = videobuf_streamon(&fh->vb_vidq);
1372 mutex_unlock(&dev->lock);
1377 static int vidioc_streamoff(struct file *file, void *priv,
1378 enum v4l2_buf_type type)
1380 struct em28xx_fh *fh = priv;
1381 struct em28xx *dev = fh->dev;
1384 rc = check_dev(dev);
1388 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1390 if (type != fh->type)
1393 mutex_lock(&dev->lock);
1395 videobuf_streamoff(&fh->vb_vidq);
1398 mutex_unlock(&dev->lock);
1403 static int vidioc_querycap(struct file *file, void *priv,
1404 struct v4l2_capability *cap)
1406 struct em28xx_fh *fh = priv;
1407 struct em28xx *dev = fh->dev;
1409 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1410 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1411 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1413 cap->version = EM28XX_VERSION_CODE;
1416 V4L2_CAP_SLICED_VBI_CAPTURE |
1417 V4L2_CAP_VIDEO_CAPTURE |
1419 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1421 if (dev->tuner_type != TUNER_ABSENT)
1422 cap->capabilities |= V4L2_CAP_TUNER;
1427 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1428 struct v4l2_fmtdesc *f)
1430 if (unlikely(f->index >= ARRAY_SIZE(format)))
1433 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1434 f->pixelformat = format[f->index].fourcc;
1439 /* Sliced VBI ioctls */
1440 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1441 struct v4l2_format *f)
1443 struct em28xx_fh *fh = priv;
1444 struct em28xx *dev = fh->dev;
1447 rc = check_dev(dev);
1451 mutex_lock(&dev->lock);
1453 f->fmt.sliced.service_set = 0;
1454 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
1456 if (f->fmt.sliced.service_set == 0)
1459 mutex_unlock(&dev->lock);
1464 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1465 struct v4l2_format *f)
1467 struct em28xx_fh *fh = priv;
1468 struct em28xx *dev = fh->dev;
1471 rc = check_dev(dev);
1475 mutex_lock(&dev->lock);
1476 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
1477 mutex_unlock(&dev->lock);
1479 if (f->fmt.sliced.service_set == 0)
1486 static int vidioc_reqbufs(struct file *file, void *priv,
1487 struct v4l2_requestbuffers *rb)
1489 struct em28xx_fh *fh = priv;
1490 struct em28xx *dev = fh->dev;
1493 rc = check_dev(dev);
1497 return videobuf_reqbufs(&fh->vb_vidq, rb);
1500 static int vidioc_querybuf(struct file *file, void *priv,
1501 struct v4l2_buffer *b)
1503 struct em28xx_fh *fh = priv;
1504 struct em28xx *dev = fh->dev;
1507 rc = check_dev(dev);
1511 return videobuf_querybuf(&fh->vb_vidq, b);
1514 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1516 struct em28xx_fh *fh = priv;
1517 struct em28xx *dev = fh->dev;
1520 rc = check_dev(dev);
1524 return videobuf_qbuf(&fh->vb_vidq, b);
1527 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1529 struct em28xx_fh *fh = priv;
1530 struct em28xx *dev = fh->dev;
1533 rc = check_dev(dev);
1537 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1540 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1541 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1543 struct em28xx_fh *fh = priv;
1545 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1550 /* ----------------------------------------------------------- */
1551 /* RADIO ESPECIFIC IOCTLS */
1552 /* ----------------------------------------------------------- */
1554 static int radio_querycap(struct file *file, void *priv,
1555 struct v4l2_capability *cap)
1557 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1559 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1560 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1561 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1563 cap->version = EM28XX_VERSION_CODE;
1564 cap->capabilities = V4L2_CAP_TUNER;
1568 static int radio_g_tuner(struct file *file, void *priv,
1569 struct v4l2_tuner *t)
1571 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1573 if (unlikely(t->index > 0))
1576 strcpy(t->name, "Radio");
1577 t->type = V4L2_TUNER_RADIO;
1579 mutex_lock(&dev->lock);
1580 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1581 mutex_unlock(&dev->lock);
1586 static int radio_enum_input(struct file *file, void *priv,
1587 struct v4l2_input *i)
1591 strcpy(i->name, "Radio");
1592 i->type = V4L2_INPUT_TYPE_TUNER;
1597 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1599 if (unlikely(a->index))
1602 strcpy(a->name, "Radio");
1606 static int radio_s_tuner(struct file *file, void *priv,
1607 struct v4l2_tuner *t)
1609 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1614 mutex_lock(&dev->lock);
1615 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1616 mutex_unlock(&dev->lock);
1621 static int radio_s_audio(struct file *file, void *fh,
1622 struct v4l2_audio *a)
1627 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1632 static int radio_queryctrl(struct file *file, void *priv,
1633 struct v4l2_queryctrl *qc)
1637 if (qc->id < V4L2_CID_BASE ||
1638 qc->id >= V4L2_CID_LASTP1)
1641 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1642 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1643 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1652 * em28xx_v4l2_open()
1653 * inits the device and starts isoc transfer
1655 static int em28xx_v4l2_open(struct file *filp)
1657 int minor = video_devdata(filp)->minor;
1658 int errCode = 0, radio;
1660 enum v4l2_buf_type fh_type;
1661 struct em28xx_fh *fh;
1663 dev = em28xx_get_device(minor, &fh_type, &radio);
1668 mutex_lock(&dev->lock);
1670 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1671 minor, v4l2_type_names[fh_type], dev->users);
1674 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1676 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1677 mutex_unlock(&dev->lock);
1683 filp->private_data = fh;
1685 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1686 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
1687 em28xx_set_alternate(dev);
1688 em28xx_resolution_set(dev);
1690 /* Needed, since GPIO might have disabled power of
1693 em28xx_wake_i2c(dev);
1697 em28xx_videodbg("video_open: setting radio device\n");
1698 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1703 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1704 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1705 sizeof(struct em28xx_buffer), fh);
1707 mutex_unlock(&dev->lock);
1713 * em28xx_realease_resources()
1714 * unregisters the v4l2,i2c and usb devices
1715 * called when the device gets disconected or at module unload
1717 void em28xx_release_analog_resources(struct em28xx *dev)
1720 /*FIXME: I2C IR should be disconnected */
1722 if (dev->radio_dev) {
1723 if (-1 != dev->radio_dev->minor)
1724 video_unregister_device(dev->radio_dev);
1726 video_device_release(dev->radio_dev);
1727 dev->radio_dev = NULL;
1730 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1732 if (-1 != dev->vbi_dev->minor)
1733 video_unregister_device(dev->vbi_dev);
1735 video_device_release(dev->vbi_dev);
1736 dev->vbi_dev = NULL;
1739 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1741 if (-1 != dev->vdev->minor)
1742 video_unregister_device(dev->vdev);
1744 video_device_release(dev->vdev);
1750 * em28xx_v4l2_close()
1751 * stops streaming and deallocates all resources allocated by the v4l2
1754 static int em28xx_v4l2_close(struct file *filp)
1756 struct em28xx_fh *fh = filp->private_data;
1757 struct em28xx *dev = fh->dev;
1760 em28xx_videodbg("users=%d\n", dev->users);
1763 mutex_lock(&dev->lock);
1767 if (dev->users == 1) {
1768 videobuf_stop(&fh->vb_vidq);
1769 videobuf_mmap_free(&fh->vb_vidq);
1771 /* the device is already disconnect,
1772 free the remaining resources */
1773 if (dev->state & DEV_DISCONNECTED) {
1774 em28xx_release_resources(dev);
1775 mutex_unlock(&dev->lock);
1780 /* Save some power by putting tuner to sleep */
1781 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_standby);
1783 /* do this before setting alternate! */
1784 em28xx_uninit_isoc(dev);
1785 em28xx_set_mode(dev, EM28XX_SUSPEND);
1787 /* set alternate 0 */
1789 em28xx_videodbg("setting alternate 0\n");
1790 errCode = usb_set_interface(dev->udev, 0, 0);
1792 em28xx_errdev("cannot change alternate number to "
1793 "0 (error=%i)\n", errCode);
1798 wake_up_interruptible_nr(&dev->open, 1);
1799 mutex_unlock(&dev->lock);
1804 * em28xx_v4l2_read()
1805 * will allocate buffers when called for the first time
1808 em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1811 struct em28xx_fh *fh = filp->private_data;
1812 struct em28xx *dev = fh->dev;
1815 rc = check_dev(dev);
1819 /* FIXME: read() is not prepared to allow changing the video
1820 resolution while streaming. Seems a bug at em28xx_set_fmt
1823 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1824 mutex_lock(&dev->lock);
1826 mutex_unlock(&dev->lock);
1828 if (unlikely(rc < 0))
1831 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1832 filp->f_flags & O_NONBLOCK);
1838 * em28xx_v4l2_poll()
1839 * will allocate buffers when called for the first time
1841 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
1843 struct em28xx_fh *fh = filp->private_data;
1844 struct em28xx *dev = fh->dev;
1847 rc = check_dev(dev);
1851 mutex_lock(&dev->lock);
1853 mutex_unlock(&dev->lock);
1855 if (unlikely(rc < 0))
1858 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1861 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1865 * em28xx_v4l2_mmap()
1867 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1869 struct em28xx_fh *fh = filp->private_data;
1870 struct em28xx *dev = fh->dev;
1873 rc = check_dev(dev);
1877 mutex_lock(&dev->lock);
1879 mutex_unlock(&dev->lock);
1881 if (unlikely(rc < 0))
1884 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1886 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1887 (unsigned long)vma->vm_start,
1888 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1894 static const struct v4l2_file_operations em28xx_v4l_fops = {
1895 .owner = THIS_MODULE,
1896 .open = em28xx_v4l2_open,
1897 .release = em28xx_v4l2_close,
1898 .read = em28xx_v4l2_read,
1899 .poll = em28xx_v4l2_poll,
1900 .mmap = em28xx_v4l2_mmap,
1901 .ioctl = video_ioctl2,
1904 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1905 .vidioc_querycap = vidioc_querycap,
1906 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1907 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1908 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1909 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1910 .vidioc_g_audio = vidioc_g_audio,
1911 .vidioc_s_audio = vidioc_s_audio,
1912 .vidioc_cropcap = vidioc_cropcap,
1914 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1915 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1916 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1918 .vidioc_reqbufs = vidioc_reqbufs,
1919 .vidioc_querybuf = vidioc_querybuf,
1920 .vidioc_qbuf = vidioc_qbuf,
1921 .vidioc_dqbuf = vidioc_dqbuf,
1922 .vidioc_s_std = vidioc_s_std,
1923 .vidioc_g_parm = vidioc_g_parm,
1924 .vidioc_s_parm = vidioc_s_parm,
1925 .vidioc_enum_input = vidioc_enum_input,
1926 .vidioc_g_input = vidioc_g_input,
1927 .vidioc_s_input = vidioc_s_input,
1928 .vidioc_queryctrl = vidioc_queryctrl,
1929 .vidioc_g_ctrl = vidioc_g_ctrl,
1930 .vidioc_s_ctrl = vidioc_s_ctrl,
1931 .vidioc_streamon = vidioc_streamon,
1932 .vidioc_streamoff = vidioc_streamoff,
1933 .vidioc_g_tuner = vidioc_g_tuner,
1934 .vidioc_s_tuner = vidioc_s_tuner,
1935 .vidioc_g_frequency = vidioc_g_frequency,
1936 .vidioc_s_frequency = vidioc_s_frequency,
1937 #ifdef CONFIG_VIDEO_ADV_DEBUG
1938 .vidioc_g_register = vidioc_g_register,
1939 .vidioc_s_register = vidioc_s_register,
1940 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1942 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1943 .vidiocgmbuf = vidiocgmbuf,
1947 static const struct video_device em28xx_video_template = {
1948 .fops = &em28xx_v4l_fops,
1949 .release = video_device_release,
1950 .ioctl_ops = &video_ioctl_ops,
1954 .tvnorms = V4L2_STD_ALL,
1955 .current_norm = V4L2_STD_PAL,
1958 static const struct v4l2_file_operations radio_fops = {
1959 .owner = THIS_MODULE,
1960 .open = em28xx_v4l2_open,
1961 .release = em28xx_v4l2_close,
1962 .ioctl = video_ioctl2,
1965 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1966 .vidioc_querycap = radio_querycap,
1967 .vidioc_g_tuner = radio_g_tuner,
1968 .vidioc_enum_input = radio_enum_input,
1969 .vidioc_g_audio = radio_g_audio,
1970 .vidioc_s_tuner = radio_s_tuner,
1971 .vidioc_s_audio = radio_s_audio,
1972 .vidioc_s_input = radio_s_input,
1973 .vidioc_queryctrl = radio_queryctrl,
1974 .vidioc_g_ctrl = vidioc_g_ctrl,
1975 .vidioc_s_ctrl = vidioc_s_ctrl,
1976 .vidioc_g_frequency = vidioc_g_frequency,
1977 .vidioc_s_frequency = vidioc_s_frequency,
1978 #ifdef CONFIG_VIDEO_ADV_DEBUG
1979 .vidioc_g_register = vidioc_g_register,
1980 .vidioc_s_register = vidioc_s_register,
1984 static struct video_device em28xx_radio_template = {
1985 .name = "em28xx-radio",
1986 .fops = &radio_fops,
1987 .ioctl_ops = &radio_ioctl_ops,
1991 /******************************** usb interface ******************************/
1995 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1996 const struct video_device *template,
1997 const char *type_name)
1999 struct video_device *vfd;
2001 vfd = video_device_alloc();
2007 vfd->v4l2_dev = &dev->v4l2_dev;
2008 vfd->release = video_device_release;
2009 vfd->debug = video_debug;
2011 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2012 dev->name, type_name);
2017 int em28xx_register_analog_devices(struct em28xx *dev)
2022 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
2024 (EM28XX_VERSION_CODE >> 16) & 0xff,
2025 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2027 /* set default norm */
2028 dev->norm = em28xx_video_template.current_norm;
2029 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
2032 /* Analog specific initialization */
2033 dev->format = &format[0];
2034 em28xx_set_video_format(dev, format[0].fourcc,
2035 norm_maxw(dev), norm_maxh(dev));
2037 video_mux(dev, dev->ctl_input);
2039 /* Audio defaults */
2043 /* enable vbi capturing */
2045 /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
2046 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2047 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2048 (EM28XX_XCLK_AUDIO_UNMUTE | val));
2049 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
2051 em28xx_set_outfmt(dev);
2052 em28xx_colorlevels_set_default(dev);
2053 em28xx_compression_disable(dev);
2055 /* allocate and fill video video_device struct */
2056 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2058 em28xx_errdev("cannot allocate video_device.\n");
2062 /* register v4l2 video video_device */
2063 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2064 video_nr[dev->devno]);
2066 em28xx_errdev("unable to register video device (error=%i).\n",
2071 /* Allocate and fill vbi video_device struct */
2072 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2074 /* register v4l2 vbi video_device */
2075 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2076 vbi_nr[dev->devno]);
2078 em28xx_errdev("unable to register vbi device\n");
2082 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2083 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2085 if (!dev->radio_dev) {
2086 em28xx_errdev("cannot allocate video_device.\n");
2089 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2090 radio_nr[dev->devno]);
2092 em28xx_errdev("can't register radio device\n");
2095 em28xx_info("Registered radio device as /dev/radio%d\n",
2096 dev->radio_dev->num);
2099 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2100 dev->vdev->num, dev->vbi_dev->num);