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>
9 Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
11 Some parts based on SN9C10x PC Camera Controllers GPL driver made
12 by Luca Risolia <luca.risolia@studio.unibo.it>
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <linux/init.h>
30 #include <linux/list.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/bitmap.h>
34 #include <linux/usb.h>
35 #include <linux/i2c.h>
37 #include <linux/mutex.h>
38 #include <linux/slab.h>
41 #include <media/v4l2-common.h>
42 #include <media/v4l2-ioctl.h>
43 #include <media/v4l2-chip-ident.h>
44 #include <media/msp3400.h>
45 #include <media/tuner.h>
47 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
48 "Markus Rechberger <mrechberger@gmail.com>, " \
49 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
50 "Sascha Sommer <saschasommer@freenet.de>"
52 #define DRIVER_DESC "Empia em28xx based USB video device driver"
54 #define EM28XX_VERSION "0.1.3"
56 #define em28xx_videodbg(fmt, arg...) do {\
58 printk(KERN_INFO "%s %s :"fmt, \
59 dev->name, __func__ , ##arg); } while (0)
61 static unsigned int isoc_debug;
62 module_param(isoc_debug, int, 0644);
63 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
65 #define em28xx_isocdbg(fmt, arg...) \
68 printk(KERN_INFO "%s %s :"fmt, \
69 dev->name, __func__ , ##arg); \
73 MODULE_AUTHOR(DRIVER_AUTHOR);
74 MODULE_DESCRIPTION(DRIVER_DESC);
75 MODULE_LICENSE("GPL");
76 MODULE_VERSION(EM28XX_VERSION);
78 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
82 module_param_array(video_nr, int, NULL, 0444);
83 module_param_array(vbi_nr, int, NULL, 0444);
84 module_param_array(radio_nr, int, NULL, 0444);
85 MODULE_PARM_DESC(video_nr, "video device numbers");
86 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
87 MODULE_PARM_DESC(radio_nr, "radio device numbers");
89 static unsigned int video_debug;
90 module_param(video_debug, int, 0644);
91 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
93 /* supported video standards */
94 static struct em28xx_fmt format[] = {
96 .name = "16 bpp YUY2, 4:2:2, packed",
97 .fourcc = V4L2_PIX_FMT_YUYV,
99 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
101 .name = "16 bpp RGB 565, LE",
102 .fourcc = V4L2_PIX_FMT_RGB565,
104 .reg = EM28XX_OUTFMT_RGB_16_656,
106 .name = "8 bpp Bayer BGBG..GRGR",
107 .fourcc = V4L2_PIX_FMT_SBGGR8,
109 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
111 .name = "8 bpp Bayer GRGR..BGBG",
112 .fourcc = V4L2_PIX_FMT_SGRBG8,
114 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
116 .name = "8 bpp Bayer GBGB..RGRG",
117 .fourcc = V4L2_PIX_FMT_SGBRG8,
119 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
121 .name = "12 bpp YUV411",
122 .fourcc = V4L2_PIX_FMT_YUV411P,
124 .reg = EM28XX_OUTFMT_YUV411,
128 /* supported controls */
129 /* Common to all boards */
130 static struct v4l2_queryctrl ac97_qctrl[] = {
132 .id = V4L2_CID_AUDIO_VOLUME,
133 .type = V4L2_CTRL_TYPE_INTEGER,
138 .default_value = 0x1f,
139 .flags = V4L2_CTRL_FLAG_SLIDER,
141 .id = V4L2_CID_AUDIO_MUTE,
142 .type = V4L2_CTRL_TYPE_BOOLEAN,
152 /* ------------------------------------------------------------------
153 DMA and thread functions
154 ------------------------------------------------------------------*/
157 * Announces that a buffer were filled and request the next
159 static inline void buffer_filled(struct em28xx *dev,
160 struct em28xx_dmaqueue *dma_q,
161 struct em28xx_buffer *buf)
163 /* Advice that buffer was filled */
164 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
165 buf->vb.state = VIDEOBUF_DONE;
166 buf->vb.field_count++;
167 v4l2_get_timestamp(&buf->vb.ts);
169 dev->usb_ctl.vid_buf = NULL;
171 list_del(&buf->vb.queue);
172 wake_up(&buf->vb.done);
175 static inline void vbi_buffer_filled(struct em28xx *dev,
176 struct em28xx_dmaqueue *dma_q,
177 struct em28xx_buffer *buf)
179 /* Advice that buffer was filled */
180 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
182 buf->vb.state = VIDEOBUF_DONE;
183 buf->vb.field_count++;
184 v4l2_get_timestamp(&buf->vb.ts);
186 dev->usb_ctl.vbi_buf = NULL;
188 list_del(&buf->vb.queue);
189 wake_up(&buf->vb.done);
193 * Identify the buffer header type and properly handles
195 static void em28xx_copy_video(struct em28xx *dev,
196 struct em28xx_dmaqueue *dma_q,
197 struct em28xx_buffer *buf,
199 unsigned char *outp, unsigned long len)
201 void *fieldstart, *startwrite, *startread;
202 int linesdone, currlinedone, offset, lencopy, remain;
203 int bytesperline = dev->width << 1;
205 if (dma_q->pos + len > buf->vb.size)
206 len = buf->vb.size - dma_q->pos;
211 if (dev->progressive || buf->top_field)
213 else /* interlaced mode, even nr. of lines */
214 fieldstart = outp + bytesperline;
216 linesdone = dma_q->pos / bytesperline;
217 currlinedone = dma_q->pos % bytesperline;
219 if (dev->progressive)
220 offset = linesdone * bytesperline + currlinedone;
222 offset = linesdone * bytesperline * 2 + currlinedone;
224 startwrite = fieldstart + offset;
225 lencopy = bytesperline - currlinedone;
226 lencopy = lencopy > remain ? remain : lencopy;
228 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
229 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
230 ((char *)startwrite + lencopy) -
231 ((char *)outp + buf->vb.size));
232 remain = (char *)outp + buf->vb.size - (char *)startwrite;
237 memcpy(startwrite, startread, lencopy);
242 if (dev->progressive)
243 startwrite += lencopy;
245 startwrite += lencopy + bytesperline;
246 startread += lencopy;
247 if (bytesperline > remain)
250 lencopy = bytesperline;
252 if ((char *)startwrite + lencopy > (char *)outp +
254 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
256 ((char *)startwrite + lencopy) -
257 ((char *)outp + buf->vb.size));
258 lencopy = remain = (char *)outp + buf->vb.size -
264 memcpy(startwrite, startread, lencopy);
272 static void em28xx_copy_vbi(struct em28xx *dev,
273 struct em28xx_dmaqueue *dma_q,
274 struct em28xx_buffer *buf,
276 unsigned char *outp, unsigned long len)
278 void *startwrite, *startread;
283 em28xx_isocdbg("dev is null\n");
286 bytesperline = dev->vbi_width;
289 em28xx_isocdbg("dma_q is null\n");
296 em28xx_isocdbg("p is null\n");
300 em28xx_isocdbg("outp is null\n");
304 if (dma_q->pos + len > buf->vb.size)
305 len = buf->vb.size - dma_q->pos;
309 startwrite = outp + dma_q->pos;
312 /* Make sure the bottom field populates the second half of the frame */
313 if (buf->top_field == 0) {
314 startwrite += bytesperline * dev->vbi_height;
315 offset += bytesperline * dev->vbi_height;
318 memcpy(startwrite, startread, len);
322 static inline void print_err_status(struct em28xx *dev,
323 int packet, int status)
325 char *errmsg = "Unknown";
329 errmsg = "unlinked synchronuously";
332 errmsg = "unlinked asynchronuously";
335 errmsg = "Buffer error (overrun)";
338 errmsg = "Stalled (device not responding)";
341 errmsg = "Babble (bad cable?)";
344 errmsg = "Bit-stuff error (bad cable?)";
347 errmsg = "CRC/Timeout (could be anything)";
350 errmsg = "Device does not respond";
354 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
356 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
357 packet, status, errmsg);
362 * video-buf generic routine to get the next available buffer
364 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
365 struct em28xx_buffer **buf)
367 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
370 if (list_empty(&dma_q->active)) {
371 em28xx_isocdbg("No active queue to serve\n");
372 dev->usb_ctl.vid_buf = NULL;
377 /* Get the next buffer */
378 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
380 /* Cleans up buffer - Useful for testing for frame/URB loss */
381 outp = videobuf_to_vmalloc(&(*buf)->vb);
382 memset(outp, 0, (*buf)->vb.size);
384 dev->usb_ctl.vid_buf = *buf;
390 * video-buf generic routine to get the next available VBI buffer
392 static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
393 struct em28xx_buffer **buf)
395 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
398 if (list_empty(&dma_q->active)) {
399 em28xx_isocdbg("No active queue to serve\n");
400 dev->usb_ctl.vbi_buf = NULL;
405 /* Get the next buffer */
406 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
407 /* Cleans up buffer - Useful for testing for frame/URB loss */
408 outp = videobuf_to_vmalloc(&(*buf)->vb);
409 memset(outp, 0x00, (*buf)->vb.size);
411 dev->usb_ctl.vbi_buf = *buf;
416 /* Processes and copies the URB data content to a frame buffer queue */
417 static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
419 struct em28xx_buffer *buf;
420 struct em28xx_dmaqueue *dma_q = &dev->vidq;
421 int xfer_bulk, num_packets, i, rc = 1;
422 unsigned int actual_length, len = 0;
423 unsigned char *p, *outp = NULL;
428 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
432 print_err_status(dev, -1, urb->status);
434 xfer_bulk = usb_pipebulk(urb->pipe);
436 buf = dev->usb_ctl.vid_buf;
438 outp = videobuf_to_vmalloc(&buf->vb);
440 if (xfer_bulk) /* bulk */
443 num_packets = urb->number_of_packets;
445 for (i = 0; i < num_packets; i++) {
446 if (xfer_bulk) { /* bulk */
447 actual_length = urb->actual_length;
449 p = urb->transfer_buffer;
451 if (urb->iso_frame_desc[i].status < 0) {
452 print_err_status(dev, i,
453 urb->iso_frame_desc[i].status);
454 if (urb->iso_frame_desc[i].status != -EPROTO)
458 actual_length = urb->iso_frame_desc[i].actual_length;
459 if (actual_length > dev->max_pkt_size) {
460 em28xx_isocdbg("packet bigger than packet size");
464 p = urb->transfer_buffer +
465 urb->iso_frame_desc[i].offset;
468 if (actual_length <= 0) {
469 /* NOTE: happens very often with isoc transfers */
470 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
474 /* FIXME: incomplete buffer checks where removed to make
475 logic simpler. Impacts of those changes should be evaluated
477 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
478 em28xx_isocdbg("VBI HEADER!!!\n");
479 /* FIXME: Should add vbi copy */
482 if (p[0] == 0x22 && p[1] == 0x5a) {
483 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
484 len, (p[2] & 1) ? "odd" : "even");
486 if (dev->progressive || !(p[2] & 1)) {
488 buffer_filled(dev, dma_q, buf);
489 get_next_buf(dma_q, &buf);
493 outp = videobuf_to_vmalloc(&buf->vb);
506 if (p[0] != 0x88 && p[0] != 0x22) {
507 /* NOTE: no intermediate data packet header
508 * 88 88 88 88 when using bulk transfers */
509 em28xx_isocdbg("frame is not complete\n");
512 len = actual_length - 4;
515 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
521 /* Version of the urb data handler that takes into account a mixture of
522 video and VBI data */
523 static inline int em28xx_urb_data_copy_vbi(struct em28xx *dev, struct urb *urb)
525 struct em28xx_buffer *buf, *vbi_buf;
526 struct em28xx_dmaqueue *dma_q = &dev->vidq;
527 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
528 int xfer_bulk, vbi_size, num_packets, i, rc = 1;
529 unsigned int actual_length, len = 0;
530 unsigned char *p, *outp = NULL, *vbioutp = NULL;
535 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
539 print_err_status(dev, -1, urb->status);
541 xfer_bulk = usb_pipebulk(urb->pipe);
543 buf = dev->usb_ctl.vid_buf;
545 outp = videobuf_to_vmalloc(&buf->vb);
547 vbi_buf = dev->usb_ctl.vbi_buf;
549 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
551 if (xfer_bulk) /* bulk */
554 num_packets = urb->number_of_packets;
556 for (i = 0; i < num_packets; i++) {
557 if (xfer_bulk) { /* bulk */
558 actual_length = urb->actual_length;
560 p = urb->transfer_buffer;
562 if (urb->iso_frame_desc[i].status < 0) {
563 print_err_status(dev, i,
564 urb->iso_frame_desc[i].status);
565 if (urb->iso_frame_desc[i].status != -EPROTO)
569 actual_length = urb->iso_frame_desc[i].actual_length;
570 if (actual_length > dev->max_pkt_size) {
571 em28xx_isocdbg("packet bigger than packet size");
575 p = urb->transfer_buffer +
576 urb->iso_frame_desc[i].offset;
579 if (actual_length <= 0) {
580 /* NOTE: happens very often with isoc transfers */
581 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
585 /* capture type 0 = vbi start
586 capture type 1 = video start
587 capture type 2 = video in progress */
588 if (p[0] == 0x33 && p[1] == 0x95) {
589 dev->capture_type = 0;
591 em28xx_isocdbg("VBI START HEADER!!!\n");
592 dev->cur_field = p[2];
594 len = actual_length - 4;
595 } else if (p[0] == 0x88 && p[1] == 0x88 &&
596 p[2] == 0x88 && p[3] == 0x88) {
599 len = actual_length - 4;
600 } else if (p[0] == 0x22 && p[1] == 0x5a) {
603 len = actual_length - 4;
605 /* NOTE: With bulk transfers, intermediate data packets
606 * have no continuation header */
610 vbi_size = dev->vbi_width * dev->vbi_height;
612 if (dev->capture_type == 0) {
613 if (dev->vbi_read >= vbi_size) {
614 /* We've already read all the VBI data, so
615 treat the rest as video */
616 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
617 } else if ((dev->vbi_read + len) < vbi_size) {
618 /* This entire frame is VBI data */
619 if (dev->vbi_read == 0 &&
620 (!(dev->cur_field & 1))) {
621 /* Brand new frame */
623 vbi_buffer_filled(dev,
626 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
630 vbioutp = videobuf_to_vmalloc(
634 if (dev->vbi_read == 0) {
636 if (vbi_buf != NULL) {
637 if (dev->cur_field & 1)
638 vbi_buf->top_field = 0;
640 vbi_buf->top_field = 1;
644 dev->vbi_read += len;
645 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
648 /* Some of this frame is VBI data and some is
650 int vbi_data_len = vbi_size - dev->vbi_read;
651 dev->vbi_read += vbi_data_len;
652 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
653 vbioutp, vbi_data_len);
654 dev->capture_type = 1;
660 if (dev->capture_type == 1) {
661 dev->capture_type = 2;
662 if (dev->progressive || !(dev->cur_field & 1)) {
664 buffer_filled(dev, dma_q, buf);
665 get_next_buf(dma_q, &buf);
669 outp = videobuf_to_vmalloc(&buf->vb);
672 if (dev->cur_field & 1)
681 if (buf != NULL && dev->capture_type == 2) {
682 if (len >= 4 && p[0] == 0x88 && p[1] == 0x88 &&
683 p[2] == 0x88 && p[3] == 0x88) {
687 if (len >= 4 && p[0] == 0x22 && p[1] == 0x5a) {
688 em28xx_isocdbg("Video frame %d, len=%i, %s\n",
689 p[2], len, (p[2] & 1) ?
696 em28xx_copy_video(dev, dma_q, buf, p, outp,
704 /* ------------------------------------------------------------------
706 ------------------------------------------------------------------*/
709 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
711 struct em28xx_fh *fh = vq->priv_data;
712 struct em28xx *dev = fh->dev;
713 struct v4l2_frequency f;
715 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
719 *count = EM28XX_DEF_BUF;
721 if (*count < EM28XX_MIN_BUF)
722 *count = EM28XX_MIN_BUF;
724 /* Ask tuner to go to analog or radio mode */
725 memset(&f, 0, sizeof(f));
726 f.frequency = dev->ctl_freq;
727 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
729 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
734 /* This is called *without* dev->slock held; please keep it that way */
735 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
737 struct em28xx_fh *fh = vq->priv_data;
738 struct em28xx *dev = fh->dev;
739 unsigned long flags = 0;
743 /* We used to wait for the buffer to finish here, but this didn't work
744 because, as we were keeping the state as VIDEOBUF_QUEUED,
745 videobuf_queue_cancel marked it as finished for us.
746 (Also, it could wedge forever if the hardware was misconfigured.)
748 This should be safe; by the time we get here, the buffer isn't
749 queued anymore. If we ever start marking the buffers as
750 VIDEOBUF_ACTIVE, it won't be, though.
752 spin_lock_irqsave(&dev->slock, flags);
753 if (dev->usb_ctl.vid_buf == buf)
754 dev->usb_ctl.vid_buf = NULL;
755 spin_unlock_irqrestore(&dev->slock, flags);
757 videobuf_vmalloc_free(&buf->vb);
758 buf->vb.state = VIDEOBUF_NEEDS_INIT;
762 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
763 enum v4l2_field field)
765 struct em28xx_fh *fh = vq->priv_data;
766 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
767 struct em28xx *dev = fh->dev;
768 int rc = 0, urb_init = 0;
770 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
773 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
776 buf->vb.width = dev->width;
777 buf->vb.height = dev->height;
778 buf->vb.field = field;
780 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
781 rc = videobuf_iolock(vq, &buf->vb, NULL);
786 if (!dev->usb_ctl.analog_bufs.num_bufs)
790 if (em28xx_vbi_supported(dev) == 1)
791 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE, 0,
794 EM28XX_NUM_ISOC_PACKETS,
795 em28xx_urb_data_copy_vbi);
797 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE, 0,
800 EM28XX_NUM_ISOC_PACKETS,
801 em28xx_urb_data_copy);
806 buf->vb.state = VIDEOBUF_PREPARED;
810 free_buffer(vq, buf);
815 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
817 struct em28xx_buffer *buf = container_of(vb,
818 struct em28xx_buffer,
820 struct em28xx_fh *fh = vq->priv_data;
821 struct em28xx *dev = fh->dev;
822 struct em28xx_dmaqueue *vidq = &dev->vidq;
824 buf->vb.state = VIDEOBUF_QUEUED;
825 list_add_tail(&buf->vb.queue, &vidq->active);
829 static void buffer_release(struct videobuf_queue *vq,
830 struct videobuf_buffer *vb)
832 struct em28xx_buffer *buf = container_of(vb,
833 struct em28xx_buffer,
835 struct em28xx_fh *fh = vq->priv_data;
836 struct em28xx *dev = (struct em28xx *)fh->dev;
838 em28xx_isocdbg("em28xx: called buffer_release\n");
840 free_buffer(vq, buf);
843 static struct videobuf_queue_ops em28xx_video_qops = {
844 .buf_setup = buffer_setup,
845 .buf_prepare = buffer_prepare,
846 .buf_queue = buffer_queue,
847 .buf_release = buffer_release,
850 /********************* v4l2 interface **************************************/
852 static void video_mux(struct em28xx *dev, int index)
854 dev->ctl_input = index;
855 dev->ctl_ainput = INPUT(index)->amux;
856 dev->ctl_aoutput = INPUT(index)->aout;
858 if (!dev->ctl_aoutput)
859 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
861 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
862 INPUT(index)->vmux, 0, 0);
864 if (dev->board.has_msp34xx) {
865 if (dev->i2s_speed) {
866 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
867 s_i2s_clock_freq, dev->i2s_speed);
869 /* Note: this is msp3400 specific */
870 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
871 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
874 if (dev->board.adecoder != EM28XX_NOADECODER) {
875 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
876 dev->ctl_ainput, dev->ctl_aoutput, 0);
879 em28xx_audio_analog_set(dev);
882 /* Usage lock check functions */
883 static int res_get(struct em28xx_fh *fh, unsigned int bit)
885 struct em28xx *dev = fh->dev;
887 if (fh->resources & bit)
888 /* have it already allocated */
892 if (dev->resources & bit) {
893 /* no, someone else uses it */
896 /* it's free, grab it */
897 fh->resources |= bit;
898 dev->resources |= bit;
899 em28xx_videodbg("res: get %d\n", bit);
903 static int res_check(struct em28xx_fh *fh, unsigned int bit)
905 return fh->resources & bit;
908 static int res_locked(struct em28xx *dev, unsigned int bit)
910 return dev->resources & bit;
913 static void res_free(struct em28xx_fh *fh, unsigned int bits)
915 struct em28xx *dev = fh->dev;
917 BUG_ON((fh->resources & bits) != bits);
919 fh->resources &= ~bits;
920 dev->resources &= ~bits;
921 em28xx_videodbg("res: put %d\n", bits);
924 static int get_ressource(struct em28xx_fh *fh)
927 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
928 return EM28XX_RESOURCE_VIDEO;
929 case V4L2_BUF_TYPE_VBI_CAPTURE:
930 return EM28XX_RESOURCE_VBI;
939 * return the ac97 supported controls
941 static int ac97_queryctrl(struct v4l2_queryctrl *qc)
945 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
946 if (qc->id && qc->id == ac97_qctrl[i].id) {
947 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
952 /* Control is not ac97 related */
958 * return the current values for ac97 mute and volume
960 static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
963 case V4L2_CID_AUDIO_MUTE:
964 ctrl->value = dev->mute;
966 case V4L2_CID_AUDIO_VOLUME:
967 ctrl->value = dev->volume;
970 /* Control is not ac97 related */
977 * set values for ac97 mute and volume
979 static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
983 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
984 if (ctrl->id == ac97_qctrl[i].id)
987 /* Announce that hasn't handle it */
991 if (ctrl->value < ac97_qctrl[i].minimum ||
992 ctrl->value > ac97_qctrl[i].maximum)
996 case V4L2_CID_AUDIO_MUTE:
997 dev->mute = ctrl->value;
999 case V4L2_CID_AUDIO_VOLUME:
1000 dev->volume = ctrl->value;
1004 return em28xx_audio_analog_set(dev);
1007 static int check_dev(struct em28xx *dev)
1009 if (dev->state & DEV_DISCONNECTED) {
1010 em28xx_errdev("v4l2 ioctl: device not present\n");
1014 if (dev->state & DEV_MISCONFIGURED) {
1015 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
1016 "close and open it again\n");
1022 static void get_scale(struct em28xx *dev,
1023 unsigned int width, unsigned int height,
1024 unsigned int *hscale, unsigned int *vscale)
1026 unsigned int maxw = norm_maxw(dev);
1027 unsigned int maxh = norm_maxh(dev);
1029 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1030 if (*hscale >= 0x4000)
1033 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1034 if (*vscale >= 0x4000)
1038 /* ------------------------------------------------------------------
1039 IOCTL vidioc handling
1040 ------------------------------------------------------------------*/
1042 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1043 struct v4l2_format *f)
1045 struct em28xx_fh *fh = priv;
1046 struct em28xx *dev = fh->dev;
1048 f->fmt.pix.width = dev->width;
1049 f->fmt.pix.height = dev->height;
1050 f->fmt.pix.pixelformat = dev->format->fourcc;
1051 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
1052 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
1053 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1055 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1056 if (dev->progressive)
1057 f->fmt.pix.field = V4L2_FIELD_NONE;
1059 f->fmt.pix.field = dev->interlaced ?
1060 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1064 static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1068 for (i = 0; i < ARRAY_SIZE(format); i++)
1069 if (format[i].fourcc == fourcc)
1075 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1076 struct v4l2_format *f)
1078 struct em28xx_fh *fh = priv;
1079 struct em28xx *dev = fh->dev;
1080 unsigned int width = f->fmt.pix.width;
1081 unsigned int height = f->fmt.pix.height;
1082 unsigned int maxw = norm_maxw(dev);
1083 unsigned int maxh = norm_maxh(dev);
1084 unsigned int hscale, vscale;
1085 struct em28xx_fmt *fmt;
1087 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1089 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1090 f->fmt.pix.pixelformat);
1094 if (dev->board.is_em2800) {
1095 /* the em2800 can only scale down to 50% */
1096 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1097 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
1098 /* MaxPacketSize for em2800 is too small to capture at full resolution
1099 * use half of maxw as the scaler can only scale to 50% */
1100 if (width == maxw && height == maxh)
1103 /* width must even because of the YUYV format
1104 height must be even because of interlacing */
1105 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1109 get_scale(dev, width, height, &hscale, &vscale);
1111 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1112 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1114 f->fmt.pix.width = width;
1115 f->fmt.pix.height = height;
1116 f->fmt.pix.pixelformat = fmt->fourcc;
1117 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1118 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1119 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1120 if (dev->progressive)
1121 f->fmt.pix.field = V4L2_FIELD_NONE;
1123 f->fmt.pix.field = dev->interlaced ?
1124 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1129 static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1130 unsigned width, unsigned height)
1132 struct em28xx_fmt *fmt;
1134 fmt = format_by_fourcc(fourcc);
1140 dev->height = height;
1142 /* set new image size */
1143 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1145 em28xx_set_alternate(dev);
1146 em28xx_resolution_set(dev);
1151 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1152 struct v4l2_format *f)
1154 struct em28xx_fh *fh = priv;
1155 struct em28xx *dev = fh->dev;
1158 rc = check_dev(dev);
1162 vidioc_try_fmt_vid_cap(file, priv, f);
1164 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1165 em28xx_errdev("%s queue busy\n", __func__);
1169 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
1170 f->fmt.pix.width, f->fmt.pix.height);
1173 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1175 struct em28xx_fh *fh = priv;
1176 struct em28xx *dev = fh->dev;
1179 rc = check_dev(dev);
1188 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1190 struct em28xx_fh *fh = priv;
1191 struct em28xx *dev = fh->dev;
1194 rc = check_dev(dev);
1198 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1203 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
1205 struct em28xx_fh *fh = priv;
1206 struct em28xx *dev = fh->dev;
1207 struct v4l2_format f;
1210 rc = check_dev(dev);
1216 /* Adjusts width/height, if needed */
1217 f.fmt.pix.width = dev->width;
1218 f.fmt.pix.height = dev->height;
1219 vidioc_try_fmt_vid_cap(file, priv, &f);
1221 /* set new image size */
1222 dev->width = f.fmt.pix.width;
1223 dev->height = f.fmt.pix.height;
1224 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1226 em28xx_resolution_set(dev);
1227 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
1232 static int vidioc_g_parm(struct file *file, void *priv,
1233 struct v4l2_streamparm *p)
1235 struct em28xx_fh *fh = priv;
1236 struct em28xx *dev = fh->dev;
1239 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1242 if (dev->board.is_webcam)
1243 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1246 v4l2_video_std_frame_period(dev->norm,
1247 &p->parm.capture.timeperframe);
1252 static int vidioc_s_parm(struct file *file, void *priv,
1253 struct v4l2_streamparm *p)
1255 struct em28xx_fh *fh = priv;
1256 struct em28xx *dev = fh->dev;
1258 if (!dev->board.is_webcam)
1261 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1264 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1267 static const char *iname[] = {
1268 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1269 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1270 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1271 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1272 [EM28XX_VMUX_SVIDEO] = "S-Video",
1273 [EM28XX_VMUX_TELEVISION] = "Television",
1274 [EM28XX_VMUX_CABLE] = "Cable TV",
1275 [EM28XX_VMUX_DVB] = "DVB",
1276 [EM28XX_VMUX_DEBUG] = "for debug only",
1279 static int vidioc_enum_input(struct file *file, void *priv,
1280 struct v4l2_input *i)
1282 struct em28xx_fh *fh = priv;
1283 struct em28xx *dev = fh->dev;
1287 if (n >= MAX_EM28XX_INPUT)
1289 if (0 == INPUT(n)->type)
1293 i->type = V4L2_INPUT_TYPE_CAMERA;
1295 strcpy(i->name, iname[INPUT(n)->type]);
1297 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1298 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1299 i->type = V4L2_INPUT_TYPE_TUNER;
1301 i->std = dev->vdev->tvnorms;
1306 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1308 struct em28xx_fh *fh = priv;
1309 struct em28xx *dev = fh->dev;
1311 *i = dev->ctl_input;
1316 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1318 struct em28xx_fh *fh = priv;
1319 struct em28xx *dev = fh->dev;
1322 rc = check_dev(dev);
1326 if (i >= MAX_EM28XX_INPUT)
1328 if (0 == INPUT(i)->type)
1335 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1337 struct em28xx_fh *fh = priv;
1338 struct em28xx *dev = fh->dev;
1340 if (!dev->audio_mode.has_audio)
1344 case EM28XX_AMUX_VIDEO:
1345 strcpy(a->name, "Television");
1347 case EM28XX_AMUX_LINE_IN:
1348 strcpy(a->name, "Line In");
1350 case EM28XX_AMUX_VIDEO2:
1351 strcpy(a->name, "Television alt");
1353 case EM28XX_AMUX_PHONE:
1354 strcpy(a->name, "Phone");
1356 case EM28XX_AMUX_MIC:
1357 strcpy(a->name, "Mic");
1359 case EM28XX_AMUX_CD:
1360 strcpy(a->name, "CD");
1362 case EM28XX_AMUX_AUX:
1363 strcpy(a->name, "Aux");
1365 case EM28XX_AMUX_PCM_OUT:
1366 strcpy(a->name, "PCM");
1372 a->index = dev->ctl_ainput;
1373 a->capability = V4L2_AUDCAP_STEREO;
1378 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1380 struct em28xx_fh *fh = priv;
1381 struct em28xx *dev = fh->dev;
1384 if (!dev->audio_mode.has_audio)
1387 if (a->index >= MAX_EM28XX_INPUT)
1389 if (0 == INPUT(a->index)->type)
1392 dev->ctl_ainput = INPUT(a->index)->amux;
1393 dev->ctl_aoutput = INPUT(a->index)->aout;
1395 if (!dev->ctl_aoutput)
1396 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
1401 static int vidioc_queryctrl(struct file *file, void *priv,
1402 struct v4l2_queryctrl *qc)
1404 struct em28xx_fh *fh = priv;
1405 struct em28xx *dev = fh->dev;
1409 rc = check_dev(dev);
1413 memset(qc, 0, sizeof(*qc));
1417 /* enumerate AC97 controls */
1418 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1419 rc = ac97_queryctrl(qc);
1424 /* enumerate V4L2 device controls */
1425 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
1434 * FIXME: This is an indirect way to check if a control exists at a
1435 * subdev. Instead of that hack, maybe the better would be to change all
1436 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1438 static int check_subdev_ctrl(struct em28xx *dev, int id)
1440 struct v4l2_queryctrl qc;
1442 memset(&qc, 0, sizeof(qc));
1445 /* enumerate V4L2 device controls */
1446 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1454 static int vidioc_g_ctrl(struct file *file, void *priv,
1455 struct v4l2_control *ctrl)
1457 struct em28xx_fh *fh = priv;
1458 struct em28xx *dev = fh->dev;
1461 rc = check_dev(dev);
1466 /* Set an AC97 control */
1467 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1468 rc = ac97_get_ctrl(dev, ctrl);
1472 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1474 if (check_subdev_ctrl(dev, ctrl->id))
1477 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1484 static int vidioc_s_ctrl(struct file *file, void *priv,
1485 struct v4l2_control *ctrl)
1487 struct em28xx_fh *fh = priv;
1488 struct em28xx *dev = fh->dev;
1491 rc = check_dev(dev);
1495 /* Set an AC97 control */
1496 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1497 rc = ac97_set_ctrl(dev, ctrl);
1501 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
1503 rc = check_subdev_ctrl(dev, ctrl->id);
1505 v4l2_device_call_all(&dev->v4l2_dev, 0,
1506 core, s_ctrl, ctrl);
1508 * In the case of non-AC97 volume controls, we still need
1509 * to do some setups at em28xx, in order to mute/unmute
1510 * and to adjust audio volume. However, the value ranges
1511 * should be checked by the corresponding V4L subdriver.
1514 case V4L2_CID_AUDIO_MUTE:
1515 dev->mute = ctrl->value;
1516 rc = em28xx_audio_analog_set(dev);
1518 case V4L2_CID_AUDIO_VOLUME:
1519 dev->volume = ctrl->value;
1520 rc = em28xx_audio_analog_set(dev);
1523 return (rc < 0) ? rc : 0;
1526 static int vidioc_g_tuner(struct file *file, void *priv,
1527 struct v4l2_tuner *t)
1529 struct em28xx_fh *fh = priv;
1530 struct em28xx *dev = fh->dev;
1533 rc = check_dev(dev);
1540 strcpy(t->name, "Tuner");
1541 t->type = V4L2_TUNER_ANALOG_TV;
1543 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1547 static int vidioc_s_tuner(struct file *file, void *priv,
1548 struct v4l2_tuner *t)
1550 struct em28xx_fh *fh = priv;
1551 struct em28xx *dev = fh->dev;
1554 rc = check_dev(dev);
1561 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1565 static int vidioc_g_frequency(struct file *file, void *priv,
1566 struct v4l2_frequency *f)
1568 struct em28xx_fh *fh = priv;
1569 struct em28xx *dev = fh->dev;
1571 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1572 f->frequency = dev->ctl_freq;
1576 static int vidioc_s_frequency(struct file *file, void *priv,
1577 struct v4l2_frequency *f)
1579 struct em28xx_fh *fh = priv;
1580 struct em28xx *dev = fh->dev;
1583 rc = check_dev(dev);
1590 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1592 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1595 dev->ctl_freq = f->frequency;
1596 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1601 #ifdef CONFIG_VIDEO_ADV_DEBUG
1602 static int em28xx_reg_len(int reg)
1605 case EM28XX_R40_AC97LSB:
1606 case EM28XX_R30_HSCALELOW:
1607 case EM28XX_R32_VSCALELOW:
1614 static int vidioc_g_chip_ident(struct file *file, void *priv,
1615 struct v4l2_dbg_chip_ident *chip)
1617 struct em28xx_fh *fh = priv;
1618 struct em28xx *dev = fh->dev;
1620 chip->ident = V4L2_IDENT_NONE;
1623 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
1629 static int vidioc_g_register(struct file *file, void *priv,
1630 struct v4l2_dbg_register *reg)
1632 struct em28xx_fh *fh = priv;
1633 struct em28xx *dev = fh->dev;
1636 switch (reg->match.type) {
1637 case V4L2_CHIP_MATCH_AC97:
1638 ret = em28xx_read_ac97(dev, reg->reg);
1645 case V4L2_CHIP_MATCH_I2C_DRIVER:
1646 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1648 case V4L2_CHIP_MATCH_I2C_ADDR:
1649 /* TODO: is this correct? */
1650 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1653 if (!v4l2_chip_match_host(®->match))
1658 reg->size = em28xx_reg_len(reg->reg);
1659 if (reg->size == 1) {
1660 ret = em28xx_read_reg(dev, reg->reg);
1668 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1669 reg->reg, (char *)&val, 2);
1673 reg->val = le16_to_cpu(val);
1679 static int vidioc_s_register(struct file *file, void *priv,
1680 struct v4l2_dbg_register *reg)
1682 struct em28xx_fh *fh = priv;
1683 struct em28xx *dev = fh->dev;
1686 switch (reg->match.type) {
1687 case V4L2_CHIP_MATCH_AC97:
1688 return em28xx_write_ac97(dev, reg->reg, reg->val);
1689 case V4L2_CHIP_MATCH_I2C_DRIVER:
1690 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1692 case V4L2_CHIP_MATCH_I2C_ADDR:
1693 /* TODO: is this correct? */
1694 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1697 if (!v4l2_chip_match_host(®->match))
1702 buf = cpu_to_le16(reg->val);
1704 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1705 em28xx_reg_len(reg->reg));
1710 static int vidioc_cropcap(struct file *file, void *priv,
1711 struct v4l2_cropcap *cc)
1713 struct em28xx_fh *fh = priv;
1714 struct em28xx *dev = fh->dev;
1716 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1719 cc->bounds.left = 0;
1721 cc->bounds.width = dev->width;
1722 cc->bounds.height = dev->height;
1723 cc->defrect = cc->bounds;
1724 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1725 cc->pixelaspect.denominator = 59;
1730 static int vidioc_streamon(struct file *file, void *priv,
1731 enum v4l2_buf_type type)
1733 struct em28xx_fh *fh = priv;
1734 struct em28xx *dev = fh->dev;
1737 rc = check_dev(dev);
1741 if (unlikely(type != fh->type))
1744 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1745 fh, type, fh->resources, dev->resources);
1747 if (unlikely(!res_get(fh, get_ressource(fh))))
1750 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1751 rc = videobuf_streamon(&fh->vb_vidq);
1752 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1753 rc = videobuf_streamon(&fh->vb_vbiq);
1758 static int vidioc_streamoff(struct file *file, void *priv,
1759 enum v4l2_buf_type type)
1761 struct em28xx_fh *fh = priv;
1762 struct em28xx *dev = fh->dev;
1765 rc = check_dev(dev);
1769 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1770 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
1772 if (type != fh->type)
1775 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1776 fh, type, fh->resources, dev->resources);
1778 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1779 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1780 videobuf_streamoff(&fh->vb_vidq);
1781 res_free(fh, EM28XX_RESOURCE_VIDEO);
1783 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1784 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1785 videobuf_streamoff(&fh->vb_vbiq);
1786 res_free(fh, EM28XX_RESOURCE_VBI);
1793 static int vidioc_querycap(struct file *file, void *priv,
1794 struct v4l2_capability *cap)
1796 struct em28xx_fh *fh = priv;
1797 struct em28xx *dev = fh->dev;
1799 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1800 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1801 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1804 V4L2_CAP_SLICED_VBI_CAPTURE |
1805 V4L2_CAP_VIDEO_CAPTURE |
1806 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1809 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1811 if (dev->audio_mode.has_audio)
1812 cap->capabilities |= V4L2_CAP_AUDIO;
1814 if (dev->tuner_type != TUNER_ABSENT)
1815 cap->capabilities |= V4L2_CAP_TUNER;
1820 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1821 struct v4l2_fmtdesc *f)
1823 if (unlikely(f->index >= ARRAY_SIZE(format)))
1826 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1827 f->pixelformat = format[f->index].fourcc;
1832 static int vidioc_enum_framesizes(struct file *file, void *priv,
1833 struct v4l2_frmsizeenum *fsize)
1835 struct em28xx_fh *fh = priv;
1836 struct em28xx *dev = fh->dev;
1837 struct em28xx_fmt *fmt;
1838 unsigned int maxw = norm_maxw(dev);
1839 unsigned int maxh = norm_maxh(dev);
1841 fmt = format_by_fourcc(fsize->pixel_format);
1843 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1844 fsize->pixel_format);
1848 if (dev->board.is_em2800) {
1849 if (fsize->index > 1)
1851 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1852 fsize->discrete.width = maxw / (1 + fsize->index);
1853 fsize->discrete.height = maxh / (1 + fsize->index);
1857 if (fsize->index != 0)
1860 /* Report a continuous range */
1861 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1862 fsize->stepwise.min_width = 48;
1863 fsize->stepwise.min_height = 32;
1864 fsize->stepwise.max_width = maxw;
1865 fsize->stepwise.max_height = maxh;
1866 fsize->stepwise.step_width = 1;
1867 fsize->stepwise.step_height = 1;
1871 /* Sliced VBI ioctls */
1872 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1873 struct v4l2_format *f)
1875 struct em28xx_fh *fh = priv;
1876 struct em28xx *dev = fh->dev;
1879 rc = check_dev(dev);
1883 f->fmt.sliced.service_set = 0;
1884 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
1886 if (f->fmt.sliced.service_set == 0)
1892 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1893 struct v4l2_format *f)
1895 struct em28xx_fh *fh = priv;
1896 struct em28xx *dev = fh->dev;
1899 rc = check_dev(dev);
1903 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
1905 if (f->fmt.sliced.service_set == 0)
1911 /* RAW VBI ioctls */
1913 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1914 struct v4l2_format *format)
1916 struct em28xx_fh *fh = priv;
1917 struct em28xx *dev = fh->dev;
1919 format->fmt.vbi.samples_per_line = dev->vbi_width;
1920 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1921 format->fmt.vbi.offset = 0;
1922 format->fmt.vbi.flags = 0;
1923 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1924 format->fmt.vbi.count[0] = dev->vbi_height;
1925 format->fmt.vbi.count[1] = dev->vbi_height;
1927 /* Varies by video standard (NTSC, PAL, etc.) */
1928 if (dev->norm & V4L2_STD_525_60) {
1930 format->fmt.vbi.start[0] = 10;
1931 format->fmt.vbi.start[1] = 273;
1932 } else if (dev->norm & V4L2_STD_625_50) {
1934 format->fmt.vbi.start[0] = 6;
1935 format->fmt.vbi.start[1] = 318;
1941 static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1942 struct v4l2_format *format)
1944 struct em28xx_fh *fh = priv;
1945 struct em28xx *dev = fh->dev;
1947 format->fmt.vbi.samples_per_line = dev->vbi_width;
1948 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1949 format->fmt.vbi.offset = 0;
1950 format->fmt.vbi.flags = 0;
1951 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1952 format->fmt.vbi.count[0] = dev->vbi_height;
1953 format->fmt.vbi.count[1] = dev->vbi_height;
1955 /* Varies by video standard (NTSC, PAL, etc.) */
1956 if (dev->norm & V4L2_STD_525_60) {
1958 format->fmt.vbi.start[0] = 10;
1959 format->fmt.vbi.start[1] = 273;
1960 } else if (dev->norm & V4L2_STD_625_50) {
1962 format->fmt.vbi.start[0] = 6;
1963 format->fmt.vbi.start[1] = 318;
1969 static int vidioc_reqbufs(struct file *file, void *priv,
1970 struct v4l2_requestbuffers *rb)
1972 struct em28xx_fh *fh = priv;
1973 struct em28xx *dev = fh->dev;
1976 rc = check_dev(dev);
1980 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1981 return videobuf_reqbufs(&fh->vb_vidq, rb);
1983 return videobuf_reqbufs(&fh->vb_vbiq, rb);
1986 static int vidioc_querybuf(struct file *file, void *priv,
1987 struct v4l2_buffer *b)
1989 struct em28xx_fh *fh = priv;
1990 struct em28xx *dev = fh->dev;
1993 rc = check_dev(dev);
1997 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1998 return videobuf_querybuf(&fh->vb_vidq, b);
2000 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
2001 the videobuf framework, but we probably shouldn't be
2002 returning a buffer larger than that which was asked for.
2003 At a minimum, it causes a crash in zvbi since it does
2004 a memcpy based on the source buffer length */
2005 int result = videobuf_querybuf(&fh->vb_vbiq, b);
2006 b->length = dev->vbi_width * dev->vbi_height * 2;
2012 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2014 struct em28xx_fh *fh = priv;
2015 struct em28xx *dev = fh->dev;
2018 rc = check_dev(dev);
2022 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2023 return videobuf_qbuf(&fh->vb_vidq, b);
2025 return videobuf_qbuf(&fh->vb_vbiq, b);
2028 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2030 struct em28xx_fh *fh = priv;
2031 struct em28xx *dev = fh->dev;
2034 rc = check_dev(dev);
2038 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2039 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
2042 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
2046 /* ----------------------------------------------------------- */
2047 /* RADIO ESPECIFIC IOCTLS */
2048 /* ----------------------------------------------------------- */
2050 static int radio_querycap(struct file *file, void *priv,
2051 struct v4l2_capability *cap)
2053 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2055 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2056 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
2057 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
2059 cap->capabilities = V4L2_CAP_TUNER;
2063 static int radio_g_tuner(struct file *file, void *priv,
2064 struct v4l2_tuner *t)
2066 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2068 if (unlikely(t->index > 0))
2071 strcpy(t->name, "Radio");
2072 t->type = V4L2_TUNER_RADIO;
2074 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
2079 static int radio_enum_input(struct file *file, void *priv,
2080 struct v4l2_input *i)
2084 strcpy(i->name, "Radio");
2085 i->type = V4L2_INPUT_TYPE_TUNER;
2090 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2092 if (unlikely(a->index))
2095 strcpy(a->name, "Radio");
2099 static int radio_s_tuner(struct file *file, void *priv,
2100 struct v4l2_tuner *t)
2102 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2107 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
2112 static int radio_s_audio(struct file *file, void *fh,
2113 const struct v4l2_audio *a)
2118 static int radio_s_input(struct file *file, void *fh, unsigned int i)
2123 static int radio_queryctrl(struct file *file, void *priv,
2124 struct v4l2_queryctrl *qc)
2128 if (qc->id < V4L2_CID_BASE ||
2129 qc->id >= V4L2_CID_LASTP1)
2132 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2133 if (qc->id && qc->id == ac97_qctrl[i].id) {
2134 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
2143 * em28xx_v4l2_open()
2144 * inits the device and starts isoc transfer
2146 static int em28xx_v4l2_open(struct file *filp)
2148 int errCode = 0, radio = 0;
2149 struct video_device *vdev = video_devdata(filp);
2150 struct em28xx *dev = video_drvdata(filp);
2151 enum v4l2_buf_type fh_type = 0;
2152 struct em28xx_fh *fh;
2153 enum v4l2_field field;
2155 switch (vdev->vfl_type) {
2156 case VFL_TYPE_GRABBER:
2157 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2160 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2162 case VFL_TYPE_RADIO:
2167 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2168 video_device_node_name(vdev), v4l2_type_names[fh_type],
2172 if (mutex_lock_interruptible(&dev->lock))
2173 return -ERESTARTSYS;
2174 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
2176 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
2177 mutex_unlock(&dev->lock);
2183 filp->private_data = fh;
2185 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
2186 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
2187 em28xx_set_alternate(dev);
2188 em28xx_resolution_set(dev);
2190 /* Needed, since GPIO might have disabled power of
2193 em28xx_wake_i2c(dev);
2197 em28xx_videodbg("video_open: setting radio device\n");
2198 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
2203 if (dev->progressive)
2204 field = V4L2_FIELD_NONE;
2206 field = V4L2_FIELD_INTERLACED;
2208 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2210 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
2211 sizeof(struct em28xx_buffer), fh, &dev->lock);
2213 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2215 V4L2_BUF_TYPE_VBI_CAPTURE,
2217 sizeof(struct em28xx_buffer), fh, &dev->lock);
2218 mutex_unlock(&dev->lock);
2224 * em28xx_realease_resources()
2225 * unregisters the v4l2,i2c and usb devices
2226 * called when the device gets disconected or at module unload
2228 void em28xx_release_analog_resources(struct em28xx *dev)
2231 /*FIXME: I2C IR should be disconnected */
2233 if (dev->radio_dev) {
2234 if (video_is_registered(dev->radio_dev))
2235 video_unregister_device(dev->radio_dev);
2237 video_device_release(dev->radio_dev);
2238 dev->radio_dev = NULL;
2241 em28xx_info("V4L2 device %s deregistered\n",
2242 video_device_node_name(dev->vbi_dev));
2243 if (video_is_registered(dev->vbi_dev))
2244 video_unregister_device(dev->vbi_dev);
2246 video_device_release(dev->vbi_dev);
2247 dev->vbi_dev = NULL;
2250 em28xx_info("V4L2 device %s deregistered\n",
2251 video_device_node_name(dev->vdev));
2252 if (video_is_registered(dev->vdev))
2253 video_unregister_device(dev->vdev);
2255 video_device_release(dev->vdev);
2261 * em28xx_v4l2_close()
2262 * stops streaming and deallocates all resources allocated by the v4l2
2265 static int em28xx_v4l2_close(struct file *filp)
2267 struct em28xx_fh *fh = filp->private_data;
2268 struct em28xx *dev = fh->dev;
2271 em28xx_videodbg("users=%d\n", dev->users);
2273 mutex_lock(&dev->lock);
2274 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
2275 videobuf_stop(&fh->vb_vidq);
2276 res_free(fh, EM28XX_RESOURCE_VIDEO);
2279 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2280 videobuf_stop(&fh->vb_vbiq);
2281 res_free(fh, EM28XX_RESOURCE_VBI);
2284 if (dev->users == 1) {
2285 /* the device is already disconnect,
2286 free the remaining resources */
2287 if (dev->state & DEV_DISCONNECTED) {
2288 em28xx_release_resources(dev);
2289 kfree(dev->alt_max_pkt_size);
2290 mutex_unlock(&dev->lock);
2296 /* Save some power by putting tuner to sleep */
2297 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
2299 /* do this before setting alternate! */
2300 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
2301 em28xx_set_mode(dev, EM28XX_SUSPEND);
2303 /* set alternate 0 */
2305 em28xx_videodbg("setting alternate 0\n");
2306 errCode = usb_set_interface(dev->udev, 0, 0);
2308 em28xx_errdev("cannot change alternate number to "
2309 "0 (error=%i)\n", errCode);
2313 videobuf_mmap_free(&fh->vb_vidq);
2314 videobuf_mmap_free(&fh->vb_vbiq);
2317 mutex_unlock(&dev->lock);
2322 * em28xx_v4l2_read()
2323 * will allocate buffers when called for the first time
2326 em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2329 struct em28xx_fh *fh = filp->private_data;
2330 struct em28xx *dev = fh->dev;
2333 rc = check_dev(dev);
2337 if (mutex_lock_interruptible(&dev->lock))
2338 return -ERESTARTSYS;
2339 /* FIXME: read() is not prepared to allow changing the video
2340 resolution while streaming. Seems a bug at em28xx_set_fmt
2343 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2344 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
2347 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2348 filp->f_flags & O_NONBLOCK);
2349 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2350 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2353 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
2354 filp->f_flags & O_NONBLOCK);
2356 mutex_unlock(&dev->lock);
2363 * will allocate buffers when called for the first time
2365 static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
2367 struct em28xx_fh *fh = filp->private_data;
2368 struct em28xx *dev = fh->dev;
2371 rc = check_dev(dev);
2375 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2376 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2378 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2379 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2380 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2382 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
2388 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2390 struct em28xx_fh *fh = filp->private_data;
2391 struct em28xx *dev = fh->dev;
2394 mutex_lock(&dev->lock);
2395 res = em28xx_poll(filp, wait);
2396 mutex_unlock(&dev->lock);
2401 * em28xx_v4l2_mmap()
2403 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2405 struct em28xx_fh *fh = filp->private_data;
2406 struct em28xx *dev = fh->dev;
2409 rc = check_dev(dev);
2413 if (mutex_lock_interruptible(&dev->lock))
2414 return -ERESTARTSYS;
2415 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2416 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2417 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2418 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
2419 mutex_unlock(&dev->lock);
2421 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2422 (unsigned long)vma->vm_start,
2423 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2429 static const struct v4l2_file_operations em28xx_v4l_fops = {
2430 .owner = THIS_MODULE,
2431 .open = em28xx_v4l2_open,
2432 .release = em28xx_v4l2_close,
2433 .read = em28xx_v4l2_read,
2434 .poll = em28xx_v4l2_poll,
2435 .mmap = em28xx_v4l2_mmap,
2436 .unlocked_ioctl = video_ioctl2,
2439 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2440 .vidioc_querycap = vidioc_querycap,
2441 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2442 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2443 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2444 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2445 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2446 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
2447 .vidioc_enum_framesizes = vidioc_enum_framesizes,
2448 .vidioc_g_audio = vidioc_g_audio,
2449 .vidioc_s_audio = vidioc_s_audio,
2450 .vidioc_cropcap = vidioc_cropcap,
2451 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2452 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2453 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2455 .vidioc_reqbufs = vidioc_reqbufs,
2456 .vidioc_querybuf = vidioc_querybuf,
2457 .vidioc_qbuf = vidioc_qbuf,
2458 .vidioc_dqbuf = vidioc_dqbuf,
2459 .vidioc_g_std = vidioc_g_std,
2460 .vidioc_querystd = vidioc_querystd,
2461 .vidioc_s_std = vidioc_s_std,
2462 .vidioc_g_parm = vidioc_g_parm,
2463 .vidioc_s_parm = vidioc_s_parm,
2464 .vidioc_enum_input = vidioc_enum_input,
2465 .vidioc_g_input = vidioc_g_input,
2466 .vidioc_s_input = vidioc_s_input,
2467 .vidioc_queryctrl = vidioc_queryctrl,
2468 .vidioc_g_ctrl = vidioc_g_ctrl,
2469 .vidioc_s_ctrl = vidioc_s_ctrl,
2470 .vidioc_streamon = vidioc_streamon,
2471 .vidioc_streamoff = vidioc_streamoff,
2472 .vidioc_g_tuner = vidioc_g_tuner,
2473 .vidioc_s_tuner = vidioc_s_tuner,
2474 .vidioc_g_frequency = vidioc_g_frequency,
2475 .vidioc_s_frequency = vidioc_s_frequency,
2476 #ifdef CONFIG_VIDEO_ADV_DEBUG
2477 .vidioc_g_register = vidioc_g_register,
2478 .vidioc_s_register = vidioc_s_register,
2479 .vidioc_g_chip_ident = vidioc_g_chip_ident,
2483 static const struct video_device em28xx_video_template = {
2484 .fops = &em28xx_v4l_fops,
2485 .release = video_device_release,
2486 .ioctl_ops = &video_ioctl_ops,
2488 .tvnorms = V4L2_STD_ALL,
2489 .current_norm = V4L2_STD_PAL,
2492 static const struct v4l2_file_operations radio_fops = {
2493 .owner = THIS_MODULE,
2494 .open = em28xx_v4l2_open,
2495 .release = em28xx_v4l2_close,
2496 .unlocked_ioctl = video_ioctl2,
2499 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2500 .vidioc_querycap = radio_querycap,
2501 .vidioc_g_tuner = radio_g_tuner,
2502 .vidioc_enum_input = radio_enum_input,
2503 .vidioc_g_audio = radio_g_audio,
2504 .vidioc_s_tuner = radio_s_tuner,
2505 .vidioc_s_audio = radio_s_audio,
2506 .vidioc_s_input = radio_s_input,
2507 .vidioc_queryctrl = radio_queryctrl,
2508 .vidioc_g_ctrl = vidioc_g_ctrl,
2509 .vidioc_s_ctrl = vidioc_s_ctrl,
2510 .vidioc_g_frequency = vidioc_g_frequency,
2511 .vidioc_s_frequency = vidioc_s_frequency,
2512 #ifdef CONFIG_VIDEO_ADV_DEBUG
2513 .vidioc_g_register = vidioc_g_register,
2514 .vidioc_s_register = vidioc_s_register,
2518 static struct video_device em28xx_radio_template = {
2519 .name = "em28xx-radio",
2520 .fops = &radio_fops,
2521 .ioctl_ops = &radio_ioctl_ops,
2524 /******************************** usb interface ******************************/
2528 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2529 const struct video_device *template,
2530 const char *type_name)
2532 struct video_device *vfd;
2534 vfd = video_device_alloc();
2539 vfd->v4l2_dev = &dev->v4l2_dev;
2540 vfd->release = video_device_release;
2541 vfd->debug = video_debug;
2542 vfd->lock = &dev->lock;
2544 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2545 dev->name, type_name);
2547 video_set_drvdata(vfd, dev);
2551 int em28xx_register_analog_devices(struct em28xx *dev)
2557 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2558 dev->name, EM28XX_VERSION);
2560 /* set default norm */
2561 dev->norm = em28xx_video_template.current_norm;
2562 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
2563 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
2565 /* Analog specific initialization */
2566 dev->format = &format[0];
2568 maxw = norm_maxw(dev);
2569 /* MaxPacketSize for em2800 is too small to capture at full resolution
2570 * use half of maxw as the scaler can only scale to 50% */
2571 if (dev->board.is_em2800)
2574 em28xx_set_video_format(dev, format[0].fourcc,
2575 maxw, norm_maxh(dev));
2579 /* Audio defaults */
2583 /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
2584 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2585 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2586 (EM28XX_XCLK_AUDIO_UNMUTE | val));
2588 em28xx_set_outfmt(dev);
2589 em28xx_colorlevels_set_default(dev);
2590 em28xx_compression_disable(dev);
2592 /* allocate and fill video video_device struct */
2593 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2595 em28xx_errdev("cannot allocate video_device.\n");
2599 /* register v4l2 video video_device */
2600 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2601 video_nr[dev->devno]);
2603 em28xx_errdev("unable to register video device (error=%i).\n",
2608 /* Allocate and fill vbi video_device struct */
2609 if (em28xx_vbi_supported(dev) == 1) {
2610 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2613 /* register v4l2 vbi video_device */
2614 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2615 vbi_nr[dev->devno]);
2617 em28xx_errdev("unable to register vbi device\n");
2622 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2623 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2625 if (!dev->radio_dev) {
2626 em28xx_errdev("cannot allocate video_device.\n");
2629 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2630 radio_nr[dev->devno]);
2632 em28xx_errdev("can't register radio device\n");
2635 em28xx_info("Registered radio device as %s\n",
2636 video_device_node_name(dev->radio_dev));
2639 em28xx_info("V4L2 video device registered as %s\n",
2640 video_device_node_name(dev->vdev));
2643 em28xx_info("V4L2 VBI device registered as %s\n",
2644 video_device_node_name(dev->vbi_dev));