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, 1)
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 = "16bpp YUY2, 4:2:2, packed",
94 .fourcc = V4L2_PIX_FMT_YUYV,
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,
110 .default_value = 0x1f,
113 .id = V4L2_CID_AUDIO_MUTE,
114 .type = V4L2_CTRL_TYPE_BOOLEAN,
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,
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");
172 /* Interlaces frame */
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;
194 memcpy(startwrite, startread, lencopy);
199 startwrite += lencopy + bytesperline;
200 startread += lencopy;
201 if (bytesperline > remain)
204 lencopy = bytesperline;
206 if ((char *)startwrite + lencopy > (char *)outp +
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 -
217 memcpy(startwrite, startread, lencopy);
225 static inline void print_err_status(struct em28xx *dev,
226 int packet, int status)
228 char *errmsg = "Unknown";
232 errmsg = "unlinked synchronuously";
235 errmsg = "unlinked asynchronuously";
238 errmsg = "Buffer error (overrun)";
241 errmsg = "Stalled (device not responding)";
244 errmsg = "Babble (bad cable?)";
247 errmsg = "Bit-stuff error (bad cable?)";
250 errmsg = "CRC/Timeout (could be anything)";
253 errmsg = "Device does not respond";
257 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
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);
273 if (list_empty(&dma_q->active)) {
274 em28xx_isocdbg("No active queue to serve\n");
275 dev->isoc_ctl.buf = NULL;
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;
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;
306 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
309 if (urb->status < 0) {
310 print_err_status(dev, -1, urb->status);
311 if (urb->status == -ENOENT)
315 buf = dev->isoc_ctl.buf;
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;
323 print_err_status(dev, i, status);
324 if (urb->iso_frame_desc[i].status != -EPROTO)
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 */
334 if (urb->iso_frame_desc[i].actual_length >
336 em28xx_isocdbg("packet bigger than packet size");
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 */
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");
356 buffer_filled(dev, dma_q, buf);
357 get_next_buf(dma_q, &buf);
361 outp = videobuf_to_vmalloc(&buf->vb);
374 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
379 /* ------------------------------------------------------------------
381 ------------------------------------------------------------------*/
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;
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 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
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;
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;
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)
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);
459 if (!dev->isoc_ctl.num_bufs)
463 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
464 EM28XX_NUM_BUFS, dev->max_pkt_size,
470 buf->vb.state = VIDEOBUF_PREPARED;
474 free_buffer(vq, buf);
479 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
481 struct em28xx_buffer *buf = container_of(vb,
482 struct em28xx_buffer,
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,
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;
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 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
531 if (dev->board.has_msp34xx) {
532 if (dev->i2s_speed) {
533 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
536 route.input = dev->ctl_ainput;
537 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
538 /* Note: this is msp3400 specific */
539 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
543 em28xx_audio_analog_set(dev);
546 /* Usage lock check functions */
547 static int res_get(struct em28xx_fh *fh)
549 struct em28xx *dev = fh->dev;
552 /* This instance already has stream_on */
564 static int res_check(struct em28xx_fh *fh)
566 return fh->stream_on;
569 static void res_free(struct em28xx_fh *fh)
571 struct em28xx *dev = fh->dev;
579 * return the current saturation, brightness or contrast, mute state
581 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
584 case V4L2_CID_AUDIO_MUTE:
585 ctrl->value = dev->mute;
587 case V4L2_CID_AUDIO_VOLUME:
588 ctrl->value = dev->volume;
597 * mute or set new saturation, brightness or contrast
599 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
602 case V4L2_CID_AUDIO_MUTE:
603 if (ctrl->value != dev->mute) {
604 dev->mute = ctrl->value;
605 return em28xx_audio_analog_set(dev);
608 case V4L2_CID_AUDIO_VOLUME:
609 dev->volume = ctrl->value;
610 return em28xx_audio_analog_set(dev);
616 static int check_dev(struct em28xx *dev)
618 if (dev->state & DEV_DISCONNECTED) {
619 em28xx_errdev("v4l2 ioctl: device not present\n");
623 if (dev->state & DEV_MISCONFIGURED) {
624 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
625 "close and open it again\n");
631 static void get_scale(struct em28xx *dev,
632 unsigned int width, unsigned int height,
633 unsigned int *hscale, unsigned int *vscale)
635 unsigned int maxw = norm_maxw(dev);
636 unsigned int maxh = norm_maxh(dev);
638 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
639 if (*hscale >= 0x4000)
642 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
643 if (*vscale >= 0x4000)
647 /* ------------------------------------------------------------------
648 IOCTL vidioc handling
649 ------------------------------------------------------------------*/
651 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
652 struct v4l2_format *f)
654 struct em28xx_fh *fh = priv;
655 struct em28xx *dev = fh->dev;
657 mutex_lock(&dev->lock);
659 f->fmt.pix.width = dev->width;
660 f->fmt.pix.height = dev->height;
661 f->fmt.pix.pixelformat = dev->format->fourcc;
662 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
663 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
664 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
666 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
667 f->fmt.pix.field = dev->interlaced ?
668 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
670 mutex_unlock(&dev->lock);
674 static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
678 for (i = 0; i < ARRAY_SIZE(format); i++)
679 if (format[i].fourcc == fourcc)
685 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
686 struct v4l2_format *f)
688 struct em28xx_fh *fh = priv;
689 struct em28xx *dev = fh->dev;
690 int width = f->fmt.pix.width;
691 int height = f->fmt.pix.height;
692 unsigned int maxw = norm_maxw(dev);
693 unsigned int maxh = norm_maxh(dev);
694 unsigned int hscale, vscale;
695 struct em28xx_fmt *fmt;
697 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
699 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
700 f->fmt.pix.pixelformat);
704 /* width must even because of the YUYV format
705 height must be even because of interlacing */
709 if (unlikely(height < 32))
711 if (unlikely(height > maxh))
713 if (unlikely(width < 48))
715 if (unlikely(width > maxw))
718 if (dev->board.is_em2800) {
719 /* the em2800 can only scale down to 50% */
720 if (height % (maxh / 2))
722 if (width % (maxw / 2))
724 /* according to empiatech support */
725 /* the MaxPacketSize is to small to support */
726 /* framesizes larger than 640x480 @ 30 fps */
727 /* or 640x576 @ 25 fps. As this would cut */
728 /* of a part of the image we prefer */
729 /* 360x576 or 360x480 for now */
730 if (width == maxw && height == maxh)
734 get_scale(dev, width, height, &hscale, &vscale);
736 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
737 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
739 f->fmt.pix.width = width;
740 f->fmt.pix.height = height;
741 f->fmt.pix.pixelformat = fmt->fourcc;
742 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
743 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
744 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
745 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
750 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
751 struct v4l2_format *f)
753 struct em28xx_fh *fh = priv;
754 struct em28xx *dev = fh->dev;
756 struct em28xx_fmt *fmt;
762 mutex_lock(&dev->lock);
764 vidioc_try_fmt_vid_cap(file, priv, f);
766 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
772 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
773 em28xx_errdev("%s queue busy\n", __func__);
778 if (dev->stream_on && !fh->stream_on) {
779 em28xx_errdev("%s device in use by another fh\n", __func__);
784 /* set new image size */
785 dev->width = f->fmt.pix.width;
786 dev->height = f->fmt.pix.height;
788 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
790 em28xx_set_alternate(dev);
791 em28xx_resolution_set(dev);
796 mutex_unlock(&dev->lock);
800 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
802 struct em28xx_fh *fh = priv;
803 struct em28xx *dev = fh->dev;
804 struct v4l2_format f;
811 mutex_lock(&dev->lock);
814 /* Adjusts width/height, if needed */
815 f.fmt.pix.width = dev->width;
816 f.fmt.pix.height = dev->height;
817 vidioc_try_fmt_vid_cap(file, priv, &f);
819 /* set new image size */
820 dev->width = f.fmt.pix.width;
821 dev->height = f.fmt.pix.height;
822 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
824 em28xx_resolution_set(dev);
825 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
827 mutex_unlock(&dev->lock);
831 static const char *iname[] = {
832 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
833 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
834 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
835 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
836 [EM28XX_VMUX_SVIDEO] = "S-Video",
837 [EM28XX_VMUX_TELEVISION] = "Television",
838 [EM28XX_VMUX_CABLE] = "Cable TV",
839 [EM28XX_VMUX_DVB] = "DVB",
840 [EM28XX_VMUX_DEBUG] = "for debug only",
843 static int vidioc_enum_input(struct file *file, void *priv,
844 struct v4l2_input *i)
846 struct em28xx_fh *fh = priv;
847 struct em28xx *dev = fh->dev;
851 if (n >= MAX_EM28XX_INPUT)
853 if (0 == INPUT(n)->type)
857 i->type = V4L2_INPUT_TYPE_CAMERA;
859 strcpy(i->name, iname[INPUT(n)->type]);
861 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
862 (EM28XX_VMUX_CABLE == INPUT(n)->type))
863 i->type = V4L2_INPUT_TYPE_TUNER;
865 i->std = dev->vdev->tvnorms;
870 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
872 struct em28xx_fh *fh = priv;
873 struct em28xx *dev = fh->dev;
880 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
882 struct em28xx_fh *fh = priv;
883 struct em28xx *dev = fh->dev;
890 if (i >= MAX_EM28XX_INPUT)
892 if (0 == INPUT(i)->type)
897 mutex_lock(&dev->lock);
898 video_mux(dev, dev->ctl_input);
899 mutex_unlock(&dev->lock);
903 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
905 struct em28xx_fh *fh = priv;
906 struct em28xx *dev = fh->dev;
909 case EM28XX_AMUX_VIDEO:
910 strcpy(a->name, "Television");
912 case EM28XX_AMUX_LINE_IN:
913 strcpy(a->name, "Line In");
915 case EM28XX_AMUX_VIDEO2:
916 strcpy(a->name, "Television alt");
918 case EM28XX_AMUX_PHONE:
919 strcpy(a->name, "Phone");
921 case EM28XX_AMUX_MIC:
922 strcpy(a->name, "Mic");
925 strcpy(a->name, "CD");
927 case EM28XX_AMUX_AUX:
928 strcpy(a->name, "Aux");
930 case EM28XX_AMUX_PCM_OUT:
931 strcpy(a->name, "PCM");
937 a->index = dev->ctl_ainput;
938 a->capability = V4L2_AUDCAP_STEREO;
943 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
945 struct em28xx_fh *fh = priv;
946 struct em28xx *dev = fh->dev;
949 if (a->index >= MAX_EM28XX_INPUT)
951 if (0 == INPUT(a->index)->type)
954 mutex_lock(&dev->lock);
956 dev->ctl_ainput = INPUT(a->index)->amux;
957 dev->ctl_aoutput = INPUT(a->index)->aout;
959 if (!dev->ctl_aoutput)
960 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
962 mutex_unlock(&dev->lock);
966 static int vidioc_queryctrl(struct file *file, void *priv,
967 struct v4l2_queryctrl *qc)
969 struct em28xx_fh *fh = priv;
970 struct em28xx *dev = fh->dev;
979 memset(qc, 0, sizeof(*qc));
983 if (!dev->board.has_msp34xx) {
984 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
985 if (qc->id && qc->id == em28xx_qctrl[i].id) {
986 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
991 mutex_lock(&dev->lock);
992 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
993 mutex_unlock(&dev->lock);
1001 static int vidioc_g_ctrl(struct file *file, void *priv,
1002 struct v4l2_control *ctrl)
1004 struct em28xx_fh *fh = priv;
1005 struct em28xx *dev = fh->dev;
1008 rc = check_dev(dev);
1013 mutex_lock(&dev->lock);
1015 if (dev->board.has_msp34xx)
1016 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1018 rc = em28xx_get_ctrl(dev, ctrl);
1020 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1025 mutex_unlock(&dev->lock);
1029 static int vidioc_s_ctrl(struct file *file, void *priv,
1030 struct v4l2_control *ctrl)
1032 struct em28xx_fh *fh = priv;
1033 struct em28xx *dev = fh->dev;
1037 rc = check_dev(dev);
1041 mutex_lock(&dev->lock);
1043 if (dev->board.has_msp34xx)
1044 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1047 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1048 if (ctrl->id == em28xx_qctrl[i].id) {
1049 if (ctrl->value < em28xx_qctrl[i].minimum ||
1050 ctrl->value > em28xx_qctrl[i].maximum) {
1055 rc = em28xx_set_ctrl(dev, ctrl);
1061 /* Control not found - try to send it to the attached devices */
1063 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1067 mutex_unlock(&dev->lock);
1071 static int vidioc_g_tuner(struct file *file, void *priv,
1072 struct v4l2_tuner *t)
1074 struct em28xx_fh *fh = priv;
1075 struct em28xx *dev = fh->dev;
1078 rc = check_dev(dev);
1085 strcpy(t->name, "Tuner");
1087 mutex_lock(&dev->lock);
1089 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1091 mutex_unlock(&dev->lock);
1095 static int vidioc_s_tuner(struct file *file, void *priv,
1096 struct v4l2_tuner *t)
1098 struct em28xx_fh *fh = priv;
1099 struct em28xx *dev = fh->dev;
1102 rc = check_dev(dev);
1109 mutex_lock(&dev->lock);
1111 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1113 mutex_unlock(&dev->lock);
1117 static int vidioc_g_frequency(struct file *file, void *priv,
1118 struct v4l2_frequency *f)
1120 struct em28xx_fh *fh = priv;
1121 struct em28xx *dev = fh->dev;
1123 mutex_lock(&dev->lock);
1124 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1125 f->frequency = dev->ctl_freq;
1126 mutex_unlock(&dev->lock);
1131 static int vidioc_s_frequency(struct file *file, void *priv,
1132 struct v4l2_frequency *f)
1134 struct em28xx_fh *fh = priv;
1135 struct em28xx *dev = fh->dev;
1138 rc = check_dev(dev);
1145 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1147 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1150 mutex_lock(&dev->lock);
1152 dev->ctl_freq = f->frequency;
1153 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1155 mutex_unlock(&dev->lock);
1160 #ifdef CONFIG_VIDEO_ADV_DEBUG
1161 static int em28xx_reg_len(int reg)
1164 case EM28XX_R40_AC97LSB:
1165 case EM28XX_R30_HSCALELOW:
1166 case EM28XX_R32_VSCALELOW:
1173 static int vidioc_g_chip_ident(struct file *file, void *priv,
1174 struct v4l2_dbg_chip_ident *chip)
1176 struct em28xx_fh *fh = priv;
1177 struct em28xx *dev = fh->dev;
1179 chip->ident = V4L2_IDENT_NONE;
1182 em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_CHIP_IDENT, chip);
1188 static int vidioc_g_register(struct file *file, void *priv,
1189 struct v4l2_dbg_register *reg)
1191 struct em28xx_fh *fh = priv;
1192 struct em28xx *dev = fh->dev;
1195 switch (reg->match.type) {
1196 case V4L2_CHIP_MATCH_AC97:
1197 mutex_lock(&dev->lock);
1198 ret = em28xx_read_ac97(dev, reg->reg);
1199 mutex_unlock(&dev->lock);
1206 case V4L2_CHIP_MATCH_I2C_DRIVER:
1207 em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_REGISTER, reg);
1209 case V4L2_CHIP_MATCH_I2C_ADDR:
1210 /* Not supported yet */
1213 if (!v4l2_chip_match_host(®->match))
1218 reg->size = em28xx_reg_len(reg->reg);
1219 if (reg->size == 1) {
1220 mutex_lock(&dev->lock);
1221 ret = em28xx_read_reg(dev, reg->reg);
1222 mutex_unlock(&dev->lock);
1230 mutex_lock(&dev->lock);
1231 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1232 reg->reg, (char *)&val, 2);
1233 mutex_unlock(&dev->lock);
1237 reg->val = le16_to_cpu(val);
1243 static int vidioc_s_register(struct file *file, void *priv,
1244 struct v4l2_dbg_register *reg)
1246 struct em28xx_fh *fh = priv;
1247 struct em28xx *dev = fh->dev;
1251 switch (reg->match.type) {
1252 case V4L2_CHIP_MATCH_AC97:
1253 mutex_lock(&dev->lock);
1254 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1255 mutex_unlock(&dev->lock);
1258 case V4L2_CHIP_MATCH_I2C_DRIVER:
1259 em28xx_i2c_call_clients(dev, VIDIOC_DBG_S_REGISTER, reg);
1261 case V4L2_CHIP_MATCH_I2C_ADDR:
1262 /* Not supported yet */
1265 if (!v4l2_chip_match_host(®->match))
1270 buf = cpu_to_le16(reg->val);
1272 mutex_lock(&dev->lock);
1273 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1274 em28xx_reg_len(reg->reg));
1275 mutex_unlock(&dev->lock);
1282 static int vidioc_cropcap(struct file *file, void *priv,
1283 struct v4l2_cropcap *cc)
1285 struct em28xx_fh *fh = priv;
1286 struct em28xx *dev = fh->dev;
1288 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1291 cc->bounds.left = 0;
1293 cc->bounds.width = dev->width;
1294 cc->bounds.height = dev->height;
1295 cc->defrect = cc->bounds;
1296 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1297 cc->pixelaspect.denominator = 59;
1302 static int vidioc_streamon(struct file *file, void *priv,
1303 enum v4l2_buf_type type)
1305 struct em28xx_fh *fh = priv;
1306 struct em28xx *dev = fh->dev;
1309 rc = check_dev(dev);
1314 mutex_lock(&dev->lock);
1317 if (likely(rc >= 0))
1318 rc = videobuf_streamon(&fh->vb_vidq);
1320 mutex_unlock(&dev->lock);
1325 static int vidioc_streamoff(struct file *file, void *priv,
1326 enum v4l2_buf_type type)
1328 struct em28xx_fh *fh = priv;
1329 struct em28xx *dev = fh->dev;
1332 rc = check_dev(dev);
1336 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1338 if (type != fh->type)
1341 mutex_lock(&dev->lock);
1343 videobuf_streamoff(&fh->vb_vidq);
1346 mutex_unlock(&dev->lock);
1351 static int vidioc_querycap(struct file *file, void *priv,
1352 struct v4l2_capability *cap)
1354 struct em28xx_fh *fh = priv;
1355 struct em28xx *dev = fh->dev;
1357 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1358 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1359 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1361 cap->version = EM28XX_VERSION_CODE;
1364 V4L2_CAP_SLICED_VBI_CAPTURE |
1365 V4L2_CAP_VIDEO_CAPTURE |
1367 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1369 if (dev->tuner_type != TUNER_ABSENT)
1370 cap->capabilities |= V4L2_CAP_TUNER;
1375 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1376 struct v4l2_fmtdesc *f)
1378 if (unlikely(f->index >= ARRAY_SIZE(format)))
1381 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1382 f->pixelformat = format[f->index].fourcc;
1387 /* Sliced VBI ioctls */
1388 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1389 struct v4l2_format *f)
1391 struct em28xx_fh *fh = priv;
1392 struct em28xx *dev = fh->dev;
1395 rc = check_dev(dev);
1399 mutex_lock(&dev->lock);
1401 f->fmt.sliced.service_set = 0;
1403 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1405 if (f->fmt.sliced.service_set == 0)
1408 mutex_unlock(&dev->lock);
1412 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1413 struct v4l2_format *f)
1415 struct em28xx_fh *fh = priv;
1416 struct em28xx *dev = fh->dev;
1419 rc = check_dev(dev);
1423 mutex_lock(&dev->lock);
1424 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1425 mutex_unlock(&dev->lock);
1427 if (f->fmt.sliced.service_set == 0)
1434 static int vidioc_reqbufs(struct file *file, void *priv,
1435 struct v4l2_requestbuffers *rb)
1437 struct em28xx_fh *fh = priv;
1438 struct em28xx *dev = fh->dev;
1441 rc = check_dev(dev);
1445 return videobuf_reqbufs(&fh->vb_vidq, rb);
1448 static int vidioc_querybuf(struct file *file, void *priv,
1449 struct v4l2_buffer *b)
1451 struct em28xx_fh *fh = priv;
1452 struct em28xx *dev = fh->dev;
1455 rc = check_dev(dev);
1459 return videobuf_querybuf(&fh->vb_vidq, b);
1462 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1464 struct em28xx_fh *fh = priv;
1465 struct em28xx *dev = fh->dev;
1468 rc = check_dev(dev);
1472 return videobuf_qbuf(&fh->vb_vidq, b);
1475 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1477 struct em28xx_fh *fh = priv;
1478 struct em28xx *dev = fh->dev;
1481 rc = check_dev(dev);
1485 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1488 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1489 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1491 struct em28xx_fh *fh = priv;
1493 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1498 /* ----------------------------------------------------------- */
1499 /* RADIO ESPECIFIC IOCTLS */
1500 /* ----------------------------------------------------------- */
1502 static int radio_querycap(struct file *file, void *priv,
1503 struct v4l2_capability *cap)
1505 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1507 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1508 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1509 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1511 cap->version = EM28XX_VERSION_CODE;
1512 cap->capabilities = V4L2_CAP_TUNER;
1516 static int radio_g_tuner(struct file *file, void *priv,
1517 struct v4l2_tuner *t)
1519 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1521 if (unlikely(t->index > 0))
1524 strcpy(t->name, "Radio");
1525 t->type = V4L2_TUNER_RADIO;
1527 mutex_lock(&dev->lock);
1528 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1529 mutex_unlock(&dev->lock);
1534 static int radio_enum_input(struct file *file, void *priv,
1535 struct v4l2_input *i)
1539 strcpy(i->name, "Radio");
1540 i->type = V4L2_INPUT_TYPE_TUNER;
1545 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1547 if (unlikely(a->index))
1550 strcpy(a->name, "Radio");
1554 static int radio_s_tuner(struct file *file, void *priv,
1555 struct v4l2_tuner *t)
1557 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1562 mutex_lock(&dev->lock);
1563 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1564 mutex_unlock(&dev->lock);
1569 static int radio_s_audio(struct file *file, void *fh,
1570 struct v4l2_audio *a)
1575 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1580 static int radio_queryctrl(struct file *file, void *priv,
1581 struct v4l2_queryctrl *qc)
1585 if (qc->id < V4L2_CID_BASE ||
1586 qc->id >= V4L2_CID_LASTP1)
1589 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1590 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1591 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1600 * em28xx_v4l2_open()
1601 * inits the device and starts isoc transfer
1603 static int em28xx_v4l2_open(struct file *filp)
1605 int minor = video_devdata(filp)->minor;
1606 int errCode = 0, radio;
1608 enum v4l2_buf_type fh_type;
1609 struct em28xx_fh *fh;
1611 dev = em28xx_get_device(minor, &fh_type, &radio);
1616 mutex_lock(&dev->lock);
1618 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1619 minor, v4l2_type_names[fh_type], dev->users);
1622 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1624 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1625 mutex_unlock(&dev->lock);
1631 filp->private_data = fh;
1633 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1634 dev->width = norm_maxw(dev);
1635 dev->height = norm_maxh(dev);
1639 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
1640 em28xx_set_alternate(dev);
1641 em28xx_resolution_set(dev);
1643 /* Needed, since GPIO might have disabled power of
1646 em28xx_wake_i2c(dev);
1650 em28xx_videodbg("video_open: setting radio device\n");
1651 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1656 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1657 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1658 sizeof(struct em28xx_buffer), fh);
1660 mutex_unlock(&dev->lock);
1666 * em28xx_realease_resources()
1667 * unregisters the v4l2,i2c and usb devices
1668 * called when the device gets disconected or at module unload
1670 void em28xx_release_analog_resources(struct em28xx *dev)
1673 /*FIXME: I2C IR should be disconnected */
1675 if (dev->radio_dev) {
1676 if (-1 != dev->radio_dev->minor)
1677 video_unregister_device(dev->radio_dev);
1679 video_device_release(dev->radio_dev);
1680 dev->radio_dev = NULL;
1683 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1685 if (-1 != dev->vbi_dev->minor)
1686 video_unregister_device(dev->vbi_dev);
1688 video_device_release(dev->vbi_dev);
1689 dev->vbi_dev = NULL;
1692 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1694 if (-1 != dev->vdev->minor)
1695 video_unregister_device(dev->vdev);
1697 video_device_release(dev->vdev);
1703 * em28xx_v4l2_close()
1704 * stops streaming and deallocates all resources allocated by the v4l2
1707 static int em28xx_v4l2_close(struct file *filp)
1709 struct em28xx_fh *fh = filp->private_data;
1710 struct em28xx *dev = fh->dev;
1713 em28xx_videodbg("users=%d\n", dev->users);
1716 mutex_lock(&dev->lock);
1720 if (dev->users == 1) {
1721 videobuf_stop(&fh->vb_vidq);
1722 videobuf_mmap_free(&fh->vb_vidq);
1724 /* the device is already disconnect,
1725 free the remaining resources */
1726 if (dev->state & DEV_DISCONNECTED) {
1727 em28xx_release_resources(dev);
1728 mutex_unlock(&dev->lock);
1733 /* Save some power by putting tuner to sleep */
1734 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1736 /* do this before setting alternate! */
1737 em28xx_uninit_isoc(dev);
1738 em28xx_set_mode(dev, EM28XX_SUSPEND);
1740 /* set alternate 0 */
1742 em28xx_videodbg("setting alternate 0\n");
1743 errCode = usb_set_interface(dev->udev, 0, 0);
1745 em28xx_errdev("cannot change alternate number to "
1746 "0 (error=%i)\n", errCode);
1751 wake_up_interruptible_nr(&dev->open, 1);
1752 mutex_unlock(&dev->lock);
1757 * em28xx_v4l2_read()
1758 * will allocate buffers when called for the first time
1761 em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1764 struct em28xx_fh *fh = filp->private_data;
1765 struct em28xx *dev = fh->dev;
1768 rc = check_dev(dev);
1772 /* FIXME: read() is not prepared to allow changing the video
1773 resolution while streaming. Seems a bug at em28xx_set_fmt
1776 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1777 mutex_lock(&dev->lock);
1779 mutex_unlock(&dev->lock);
1781 if (unlikely(rc < 0))
1784 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1785 filp->f_flags & O_NONBLOCK);
1791 * em28xx_v4l2_poll()
1792 * will allocate buffers when called for the first time
1794 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
1796 struct em28xx_fh *fh = filp->private_data;
1797 struct em28xx *dev = fh->dev;
1800 rc = check_dev(dev);
1804 mutex_lock(&dev->lock);
1806 mutex_unlock(&dev->lock);
1808 if (unlikely(rc < 0))
1811 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1814 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1818 * em28xx_v4l2_mmap()
1820 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1822 struct em28xx_fh *fh = filp->private_data;
1823 struct em28xx *dev = fh->dev;
1826 rc = check_dev(dev);
1830 mutex_lock(&dev->lock);
1832 mutex_unlock(&dev->lock);
1834 if (unlikely(rc < 0))
1837 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1839 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1840 (unsigned long)vma->vm_start,
1841 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1847 static const struct v4l2_file_operations em28xx_v4l_fops = {
1848 .owner = THIS_MODULE,
1849 .open = em28xx_v4l2_open,
1850 .release = em28xx_v4l2_close,
1851 .read = em28xx_v4l2_read,
1852 .poll = em28xx_v4l2_poll,
1853 .mmap = em28xx_v4l2_mmap,
1854 .ioctl = video_ioctl2,
1857 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1858 .vidioc_querycap = vidioc_querycap,
1859 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1860 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1861 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1862 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1863 .vidioc_g_audio = vidioc_g_audio,
1864 .vidioc_s_audio = vidioc_s_audio,
1865 .vidioc_cropcap = vidioc_cropcap,
1867 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1868 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1869 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1871 .vidioc_reqbufs = vidioc_reqbufs,
1872 .vidioc_querybuf = vidioc_querybuf,
1873 .vidioc_qbuf = vidioc_qbuf,
1874 .vidioc_dqbuf = vidioc_dqbuf,
1875 .vidioc_s_std = vidioc_s_std,
1876 .vidioc_enum_input = vidioc_enum_input,
1877 .vidioc_g_input = vidioc_g_input,
1878 .vidioc_s_input = vidioc_s_input,
1879 .vidioc_queryctrl = vidioc_queryctrl,
1880 .vidioc_g_ctrl = vidioc_g_ctrl,
1881 .vidioc_s_ctrl = vidioc_s_ctrl,
1882 .vidioc_streamon = vidioc_streamon,
1883 .vidioc_streamoff = vidioc_streamoff,
1884 .vidioc_g_tuner = vidioc_g_tuner,
1885 .vidioc_s_tuner = vidioc_s_tuner,
1886 .vidioc_g_frequency = vidioc_g_frequency,
1887 .vidioc_s_frequency = vidioc_s_frequency,
1888 #ifdef CONFIG_VIDEO_ADV_DEBUG
1889 .vidioc_g_register = vidioc_g_register,
1890 .vidioc_s_register = vidioc_s_register,
1891 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1893 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1894 .vidiocgmbuf = vidiocgmbuf,
1898 static const struct video_device em28xx_video_template = {
1899 .fops = &em28xx_v4l_fops,
1900 .release = video_device_release,
1901 .ioctl_ops = &video_ioctl_ops,
1905 .tvnorms = V4L2_STD_ALL,
1906 .current_norm = V4L2_STD_PAL,
1909 static const struct v4l2_file_operations radio_fops = {
1910 .owner = THIS_MODULE,
1911 .open = em28xx_v4l2_open,
1912 .release = em28xx_v4l2_close,
1913 .ioctl = video_ioctl2,
1916 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1917 .vidioc_querycap = radio_querycap,
1918 .vidioc_g_tuner = radio_g_tuner,
1919 .vidioc_enum_input = radio_enum_input,
1920 .vidioc_g_audio = radio_g_audio,
1921 .vidioc_s_tuner = radio_s_tuner,
1922 .vidioc_s_audio = radio_s_audio,
1923 .vidioc_s_input = radio_s_input,
1924 .vidioc_queryctrl = radio_queryctrl,
1925 .vidioc_g_ctrl = vidioc_g_ctrl,
1926 .vidioc_s_ctrl = vidioc_s_ctrl,
1927 .vidioc_g_frequency = vidioc_g_frequency,
1928 .vidioc_s_frequency = vidioc_s_frequency,
1929 #ifdef CONFIG_VIDEO_ADV_DEBUG
1930 .vidioc_g_register = vidioc_g_register,
1931 .vidioc_s_register = vidioc_s_register,
1935 static struct video_device em28xx_radio_template = {
1936 .name = "em28xx-radio",
1937 .fops = &radio_fops,
1938 .ioctl_ops = &radio_ioctl_ops,
1942 /******************************** usb interface ******************************/
1946 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1947 const struct video_device *template,
1948 const char *type_name)
1950 struct video_device *vfd;
1952 vfd = video_device_alloc();
1957 vfd->parent = &dev->udev->dev;
1958 vfd->release = video_device_release;
1959 vfd->debug = video_debug;
1961 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1962 dev->name, type_name);
1967 int em28xx_register_analog_devices(struct em28xx *dev)
1972 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
1974 (EM28XX_VERSION_CODE >> 16) & 0xff,
1975 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
1977 /* set default norm */
1978 dev->norm = em28xx_video_template.current_norm;
1979 dev->width = norm_maxw(dev);
1980 dev->height = norm_maxh(dev);
1981 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1986 /* Analog specific initialization */
1987 dev->format = &format[0];
1988 video_mux(dev, dev->ctl_input);
1990 /* Audio defaults */
1994 /* enable vbi capturing */
1996 /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
1997 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
1998 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
1999 (EM28XX_XCLK_AUDIO_UNMUTE | val));
2000 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
2002 em28xx_set_outfmt(dev);
2003 em28xx_colorlevels_set_default(dev);
2004 em28xx_compression_disable(dev);
2006 /* allocate and fill video video_device struct */
2007 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2009 em28xx_errdev("cannot allocate video_device.\n");
2013 /* register v4l2 video video_device */
2014 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2015 video_nr[dev->devno]);
2017 em28xx_errdev("unable to register video device (error=%i).\n",
2022 /* Allocate and fill vbi video_device struct */
2023 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2025 /* register v4l2 vbi video_device */
2026 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2027 vbi_nr[dev->devno]);
2029 em28xx_errdev("unable to register vbi device\n");
2033 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2034 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2036 if (!dev->radio_dev) {
2037 em28xx_errdev("cannot allocate video_device.\n");
2040 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2041 radio_nr[dev->devno]);
2043 em28xx_errdev("can't register radio device\n");
2046 em28xx_info("Registered radio device as /dev/radio%d\n",
2047 dev->radio_dev->num);
2050 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2051 dev->vdev->num, dev->vbi_dev->num);