2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
5 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
7 Mauro Carvalho Chehab <mchehab@infradead.org>
8 Sascha Sommer <saschasommer@freenet.de>
10 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/bitmap.h>
33 #include <linux/usb.h>
34 #include <linux/i2c.h>
35 #include <linux/version.h>
37 #include <linux/mutex.h>
40 #include <media/v4l2-common.h>
41 #include <media/v4l2-ioctl.h>
42 #include <media/v4l2-chip-ident.h>
43 #include <media/msp3400.h>
44 #include <media/tuner.h>
46 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
47 "Markus Rechberger <mrechberger@gmail.com>, " \
48 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
49 "Sascha Sommer <saschasommer@freenet.de>"
51 #define DRIVER_DESC "Empia em28xx based USB video device driver"
52 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
54 #define em28xx_videodbg(fmt, arg...) do {\
56 printk(KERN_INFO "%s %s :"fmt, \
57 dev->name, __func__ , ##arg); } while (0)
59 static unsigned int isoc_debug;
60 module_param(isoc_debug, int, 0644);
61 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
63 #define em28xx_isocdbg(fmt, arg...) \
66 printk(KERN_INFO "%s %s :"fmt, \
67 dev->name, __func__ , ##arg); \
71 MODULE_AUTHOR(DRIVER_AUTHOR);
72 MODULE_DESCRIPTION(DRIVER_DESC);
73 MODULE_LICENSE("GPL");
75 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
76 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
77 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79 module_param_array(video_nr, int, NULL, 0444);
80 module_param_array(vbi_nr, int, NULL, 0444);
81 module_param_array(radio_nr, int, NULL, 0444);
82 MODULE_PARM_DESC(video_nr, "video device numbers");
83 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
84 MODULE_PARM_DESC(radio_nr, "radio device numbers");
86 static unsigned int video_debug;
87 module_param(video_debug, int, 0644);
88 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
90 /* supported video standards */
91 static struct em28xx_fmt format[] = {
93 .name = "16 bpp YUY2, 4:2:2, packed",
94 .fourcc = V4L2_PIX_FMT_YUYV,
96 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
98 .name = "16 bpp RGB 565, LE",
99 .fourcc = V4L2_PIX_FMT_RGB565,
101 .reg = EM28XX_OUTFMT_RGB_16_656,
103 .name = "8 bpp Bayer BGBG..GRGR",
104 .fourcc = V4L2_PIX_FMT_SBGGR8,
106 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
108 .name = "8 bpp Bayer GRGR..BGBG",
109 .fourcc = V4L2_PIX_FMT_SGRBG8,
111 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
113 .name = "8 bpp Bayer GBGB..RGRG",
114 .fourcc = V4L2_PIX_FMT_SGBRG8,
116 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
118 .name = "12 bpp YUV411",
119 .fourcc = V4L2_PIX_FMT_YUV411P,
121 .reg = EM28XX_OUTFMT_YUV411,
125 /* supported controls */
126 /* Common to all boards */
127 static struct v4l2_queryctrl ac97_qctrl[] = {
129 .id = V4L2_CID_AUDIO_VOLUME,
130 .type = V4L2_CTRL_TYPE_INTEGER,
135 .default_value = 0x1f,
136 .flags = V4L2_CTRL_FLAG_SLIDER,
138 .id = V4L2_CID_AUDIO_MUTE,
139 .type = V4L2_CTRL_TYPE_BOOLEAN,
149 /* ------------------------------------------------------------------
150 DMA and thread functions
151 ------------------------------------------------------------------*/
154 * Announces that a buffer were filled and request the next
156 static inline void buffer_filled(struct em28xx *dev,
157 struct em28xx_dmaqueue *dma_q,
158 struct em28xx_buffer *buf)
160 /* Advice that buffer was filled */
161 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
162 buf->vb.state = VIDEOBUF_DONE;
163 buf->vb.field_count++;
164 do_gettimeofday(&buf->vb.ts);
166 dev->isoc_ctl.vid_buf = NULL;
168 list_del(&buf->vb.queue);
169 wake_up(&buf->vb.done);
172 static inline void vbi_buffer_filled(struct em28xx *dev,
173 struct em28xx_dmaqueue *dma_q,
174 struct em28xx_buffer *buf)
176 /* Advice that buffer was filled */
177 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
179 buf->vb.state = VIDEOBUF_DONE;
180 buf->vb.field_count++;
181 do_gettimeofday(&buf->vb.ts);
183 dev->isoc_ctl.vbi_buf = NULL;
185 list_del(&buf->vb.queue);
186 wake_up(&buf->vb.done);
190 * Identify the buffer header type and properly handles
192 static void em28xx_copy_video(struct em28xx *dev,
193 struct em28xx_dmaqueue *dma_q,
194 struct em28xx_buffer *buf,
196 unsigned char *outp, unsigned long len)
198 void *fieldstart, *startwrite, *startread;
199 int linesdone, currlinedone, offset, lencopy, remain;
200 int bytesperline = dev->width << 1;
202 if (dma_q->pos + len > buf->vb.size)
203 len = buf->vb.size - dma_q->pos;
205 if (p[0] != 0x88 && p[0] != 0x22) {
206 em28xx_isocdbg("frame is not complete\n");
214 if (dev->progressive)
217 /* Interlaces two half frames */
221 fieldstart = outp + bytesperline;
224 linesdone = dma_q->pos / bytesperline;
225 currlinedone = dma_q->pos % bytesperline;
227 if (dev->progressive)
228 offset = linesdone * bytesperline + currlinedone;
230 offset = linesdone * bytesperline * 2 + currlinedone;
232 startwrite = fieldstart + offset;
233 lencopy = bytesperline - currlinedone;
234 lencopy = lencopy > remain ? remain : lencopy;
236 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
237 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
238 ((char *)startwrite + lencopy) -
239 ((char *)outp + buf->vb.size));
240 remain = (char *)outp + buf->vb.size - (char *)startwrite;
245 memcpy(startwrite, startread, lencopy);
250 startwrite += lencopy + bytesperline;
251 startread += lencopy;
252 if (bytesperline > remain)
255 lencopy = bytesperline;
257 if ((char *)startwrite + lencopy > (char *)outp +
259 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
260 ((char *)startwrite + lencopy) -
261 ((char *)outp + buf->vb.size));
262 lencopy = remain = (char *)outp + buf->vb.size -
268 memcpy(startwrite, startread, lencopy);
276 static void em28xx_copy_vbi(struct em28xx *dev,
277 struct em28xx_dmaqueue *dma_q,
278 struct em28xx_buffer *buf,
280 unsigned char *outp, unsigned long len)
282 void *startwrite, *startread;
284 int bytesperline = 720;
287 printk("dev is null\n");
292 printk("dma_q is null\n");
299 printk("p is null\n");
303 printk("outp is null\n");
307 if (dma_q->pos + len > buf->vb.size)
308 len = buf->vb.size - dma_q->pos;
310 if ((p[0] == 0x33 && p[1] == 0x95) ||
311 (p[0] == 0x88 && p[1] == 0x88)) {
312 /* Header field, advance past it */
320 startwrite = outp + dma_q->pos;
323 /* Make sure the bottom field populates the second half of the frame */
324 if (buf->top_field == 0) {
325 startwrite += bytesperline * 0x0c;
326 offset += bytesperline * 0x0c;
329 memcpy(startwrite, startread, len);
333 static inline void print_err_status(struct em28xx *dev,
334 int packet, int status)
336 char *errmsg = "Unknown";
340 errmsg = "unlinked synchronuously";
343 errmsg = "unlinked asynchronuously";
346 errmsg = "Buffer error (overrun)";
349 errmsg = "Stalled (device not responding)";
352 errmsg = "Babble (bad cable?)";
355 errmsg = "Bit-stuff error (bad cable?)";
358 errmsg = "CRC/Timeout (could be anything)";
361 errmsg = "Device does not respond";
365 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
367 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
368 packet, status, errmsg);
373 * video-buf generic routine to get the next available buffer
375 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
376 struct em28xx_buffer **buf)
378 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
381 if (list_empty(&dma_q->active)) {
382 em28xx_isocdbg("No active queue to serve\n");
383 dev->isoc_ctl.vid_buf = NULL;
388 /* Get the next buffer */
389 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
391 /* Cleans up buffer - Usefull for testing for frame/URB loss */
392 outp = videobuf_to_vmalloc(&(*buf)->vb);
393 memset(outp, 0, (*buf)->vb.size);
395 dev->isoc_ctl.vid_buf = *buf;
401 * video-buf generic routine to get the next available VBI buffer
403 static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
404 struct em28xx_buffer **buf)
406 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
409 if (list_empty(&dma_q->active)) {
410 em28xx_isocdbg("No active queue to serve\n");
411 dev->isoc_ctl.vbi_buf = NULL;
416 /* Get the next buffer */
417 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
418 /* Cleans up buffer - Usefull for testing for frame/URB loss */
419 outp = videobuf_to_vmalloc(&(*buf)->vb);
420 memset(outp, 0x00, (*buf)->vb.size);
422 dev->isoc_ctl.vbi_buf = *buf;
428 * Controls the isoc copy of each urb packet
430 static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
432 struct em28xx_buffer *buf;
433 struct em28xx_dmaqueue *dma_q = &dev->vidq;
434 unsigned char *outp = NULL;
435 int i, len = 0, rc = 1;
441 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
444 if (urb->status < 0) {
445 print_err_status(dev, -1, urb->status);
446 if (urb->status == -ENOENT)
450 buf = dev->isoc_ctl.vid_buf;
452 outp = videobuf_to_vmalloc(&buf->vb);
454 for (i = 0; i < urb->number_of_packets; i++) {
455 int status = urb->iso_frame_desc[i].status;
458 print_err_status(dev, i, status);
459 if (urb->iso_frame_desc[i].status != -EPROTO)
463 len = urb->iso_frame_desc[i].actual_length - 4;
465 if (urb->iso_frame_desc[i].actual_length <= 0) {
466 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
469 if (urb->iso_frame_desc[i].actual_length >
471 em28xx_isocdbg("packet bigger than packet size");
475 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
477 /* FIXME: incomplete buffer checks where removed to make
478 logic simpler. Impacts of those changes should be evaluated
480 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
481 em28xx_isocdbg("VBI HEADER!!!\n");
482 /* FIXME: Should add vbi copy */
485 if (p[0] == 0x22 && p[1] == 0x5a) {
486 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
487 len, (p[2] & 1) ? "odd" : "even");
489 if (dev->progressive || !(p[2] & 1)) {
491 buffer_filled(dev, dma_q, buf);
492 get_next_buf(dma_q, &buf);
496 outp = videobuf_to_vmalloc(&buf->vb);
509 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
514 /* Version of isoc handler that takes into account a mixture of video and
516 static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
518 struct em28xx_buffer *buf, *vbi_buf;
519 struct em28xx_dmaqueue *dma_q = &dev->vidq;
520 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
521 unsigned char *outp = NULL;
522 unsigned char *vbioutp = NULL;
523 int i, len = 0, rc = 1;
530 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
533 if (urb->status < 0) {
534 print_err_status(dev, -1, urb->status);
535 if (urb->status == -ENOENT)
539 buf = dev->isoc_ctl.vid_buf;
541 outp = videobuf_to_vmalloc(&buf->vb);
543 vbi_buf = dev->isoc_ctl.vbi_buf;
545 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
547 for (i = 0; i < urb->number_of_packets; i++) {
548 int status = urb->iso_frame_desc[i].status;
551 print_err_status(dev, i, status);
552 if (urb->iso_frame_desc[i].status != -EPROTO)
556 len = urb->iso_frame_desc[i].actual_length - 4;
558 if (urb->iso_frame_desc[i].actual_length <= 0) {
559 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
562 if (urb->iso_frame_desc[i].actual_length >
564 em28xx_isocdbg("packet bigger than packet size");
568 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
570 /* capture type 0 = vbi start
571 capture type 1 = video start
572 capture type 2 = video in progress */
573 if (p[0] == 0x33 && p[1] == 0x95) {
574 dev->capture_type = 0;
576 em28xx_isocdbg("VBI START HEADER!!!\n");
577 dev->cur_field = p[2];
580 /* FIXME: get rid of hard-coded value */
581 vbi_size = 720 * 0x0c;
583 if (dev->capture_type == 0) {
584 if (dev->vbi_read >= vbi_size) {
585 /* We've already read all the VBI data, so
586 treat the rest as video */
587 printk("djh c should never happen\n");
588 } else if ((dev->vbi_read + len) < vbi_size) {
589 /* This entire frame is VBI data */
590 if (dev->vbi_read == 0 &&
591 (!(dev->cur_field & 1))) {
592 /* Brand new frame */
594 vbi_buffer_filled(dev,
597 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
601 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
605 if (dev->vbi_read == 0) {
607 if (vbi_buf != NULL) {
608 if (dev->cur_field & 1)
609 vbi_buf->top_field = 0;
611 vbi_buf->top_field = 1;
615 dev->vbi_read += len;
616 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
619 /* Some of this frame is VBI data and some is
621 int vbi_data_len = vbi_size - dev->vbi_read;
622 dev->vbi_read += vbi_data_len;
623 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
624 vbioutp, vbi_data_len);
625 dev->capture_type = 1;
631 if (dev->capture_type == 1) {
632 dev->capture_type = 2;
633 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
634 len, (p[2] & 1) ? "odd" : "even");
636 if (dev->progressive || !(dev->cur_field & 1)) {
638 buffer_filled(dev, dma_q, buf);
639 get_next_buf(dma_q, &buf);
643 outp = videobuf_to_vmalloc(&buf->vb);
646 if (dev->cur_field & 1)
654 if (buf != NULL && dev->capture_type == 2)
655 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
661 /* ------------------------------------------------------------------
663 ------------------------------------------------------------------*/
666 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
668 struct em28xx_fh *fh = vq->priv_data;
669 struct em28xx *dev = fh->dev;
670 struct v4l2_frequency f;
672 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
675 *count = EM28XX_DEF_BUF;
677 if (*count < EM28XX_MIN_BUF)
678 *count = EM28XX_MIN_BUF;
680 /* Ask tuner to go to analog or radio mode */
681 memset(&f, 0, sizeof(f));
682 f.frequency = dev->ctl_freq;
683 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
685 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
690 /* This is called *without* dev->slock held; please keep it that way */
691 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
693 struct em28xx_fh *fh = vq->priv_data;
694 struct em28xx *dev = fh->dev;
695 unsigned long flags = 0;
699 /* We used to wait for the buffer to finish here, but this didn't work
700 because, as we were keeping the state as VIDEOBUF_QUEUED,
701 videobuf_queue_cancel marked it as finished for us.
702 (Also, it could wedge forever if the hardware was misconfigured.)
704 This should be safe; by the time we get here, the buffer isn't
705 queued anymore. If we ever start marking the buffers as
706 VIDEOBUF_ACTIVE, it won't be, though.
708 spin_lock_irqsave(&dev->slock, flags);
709 if (dev->isoc_ctl.vid_buf == buf)
710 dev->isoc_ctl.vid_buf = NULL;
711 spin_unlock_irqrestore(&dev->slock, flags);
713 videobuf_vmalloc_free(&buf->vb);
714 buf->vb.state = VIDEOBUF_NEEDS_INIT;
718 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
719 enum v4l2_field field)
721 struct em28xx_fh *fh = vq->priv_data;
722 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
723 struct em28xx *dev = fh->dev;
724 int rc = 0, urb_init = 0;
726 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
728 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
731 buf->vb.width = dev->width;
732 buf->vb.height = dev->height;
733 buf->vb.field = field;
735 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
736 rc = videobuf_iolock(vq, &buf->vb, NULL);
741 if (!dev->isoc_ctl.num_bufs)
745 if (em28xx_vbi_supported(dev) == 1)
746 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
749 em28xx_isoc_copy_vbi);
751 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
759 buf->vb.state = VIDEOBUF_PREPARED;
763 free_buffer(vq, buf);
768 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
770 struct em28xx_buffer *buf = container_of(vb,
771 struct em28xx_buffer,
773 struct em28xx_fh *fh = vq->priv_data;
774 struct em28xx *dev = fh->dev;
775 struct em28xx_dmaqueue *vidq = &dev->vidq;
777 buf->vb.state = VIDEOBUF_QUEUED;
778 list_add_tail(&buf->vb.queue, &vidq->active);
782 static void buffer_release(struct videobuf_queue *vq,
783 struct videobuf_buffer *vb)
785 struct em28xx_buffer *buf = container_of(vb,
786 struct em28xx_buffer,
788 struct em28xx_fh *fh = vq->priv_data;
789 struct em28xx *dev = (struct em28xx *)fh->dev;
791 em28xx_isocdbg("em28xx: called buffer_release\n");
793 free_buffer(vq, buf);
796 static struct videobuf_queue_ops em28xx_video_qops = {
797 .buf_setup = buffer_setup,
798 .buf_prepare = buffer_prepare,
799 .buf_queue = buffer_queue,
800 .buf_release = buffer_release,
803 /********************* v4l2 interface **************************************/
805 static void video_mux(struct em28xx *dev, int index)
807 dev->ctl_input = index;
808 dev->ctl_ainput = INPUT(index)->amux;
809 dev->ctl_aoutput = INPUT(index)->aout;
811 if (!dev->ctl_aoutput)
812 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
814 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
815 INPUT(index)->vmux, 0, 0);
817 if (dev->board.has_msp34xx) {
818 if (dev->i2s_speed) {
819 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
820 s_i2s_clock_freq, dev->i2s_speed);
822 /* Note: this is msp3400 specific */
823 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
824 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
827 if (dev->board.adecoder != EM28XX_NOADECODER) {
828 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
829 dev->ctl_ainput, dev->ctl_aoutput, 0);
832 em28xx_audio_analog_set(dev);
835 /* Usage lock check functions */
836 static int res_get(struct em28xx_fh *fh, unsigned int bit)
838 struct em28xx *dev = fh->dev;
840 if (fh->resources & bit)
841 /* have it already allocated */
845 mutex_lock(&dev->lock);
846 if (dev->resources & bit) {
847 /* no, someone else uses it */
848 mutex_unlock(&dev->lock);
851 /* it's free, grab it */
852 fh->resources |= bit;
853 dev->resources |= bit;
854 em28xx_videodbg("res: get %d\n", bit);
855 mutex_unlock(&dev->lock);
859 static int res_check(struct em28xx_fh *fh, unsigned int bit)
861 return (fh->resources & bit);
864 static int res_locked(struct em28xx *dev, unsigned int bit)
866 return (dev->resources & bit);
869 static void res_free(struct em28xx_fh *fh, unsigned int bits)
871 struct em28xx *dev = fh->dev;
873 BUG_ON((fh->resources & bits) != bits);
875 mutex_lock(&dev->lock);
876 fh->resources &= ~bits;
877 dev->resources &= ~bits;
878 em28xx_videodbg("res: put %d\n", bits);
879 mutex_unlock(&dev->lock);
882 static int get_ressource(struct em28xx_fh *fh)
885 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
886 return EM28XX_RESOURCE_VIDEO;
887 case V4L2_BUF_TYPE_VBI_CAPTURE:
888 return EM28XX_RESOURCE_VBI;
897 * return the ac97 supported controls
899 static int ac97_queryctrl(struct v4l2_queryctrl *qc)
903 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
904 if (qc->id && qc->id == ac97_qctrl[i].id) {
905 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
910 /* Control is not ac97 related */
916 * return the current values for ac97 mute and volume
918 static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
921 case V4L2_CID_AUDIO_MUTE:
922 ctrl->value = dev->mute;
924 case V4L2_CID_AUDIO_VOLUME:
925 ctrl->value = dev->volume;
928 /* Control is not ac97 related */
935 * set values for ac97 mute and volume
937 static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
941 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
942 if (ctrl->id == ac97_qctrl[i].id)
945 /* Announce that hasn't handle it */
949 if (ctrl->value < ac97_qctrl[i].minimum ||
950 ctrl->value > ac97_qctrl[i].maximum)
954 case V4L2_CID_AUDIO_MUTE:
955 dev->mute = ctrl->value;
957 case V4L2_CID_AUDIO_VOLUME:
958 dev->volume = ctrl->value;
962 return em28xx_audio_analog_set(dev);
965 static int check_dev(struct em28xx *dev)
967 if (dev->state & DEV_DISCONNECTED) {
968 em28xx_errdev("v4l2 ioctl: device not present\n");
972 if (dev->state & DEV_MISCONFIGURED) {
973 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
974 "close and open it again\n");
980 static void get_scale(struct em28xx *dev,
981 unsigned int width, unsigned int height,
982 unsigned int *hscale, unsigned int *vscale)
984 unsigned int maxw = norm_maxw(dev);
985 unsigned int maxh = norm_maxh(dev);
987 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
988 if (*hscale >= 0x4000)
991 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
992 if (*vscale >= 0x4000)
996 /* ------------------------------------------------------------------
997 IOCTL vidioc handling
998 ------------------------------------------------------------------*/
1000 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1001 struct v4l2_format *f)
1003 struct em28xx_fh *fh = priv;
1004 struct em28xx *dev = fh->dev;
1006 mutex_lock(&dev->lock);
1008 f->fmt.pix.width = dev->width;
1009 f->fmt.pix.height = dev->height;
1010 f->fmt.pix.pixelformat = dev->format->fourcc;
1011 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
1012 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
1013 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1015 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1016 if (dev->progressive)
1017 f->fmt.pix.field = V4L2_FIELD_NONE;
1019 f->fmt.pix.field = dev->interlaced ?
1020 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1022 mutex_unlock(&dev->lock);
1026 static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1030 for (i = 0; i < ARRAY_SIZE(format); i++)
1031 if (format[i].fourcc == fourcc)
1037 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1038 struct v4l2_format *f)
1040 struct em28xx_fh *fh = priv;
1041 struct em28xx *dev = fh->dev;
1042 unsigned int width = f->fmt.pix.width;
1043 unsigned int height = f->fmt.pix.height;
1044 unsigned int maxw = norm_maxw(dev);
1045 unsigned int maxh = norm_maxh(dev);
1046 unsigned int hscale, vscale;
1047 struct em28xx_fmt *fmt;
1049 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1051 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1052 f->fmt.pix.pixelformat);
1056 if (dev->board.is_em2800) {
1057 /* the em2800 can only scale down to 50% */
1058 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1059 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
1060 /* According to empiatech support the MaxPacketSize is too small
1061 * to support framesizes larger than 640x480 @ 30 fps or 640x576
1062 * @ 25 fps. As this would cut of a part of the image we prefer
1063 * 360x576 or 360x480 for now */
1064 if (width == maxw && height == maxh)
1067 /* width must even because of the YUYV format
1068 height must be even because of interlacing */
1069 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
1072 get_scale(dev, width, height, &hscale, &vscale);
1074 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1075 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1077 f->fmt.pix.width = width;
1078 f->fmt.pix.height = height;
1079 f->fmt.pix.pixelformat = fmt->fourcc;
1080 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1081 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1082 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1083 if (dev->progressive)
1084 f->fmt.pix.field = V4L2_FIELD_NONE;
1086 f->fmt.pix.field = dev->interlaced ?
1087 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1092 static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1093 unsigned width, unsigned height)
1095 struct em28xx_fmt *fmt;
1097 fmt = format_by_fourcc(fourcc);
1103 dev->height = height;
1105 /* set new image size */
1106 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1108 em28xx_set_alternate(dev);
1109 em28xx_resolution_set(dev);
1114 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1115 struct v4l2_format *f)
1117 struct em28xx_fh *fh = priv;
1118 struct em28xx *dev = fh->dev;
1121 rc = check_dev(dev);
1125 mutex_lock(&dev->lock);
1127 vidioc_try_fmt_vid_cap(file, priv, f);
1129 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1130 em28xx_errdev("%s queue busy\n", __func__);
1135 rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
1136 f->fmt.pix.width, f->fmt.pix.height);
1139 mutex_unlock(&dev->lock);
1143 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
1145 struct em28xx_fh *fh = priv;
1146 struct em28xx *dev = fh->dev;
1147 struct v4l2_format f;
1150 rc = check_dev(dev);
1154 mutex_lock(&dev->lock);
1157 /* Adjusts width/height, if needed */
1158 f.fmt.pix.width = dev->width;
1159 f.fmt.pix.height = dev->height;
1160 vidioc_try_fmt_vid_cap(file, priv, &f);
1162 /* set new image size */
1163 dev->width = f.fmt.pix.width;
1164 dev->height = f.fmt.pix.height;
1165 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1167 em28xx_resolution_set(dev);
1168 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
1170 mutex_unlock(&dev->lock);
1174 static int vidioc_g_parm(struct file *file, void *priv,
1175 struct v4l2_streamparm *p)
1177 struct em28xx_fh *fh = priv;
1178 struct em28xx *dev = fh->dev;
1181 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1184 if (dev->board.is_webcam)
1185 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1188 v4l2_video_std_frame_period(dev->norm,
1189 &p->parm.capture.timeperframe);
1194 static int vidioc_s_parm(struct file *file, void *priv,
1195 struct v4l2_streamparm *p)
1197 struct em28xx_fh *fh = priv;
1198 struct em28xx *dev = fh->dev;
1200 if (!dev->board.is_webcam)
1203 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1206 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1209 static const char *iname[] = {
1210 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1211 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1212 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1213 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1214 [EM28XX_VMUX_SVIDEO] = "S-Video",
1215 [EM28XX_VMUX_TELEVISION] = "Television",
1216 [EM28XX_VMUX_CABLE] = "Cable TV",
1217 [EM28XX_VMUX_DVB] = "DVB",
1218 [EM28XX_VMUX_DEBUG] = "for debug only",
1221 static int vidioc_enum_input(struct file *file, void *priv,
1222 struct v4l2_input *i)
1224 struct em28xx_fh *fh = priv;
1225 struct em28xx *dev = fh->dev;
1229 if (n >= MAX_EM28XX_INPUT)
1231 if (0 == INPUT(n)->type)
1235 i->type = V4L2_INPUT_TYPE_CAMERA;
1237 strcpy(i->name, iname[INPUT(n)->type]);
1239 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1240 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1241 i->type = V4L2_INPUT_TYPE_TUNER;
1243 i->std = dev->vdev->tvnorms;
1248 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1250 struct em28xx_fh *fh = priv;
1251 struct em28xx *dev = fh->dev;
1253 *i = dev->ctl_input;
1258 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1260 struct em28xx_fh *fh = priv;
1261 struct em28xx *dev = fh->dev;
1264 rc = check_dev(dev);
1268 if (i >= MAX_EM28XX_INPUT)
1270 if (0 == INPUT(i)->type)
1275 mutex_lock(&dev->lock);
1276 video_mux(dev, dev->ctl_input);
1277 mutex_unlock(&dev->lock);
1281 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1283 struct em28xx_fh *fh = priv;
1284 struct em28xx *dev = fh->dev;
1286 if (!dev->audio_mode.has_audio)
1290 case EM28XX_AMUX_VIDEO:
1291 strcpy(a->name, "Television");
1293 case EM28XX_AMUX_LINE_IN:
1294 strcpy(a->name, "Line In");
1296 case EM28XX_AMUX_VIDEO2:
1297 strcpy(a->name, "Television alt");
1299 case EM28XX_AMUX_PHONE:
1300 strcpy(a->name, "Phone");
1302 case EM28XX_AMUX_MIC:
1303 strcpy(a->name, "Mic");
1305 case EM28XX_AMUX_CD:
1306 strcpy(a->name, "CD");
1308 case EM28XX_AMUX_AUX:
1309 strcpy(a->name, "Aux");
1311 case EM28XX_AMUX_PCM_OUT:
1312 strcpy(a->name, "PCM");
1318 a->index = dev->ctl_ainput;
1319 a->capability = V4L2_AUDCAP_STEREO;
1324 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1326 struct em28xx_fh *fh = priv;
1327 struct em28xx *dev = fh->dev;
1330 if (!dev->audio_mode.has_audio)
1333 if (a->index >= MAX_EM28XX_INPUT)
1335 if (0 == INPUT(a->index)->type)
1338 mutex_lock(&dev->lock);
1340 dev->ctl_ainput = INPUT(a->index)->amux;
1341 dev->ctl_aoutput = INPUT(a->index)->aout;
1343 if (!dev->ctl_aoutput)
1344 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
1346 mutex_unlock(&dev->lock);
1350 static int vidioc_queryctrl(struct file *file, void *priv,
1351 struct v4l2_queryctrl *qc)
1353 struct em28xx_fh *fh = priv;
1354 struct em28xx *dev = fh->dev;
1358 rc = check_dev(dev);
1362 memset(qc, 0, sizeof(*qc));
1366 /* enumberate AC97 controls */
1367 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1368 rc = ac97_queryctrl(qc);
1373 /* enumberate V4L2 device controls */
1374 mutex_lock(&dev->lock);
1375 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
1376 mutex_unlock(&dev->lock);
1384 static int vidioc_g_ctrl(struct file *file, void *priv,
1385 struct v4l2_control *ctrl)
1387 struct em28xx_fh *fh = priv;
1388 struct em28xx *dev = fh->dev;
1391 rc = check_dev(dev);
1396 mutex_lock(&dev->lock);
1398 /* Set an AC97 control */
1399 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1400 rc = ac97_get_ctrl(dev, ctrl);
1404 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1406 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1410 mutex_unlock(&dev->lock);
1414 static int vidioc_s_ctrl(struct file *file, void *priv,
1415 struct v4l2_control *ctrl)
1417 struct em28xx_fh *fh = priv;
1418 struct em28xx *dev = fh->dev;
1421 rc = check_dev(dev);
1425 mutex_lock(&dev->lock);
1427 /* Set an AC97 control */
1428 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1429 rc = ac97_set_ctrl(dev, ctrl);
1433 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
1435 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
1438 * In the case of non-AC97 volume controls, we still need
1439 * to do some setups at em28xx, in order to mute/unmute
1440 * and to adjust audio volume. However, the value ranges
1441 * should be checked by the corresponding V4L subdriver.
1444 case V4L2_CID_AUDIO_MUTE:
1445 dev->mute = ctrl->value;
1446 rc = em28xx_audio_analog_set(dev);
1448 case V4L2_CID_AUDIO_VOLUME:
1449 dev->volume = ctrl->value;
1450 rc = em28xx_audio_analog_set(dev);
1454 mutex_unlock(&dev->lock);
1458 static int vidioc_g_tuner(struct file *file, void *priv,
1459 struct v4l2_tuner *t)
1461 struct em28xx_fh *fh = priv;
1462 struct em28xx *dev = fh->dev;
1465 rc = check_dev(dev);
1472 strcpy(t->name, "Tuner");
1474 mutex_lock(&dev->lock);
1475 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1476 mutex_unlock(&dev->lock);
1481 static int vidioc_s_tuner(struct file *file, void *priv,
1482 struct v4l2_tuner *t)
1484 struct em28xx_fh *fh = priv;
1485 struct em28xx *dev = fh->dev;
1488 rc = check_dev(dev);
1495 mutex_lock(&dev->lock);
1496 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1497 mutex_unlock(&dev->lock);
1502 static int vidioc_g_frequency(struct file *file, void *priv,
1503 struct v4l2_frequency *f)
1505 struct em28xx_fh *fh = priv;
1506 struct em28xx *dev = fh->dev;
1508 mutex_lock(&dev->lock);
1509 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1510 f->frequency = dev->ctl_freq;
1511 mutex_unlock(&dev->lock);
1516 static int vidioc_s_frequency(struct file *file, void *priv,
1517 struct v4l2_frequency *f)
1519 struct em28xx_fh *fh = priv;
1520 struct em28xx *dev = fh->dev;
1523 rc = check_dev(dev);
1530 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1532 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1535 mutex_lock(&dev->lock);
1537 dev->ctl_freq = f->frequency;
1538 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1540 mutex_unlock(&dev->lock);
1545 #ifdef CONFIG_VIDEO_ADV_DEBUG
1546 static int em28xx_reg_len(int reg)
1549 case EM28XX_R40_AC97LSB:
1550 case EM28XX_R30_HSCALELOW:
1551 case EM28XX_R32_VSCALELOW:
1558 static int vidioc_g_chip_ident(struct file *file, void *priv,
1559 struct v4l2_dbg_chip_ident *chip)
1561 struct em28xx_fh *fh = priv;
1562 struct em28xx *dev = fh->dev;
1564 chip->ident = V4L2_IDENT_NONE;
1567 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
1573 static int vidioc_g_register(struct file *file, void *priv,
1574 struct v4l2_dbg_register *reg)
1576 struct em28xx_fh *fh = priv;
1577 struct em28xx *dev = fh->dev;
1580 switch (reg->match.type) {
1581 case V4L2_CHIP_MATCH_AC97:
1582 mutex_lock(&dev->lock);
1583 ret = em28xx_read_ac97(dev, reg->reg);
1584 mutex_unlock(&dev->lock);
1591 case V4L2_CHIP_MATCH_I2C_DRIVER:
1592 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1594 case V4L2_CHIP_MATCH_I2C_ADDR:
1595 /* TODO: is this correct? */
1596 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1599 if (!v4l2_chip_match_host(®->match))
1604 reg->size = em28xx_reg_len(reg->reg);
1605 if (reg->size == 1) {
1606 mutex_lock(&dev->lock);
1607 ret = em28xx_read_reg(dev, reg->reg);
1608 mutex_unlock(&dev->lock);
1616 mutex_lock(&dev->lock);
1617 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1618 reg->reg, (char *)&val, 2);
1619 mutex_unlock(&dev->lock);
1623 reg->val = le16_to_cpu(val);
1629 static int vidioc_s_register(struct file *file, void *priv,
1630 struct v4l2_dbg_register *reg)
1632 struct em28xx_fh *fh = priv;
1633 struct em28xx *dev = fh->dev;
1637 switch (reg->match.type) {
1638 case V4L2_CHIP_MATCH_AC97:
1639 mutex_lock(&dev->lock);
1640 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1641 mutex_unlock(&dev->lock);
1644 case V4L2_CHIP_MATCH_I2C_DRIVER:
1645 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1647 case V4L2_CHIP_MATCH_I2C_ADDR:
1648 /* TODO: is this correct? */
1649 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1652 if (!v4l2_chip_match_host(®->match))
1657 buf = cpu_to_le16(reg->val);
1659 mutex_lock(&dev->lock);
1660 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1661 em28xx_reg_len(reg->reg));
1662 mutex_unlock(&dev->lock);
1669 static int vidioc_cropcap(struct file *file, void *priv,
1670 struct v4l2_cropcap *cc)
1672 struct em28xx_fh *fh = priv;
1673 struct em28xx *dev = fh->dev;
1675 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1678 cc->bounds.left = 0;
1680 cc->bounds.width = dev->width;
1681 cc->bounds.height = dev->height;
1682 cc->defrect = cc->bounds;
1683 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1684 cc->pixelaspect.denominator = 59;
1689 static int vidioc_streamon(struct file *file, void *priv,
1690 enum v4l2_buf_type type)
1692 struct em28xx_fh *fh = priv;
1693 struct em28xx *dev = fh->dev;
1696 rc = check_dev(dev);
1700 if (unlikely(type != fh->type))
1703 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1704 fh, type, fh->resources, dev->resources);
1706 if (unlikely(!res_get(fh,get_ressource(fh))))
1709 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1710 rc = videobuf_streamon(&fh->vb_vidq);
1711 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1712 rc = videobuf_streamon(&fh->vb_vbiq);
1717 static int vidioc_streamoff(struct file *file, void *priv,
1718 enum v4l2_buf_type type)
1720 struct em28xx_fh *fh = priv;
1721 struct em28xx *dev = fh->dev;
1724 rc = check_dev(dev);
1728 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1729 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
1731 if (type != fh->type)
1734 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1735 fh, type, fh->resources, dev->resources);
1737 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1738 videobuf_streamoff(&fh->vb_vidq);
1739 res_free(fh, EM28XX_RESOURCE_VIDEO);
1740 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1741 videobuf_streamoff(&fh->vb_vbiq);
1742 res_free(fh, EM28XX_RESOURCE_VBI);
1748 static int vidioc_querycap(struct file *file, void *priv,
1749 struct v4l2_capability *cap)
1751 struct em28xx_fh *fh = priv;
1752 struct em28xx *dev = fh->dev;
1754 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1755 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1756 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1758 cap->version = EM28XX_VERSION_CODE;
1761 V4L2_CAP_VBI_CAPTURE |
1762 V4L2_CAP_SLICED_VBI_CAPTURE |
1763 V4L2_CAP_VIDEO_CAPTURE |
1764 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1766 if (dev->audio_mode.has_audio)
1767 cap->capabilities |= V4L2_CAP_AUDIO;
1769 if (dev->tuner_type != TUNER_ABSENT)
1770 cap->capabilities |= V4L2_CAP_TUNER;
1775 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1776 struct v4l2_fmtdesc *f)
1778 if (unlikely(f->index >= ARRAY_SIZE(format)))
1781 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1782 f->pixelformat = format[f->index].fourcc;
1787 /* Sliced VBI ioctls */
1788 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1789 struct v4l2_format *f)
1791 struct em28xx_fh *fh = priv;
1792 struct em28xx *dev = fh->dev;
1795 rc = check_dev(dev);
1799 mutex_lock(&dev->lock);
1801 f->fmt.sliced.service_set = 0;
1802 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
1804 if (f->fmt.sliced.service_set == 0)
1807 mutex_unlock(&dev->lock);
1812 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1813 struct v4l2_format *f)
1815 struct em28xx_fh *fh = priv;
1816 struct em28xx *dev = fh->dev;
1819 rc = check_dev(dev);
1823 mutex_lock(&dev->lock);
1824 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
1825 mutex_unlock(&dev->lock);
1827 if (f->fmt.sliced.service_set == 0)
1833 /* RAW VBI ioctls */
1835 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1836 struct v4l2_format *format)
1838 format->fmt.vbi.samples_per_line = 720;
1839 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1840 format->fmt.vbi.offset = 0;
1841 format->fmt.vbi.flags = 0;
1843 /* Varies by video standard (NTSC, PAL, etc.) */
1844 /* FIXME: hard-coded for NTSC support */
1845 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; /* FIXME: ??? */
1846 format->fmt.vbi.count[0] = 12;
1847 format->fmt.vbi.count[1] = 12;
1848 format->fmt.vbi.start[0] = 10;
1849 format->fmt.vbi.start[1] = 273;
1854 static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1855 struct v4l2_format *format)
1857 format->fmt.vbi.samples_per_line = 720;
1858 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1859 format->fmt.vbi.offset = 0;
1860 format->fmt.vbi.flags = 0;
1862 /* Varies by video standard (NTSC, PAL, etc.) */
1863 /* FIXME: hard-coded for NTSC support */
1864 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; /* FIXME: ??? */
1865 format->fmt.vbi.count[0] = 12;
1866 format->fmt.vbi.count[1] = 12;
1867 format->fmt.vbi.start[0] = 10;
1868 format->fmt.vbi.start[1] = 273;
1873 static int vidioc_reqbufs(struct file *file, void *priv,
1874 struct v4l2_requestbuffers *rb)
1876 struct em28xx_fh *fh = priv;
1877 struct em28xx *dev = fh->dev;
1880 rc = check_dev(dev);
1884 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1885 return videobuf_reqbufs(&fh->vb_vidq, rb);
1887 return videobuf_reqbufs(&fh->vb_vbiq, rb);
1890 static int vidioc_querybuf(struct file *file, void *priv,
1891 struct v4l2_buffer *b)
1893 struct em28xx_fh *fh = priv;
1894 struct em28xx *dev = fh->dev;
1897 rc = check_dev(dev);
1901 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1902 return videobuf_querybuf(&fh->vb_vidq, b);
1904 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1905 the videobuf framework, but we probably shouldn't be
1906 returning a buffer larger than that which was asked for.
1907 At a minimum, it causes a crash in zvbi since it does
1908 a memcpy based on the source buffer length */
1909 int result = videobuf_querybuf(&fh->vb_vbiq, b);
1915 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1917 struct em28xx_fh *fh = priv;
1918 struct em28xx *dev = fh->dev;
1921 rc = check_dev(dev);
1925 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1926 return videobuf_qbuf(&fh->vb_vidq, b);
1928 return videobuf_qbuf(&fh->vb_vbiq, b);
1932 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1934 struct em28xx_fh *fh = priv;
1935 struct em28xx *dev = fh->dev;
1938 rc = check_dev(dev);
1942 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1943 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1946 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1950 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1951 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1953 struct em28xx_fh *fh = priv;
1955 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1956 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1958 return videobuf_cgmbuf(&fh->vb_vbiq, mbuf, 8);
1963 /* ----------------------------------------------------------- */
1964 /* RADIO ESPECIFIC IOCTLS */
1965 /* ----------------------------------------------------------- */
1967 static int radio_querycap(struct file *file, void *priv,
1968 struct v4l2_capability *cap)
1970 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1972 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1973 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1974 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1976 cap->version = EM28XX_VERSION_CODE;
1977 cap->capabilities = V4L2_CAP_TUNER;
1981 static int radio_g_tuner(struct file *file, void *priv,
1982 struct v4l2_tuner *t)
1984 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1986 if (unlikely(t->index > 0))
1989 strcpy(t->name, "Radio");
1990 t->type = V4L2_TUNER_RADIO;
1992 mutex_lock(&dev->lock);
1993 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1994 mutex_unlock(&dev->lock);
1999 static int radio_enum_input(struct file *file, void *priv,
2000 struct v4l2_input *i)
2004 strcpy(i->name, "Radio");
2005 i->type = V4L2_INPUT_TYPE_TUNER;
2010 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2012 if (unlikely(a->index))
2015 strcpy(a->name, "Radio");
2019 static int radio_s_tuner(struct file *file, void *priv,
2020 struct v4l2_tuner *t)
2022 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2027 mutex_lock(&dev->lock);
2028 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
2029 mutex_unlock(&dev->lock);
2034 static int radio_s_audio(struct file *file, void *fh,
2035 struct v4l2_audio *a)
2040 static int radio_s_input(struct file *file, void *fh, unsigned int i)
2045 static int radio_queryctrl(struct file *file, void *priv,
2046 struct v4l2_queryctrl *qc)
2050 if (qc->id < V4L2_CID_BASE ||
2051 qc->id >= V4L2_CID_LASTP1)
2054 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2055 if (qc->id && qc->id == ac97_qctrl[i].id) {
2056 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
2065 * em28xx_v4l2_open()
2066 * inits the device and starts isoc transfer
2068 static int em28xx_v4l2_open(struct file *filp)
2070 int minor = video_devdata(filp)->minor;
2071 int errCode = 0, radio;
2073 enum v4l2_buf_type fh_type;
2074 struct em28xx_fh *fh;
2075 enum v4l2_field field;
2077 dev = em28xx_get_device(minor, &fh_type, &radio);
2082 mutex_lock(&dev->lock);
2084 em28xx_videodbg("open minor=%d type=%s users=%d\n",
2085 minor, v4l2_type_names[fh_type], dev->users);
2088 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
2090 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
2091 mutex_unlock(&dev->lock);
2097 filp->private_data = fh;
2099 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
2100 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
2101 em28xx_set_alternate(dev);
2102 em28xx_resolution_set(dev);
2104 /* Needed, since GPIO might have disabled power of
2107 em28xx_wake_i2c(dev);
2111 em28xx_videodbg("video_open: setting radio device\n");
2112 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
2117 if (dev->progressive)
2118 field = V4L2_FIELD_NONE;
2120 field = V4L2_FIELD_INTERLACED;
2122 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2124 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
2125 sizeof(struct em28xx_buffer), fh);
2127 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2129 V4L2_BUF_TYPE_VBI_CAPTURE,
2131 sizeof(struct em28xx_buffer), fh);
2133 mutex_unlock(&dev->lock);
2139 * em28xx_realease_resources()
2140 * unregisters the v4l2,i2c and usb devices
2141 * called when the device gets disconected or at module unload
2143 void em28xx_release_analog_resources(struct em28xx *dev)
2146 /*FIXME: I2C IR should be disconnected */
2148 if (dev->radio_dev) {
2149 if (-1 != dev->radio_dev->minor)
2150 video_unregister_device(dev->radio_dev);
2152 video_device_release(dev->radio_dev);
2153 dev->radio_dev = NULL;
2156 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
2158 if (-1 != dev->vbi_dev->minor)
2159 video_unregister_device(dev->vbi_dev);
2161 video_device_release(dev->vbi_dev);
2162 dev->vbi_dev = NULL;
2165 em28xx_info("V4L2 device /dev/video%d deregistered\n",
2167 if (-1 != dev->vdev->minor)
2168 video_unregister_device(dev->vdev);
2170 video_device_release(dev->vdev);
2176 * em28xx_v4l2_close()
2177 * stops streaming and deallocates all resources allocated by the v4l2
2180 static int em28xx_v4l2_close(struct file *filp)
2182 struct em28xx_fh *fh = filp->private_data;
2183 struct em28xx *dev = fh->dev;
2186 em28xx_videodbg("users=%d\n", dev->users);
2188 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
2189 videobuf_stop(&fh->vb_vidq);
2190 res_free(fh, EM28XX_RESOURCE_VIDEO);
2193 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2194 videobuf_stop(&fh->vb_vbiq);
2195 res_free(fh, EM28XX_RESOURCE_VBI);
2198 if(dev->users == 1) {
2199 /* the device is already disconnect,
2200 free the remaining resources */
2201 if (dev->state & DEV_DISCONNECTED) {
2202 em28xx_release_resources(dev);
2207 /* Save some power by putting tuner to sleep */
2208 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_standby);
2210 /* do this before setting alternate! */
2211 em28xx_uninit_isoc(dev);
2212 em28xx_set_mode(dev, EM28XX_SUSPEND);
2214 /* set alternate 0 */
2216 em28xx_videodbg("setting alternate 0\n");
2217 errCode = usb_set_interface(dev->udev, 0, 0);
2219 em28xx_errdev("cannot change alternate number to "
2220 "0 (error=%i)\n", errCode);
2224 videobuf_mmap_free(&fh->vb_vidq);
2225 videobuf_mmap_free(&fh->vb_vbiq);
2228 wake_up_interruptible_nr(&dev->open, 1);
2233 * em28xx_v4l2_read()
2234 * will allocate buffers when called for the first time
2237 em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2240 struct em28xx_fh *fh = filp->private_data;
2241 struct em28xx *dev = fh->dev;
2244 rc = check_dev(dev);
2248 /* FIXME: read() is not prepared to allow changing the video
2249 resolution while streaming. Seems a bug at em28xx_set_fmt
2252 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2253 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
2256 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2257 filp->f_flags & O_NONBLOCK);
2261 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2262 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2265 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
2266 filp->f_flags & O_NONBLOCK);
2273 * em28xx_v4l2_poll()
2274 * will allocate buffers when called for the first time
2276 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2278 struct em28xx_fh *fh = filp->private_data;
2279 struct em28xx *dev = fh->dev;
2282 rc = check_dev(dev);
2286 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2287 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2289 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2290 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2291 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2293 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
2300 * em28xx_v4l2_mmap()
2302 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2304 struct em28xx_fh *fh = filp->private_data;
2305 struct em28xx *dev = fh->dev;
2308 rc = check_dev(dev);
2312 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2313 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2314 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2315 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
2317 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2318 (unsigned long)vma->vm_start,
2319 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2325 static const struct v4l2_file_operations em28xx_v4l_fops = {
2326 .owner = THIS_MODULE,
2327 .open = em28xx_v4l2_open,
2328 .release = em28xx_v4l2_close,
2329 .read = em28xx_v4l2_read,
2330 .poll = em28xx_v4l2_poll,
2331 .mmap = em28xx_v4l2_mmap,
2332 .ioctl = video_ioctl2,
2335 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2336 .vidioc_querycap = vidioc_querycap,
2337 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2338 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2339 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2340 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2341 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2342 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
2343 .vidioc_g_audio = vidioc_g_audio,
2344 .vidioc_s_audio = vidioc_s_audio,
2345 .vidioc_cropcap = vidioc_cropcap,
2347 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2348 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2349 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2351 .vidioc_reqbufs = vidioc_reqbufs,
2352 .vidioc_querybuf = vidioc_querybuf,
2353 .vidioc_qbuf = vidioc_qbuf,
2354 .vidioc_dqbuf = vidioc_dqbuf,
2355 .vidioc_s_std = vidioc_s_std,
2356 .vidioc_g_parm = vidioc_g_parm,
2357 .vidioc_s_parm = vidioc_s_parm,
2358 .vidioc_enum_input = vidioc_enum_input,
2359 .vidioc_g_input = vidioc_g_input,
2360 .vidioc_s_input = vidioc_s_input,
2361 .vidioc_queryctrl = vidioc_queryctrl,
2362 .vidioc_g_ctrl = vidioc_g_ctrl,
2363 .vidioc_s_ctrl = vidioc_s_ctrl,
2364 .vidioc_streamon = vidioc_streamon,
2365 .vidioc_streamoff = vidioc_streamoff,
2366 .vidioc_g_tuner = vidioc_g_tuner,
2367 .vidioc_s_tuner = vidioc_s_tuner,
2368 .vidioc_g_frequency = vidioc_g_frequency,
2369 .vidioc_s_frequency = vidioc_s_frequency,
2370 #ifdef CONFIG_VIDEO_ADV_DEBUG
2371 .vidioc_g_register = vidioc_g_register,
2372 .vidioc_s_register = vidioc_s_register,
2373 .vidioc_g_chip_ident = vidioc_g_chip_ident,
2375 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2376 .vidiocgmbuf = vidiocgmbuf,
2380 static const struct video_device em28xx_video_template = {
2381 .fops = &em28xx_v4l_fops,
2382 .release = video_device_release,
2383 .ioctl_ops = &video_ioctl_ops,
2387 .tvnorms = V4L2_STD_ALL,
2388 /* FIXME: we need this to be NTSC for VBI to work - it should
2389 be moved to a per-board definition */
2390 .current_norm = V4L2_STD_NTSC,
2393 static const struct v4l2_file_operations radio_fops = {
2394 .owner = THIS_MODULE,
2395 .open = em28xx_v4l2_open,
2396 .release = em28xx_v4l2_close,
2397 .ioctl = video_ioctl2,
2400 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2401 .vidioc_querycap = radio_querycap,
2402 .vidioc_g_tuner = radio_g_tuner,
2403 .vidioc_enum_input = radio_enum_input,
2404 .vidioc_g_audio = radio_g_audio,
2405 .vidioc_s_tuner = radio_s_tuner,
2406 .vidioc_s_audio = radio_s_audio,
2407 .vidioc_s_input = radio_s_input,
2408 .vidioc_queryctrl = radio_queryctrl,
2409 .vidioc_g_ctrl = vidioc_g_ctrl,
2410 .vidioc_s_ctrl = vidioc_s_ctrl,
2411 .vidioc_g_frequency = vidioc_g_frequency,
2412 .vidioc_s_frequency = vidioc_s_frequency,
2413 #ifdef CONFIG_VIDEO_ADV_DEBUG
2414 .vidioc_g_register = vidioc_g_register,
2415 .vidioc_s_register = vidioc_s_register,
2419 static struct video_device em28xx_radio_template = {
2420 .name = "em28xx-radio",
2421 .fops = &radio_fops,
2422 .ioctl_ops = &radio_ioctl_ops,
2426 /******************************** usb interface ******************************/
2430 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2431 const struct video_device *template,
2432 const char *type_name)
2434 struct video_device *vfd;
2436 vfd = video_device_alloc();
2442 vfd->v4l2_dev = &dev->v4l2_dev;
2443 vfd->release = video_device_release;
2444 vfd->debug = video_debug;
2446 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2447 dev->name, type_name);
2452 int em28xx_register_analog_devices(struct em28xx *dev)
2457 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
2459 (EM28XX_VERSION_CODE >> 16) & 0xff,
2460 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2462 /* set default norm */
2463 dev->norm = em28xx_video_template.current_norm;
2464 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
2467 /* Analog specific initialization */
2468 dev->format = &format[0];
2469 em28xx_set_video_format(dev, format[0].fourcc,
2470 norm_maxw(dev), norm_maxh(dev));
2472 video_mux(dev, dev->ctl_input);
2474 /* Audio defaults */
2478 /* enable vbi capturing */
2480 /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
2481 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2482 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2483 (EM28XX_XCLK_AUDIO_UNMUTE | val));
2484 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
2486 em28xx_set_outfmt(dev);
2487 em28xx_colorlevels_set_default(dev);
2488 em28xx_compression_disable(dev);
2490 /* allocate and fill video video_device struct */
2491 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2493 em28xx_errdev("cannot allocate video_device.\n");
2497 /* register v4l2 video video_device */
2498 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2499 video_nr[dev->devno]);
2501 em28xx_errdev("unable to register video device (error=%i).\n",
2506 /* Allocate and fill vbi video_device struct */
2507 if (em28xx_vbi_supported(dev) == 1) {
2508 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2511 /* register v4l2 vbi video_device */
2512 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2513 vbi_nr[dev->devno]);
2515 em28xx_errdev("unable to register vbi device\n");
2520 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2521 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2523 if (!dev->radio_dev) {
2524 em28xx_errdev("cannot allocate video_device.\n");
2527 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2528 radio_nr[dev->devno]);
2530 em28xx_errdev("can't register radio device\n");
2533 em28xx_info("Registered radio device as /dev/radio%d\n",
2534 dev->radio_dev->num);
2537 em28xx_info("V4L2 video device registered as /dev/video%d\n",
2541 em28xx_info("V4L2 VBI device registered as /dev/vbi%d\n",