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"
261 ((char *)startwrite + lencopy) -
262 ((char *)outp + buf->vb.size));
263 lencopy = remain = (char *)outp + buf->vb.size -
269 memcpy(startwrite, startread, lencopy);
277 static void em28xx_copy_vbi(struct em28xx *dev,
278 struct em28xx_dmaqueue *dma_q,
279 struct em28xx_buffer *buf,
281 unsigned char *outp, unsigned long len)
283 void *startwrite, *startread;
285 int bytesperline = 720;
288 em28xx_isocdbg("dev is null\n");
293 em28xx_isocdbg("dma_q is null\n");
300 em28xx_isocdbg("p is null\n");
304 em28xx_isocdbg("outp is null\n");
308 if (dma_q->pos + len > buf->vb.size)
309 len = buf->vb.size - dma_q->pos;
311 if ((p[0] == 0x33 && p[1] == 0x95) ||
312 (p[0] == 0x88 && p[1] == 0x88)) {
313 /* Header field, advance past it */
321 startwrite = outp + dma_q->pos;
324 /* Make sure the bottom field populates the second half of the frame */
325 if (buf->top_field == 0) {
326 startwrite += bytesperline * 0x0c;
327 offset += bytesperline * 0x0c;
330 memcpy(startwrite, startread, len);
334 static inline void print_err_status(struct em28xx *dev,
335 int packet, int status)
337 char *errmsg = "Unknown";
341 errmsg = "unlinked synchronuously";
344 errmsg = "unlinked asynchronuously";
347 errmsg = "Buffer error (overrun)";
350 errmsg = "Stalled (device not responding)";
353 errmsg = "Babble (bad cable?)";
356 errmsg = "Bit-stuff error (bad cable?)";
359 errmsg = "CRC/Timeout (could be anything)";
362 errmsg = "Device does not respond";
366 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
368 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
369 packet, status, errmsg);
374 * video-buf generic routine to get the next available buffer
376 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
377 struct em28xx_buffer **buf)
379 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
382 if (list_empty(&dma_q->active)) {
383 em28xx_isocdbg("No active queue to serve\n");
384 dev->isoc_ctl.vid_buf = NULL;
389 /* Get the next buffer */
390 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
392 /* Cleans up buffer - Usefull for testing for frame/URB loss */
393 outp = videobuf_to_vmalloc(&(*buf)->vb);
394 memset(outp, 0, (*buf)->vb.size);
396 dev->isoc_ctl.vid_buf = *buf;
402 * video-buf generic routine to get the next available VBI buffer
404 static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
405 struct em28xx_buffer **buf)
407 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
410 if (list_empty(&dma_q->active)) {
411 em28xx_isocdbg("No active queue to serve\n");
412 dev->isoc_ctl.vbi_buf = NULL;
417 /* Get the next buffer */
418 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
419 /* Cleans up buffer - Usefull for testing for frame/URB loss */
420 outp = videobuf_to_vmalloc(&(*buf)->vb);
421 memset(outp, 0x00, (*buf)->vb.size);
423 dev->isoc_ctl.vbi_buf = *buf;
429 * Controls the isoc copy of each urb packet
431 static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
433 struct em28xx_buffer *buf;
434 struct em28xx_dmaqueue *dma_q = &dev->vidq;
435 unsigned char *outp = NULL;
436 int i, len = 0, rc = 1;
442 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
445 if (urb->status < 0) {
446 print_err_status(dev, -1, urb->status);
447 if (urb->status == -ENOENT)
451 buf = dev->isoc_ctl.vid_buf;
453 outp = videobuf_to_vmalloc(&buf->vb);
455 for (i = 0; i < urb->number_of_packets; i++) {
456 int status = urb->iso_frame_desc[i].status;
459 print_err_status(dev, i, status);
460 if (urb->iso_frame_desc[i].status != -EPROTO)
464 len = urb->iso_frame_desc[i].actual_length - 4;
466 if (urb->iso_frame_desc[i].actual_length <= 0) {
467 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
470 if (urb->iso_frame_desc[i].actual_length >
472 em28xx_isocdbg("packet bigger than packet size");
476 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
478 /* FIXME: incomplete buffer checks where removed to make
479 logic simpler. Impacts of those changes should be evaluated
481 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
482 em28xx_isocdbg("VBI HEADER!!!\n");
483 /* FIXME: Should add vbi copy */
486 if (p[0] == 0x22 && p[1] == 0x5a) {
487 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
488 len, (p[2] & 1) ? "odd" : "even");
490 if (dev->progressive || !(p[2] & 1)) {
492 buffer_filled(dev, dma_q, buf);
493 get_next_buf(dma_q, &buf);
497 outp = videobuf_to_vmalloc(&buf->vb);
510 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
515 /* Version of isoc handler that takes into account a mixture of video and
517 static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
519 struct em28xx_buffer *buf, *vbi_buf;
520 struct em28xx_dmaqueue *dma_q = &dev->vidq;
521 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
522 unsigned char *outp = NULL;
523 unsigned char *vbioutp = NULL;
524 int i, len = 0, rc = 1;
531 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
534 if (urb->status < 0) {
535 print_err_status(dev, -1, urb->status);
536 if (urb->status == -ENOENT)
540 buf = dev->isoc_ctl.vid_buf;
542 outp = videobuf_to_vmalloc(&buf->vb);
544 vbi_buf = dev->isoc_ctl.vbi_buf;
546 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
548 for (i = 0; i < urb->number_of_packets; i++) {
549 int status = urb->iso_frame_desc[i].status;
552 print_err_status(dev, i, status);
553 if (urb->iso_frame_desc[i].status != -EPROTO)
557 len = urb->iso_frame_desc[i].actual_length - 4;
559 if (urb->iso_frame_desc[i].actual_length <= 0) {
560 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
563 if (urb->iso_frame_desc[i].actual_length >
565 em28xx_isocdbg("packet bigger than packet size");
569 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
571 /* capture type 0 = vbi start
572 capture type 1 = video start
573 capture type 2 = video in progress */
574 if (p[0] == 0x33 && p[1] == 0x95) {
575 dev->capture_type = 0;
577 em28xx_isocdbg("VBI START HEADER!!!\n");
578 dev->cur_field = p[2];
581 /* FIXME: get rid of hard-coded value */
582 vbi_size = 720 * 0x0c;
584 if (dev->capture_type == 0) {
585 if (dev->vbi_read >= vbi_size) {
586 /* We've already read all the VBI data, so
587 treat the rest as video */
588 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
589 } else if ((dev->vbi_read + len) < vbi_size) {
590 /* This entire frame is VBI data */
591 if (dev->vbi_read == 0 &&
592 (!(dev->cur_field & 1))) {
593 /* Brand new frame */
595 vbi_buffer_filled(dev,
598 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
602 vbioutp = videobuf_to_vmalloc(
606 if (dev->vbi_read == 0) {
608 if (vbi_buf != NULL) {
609 if (dev->cur_field & 1)
610 vbi_buf->top_field = 0;
612 vbi_buf->top_field = 1;
616 dev->vbi_read += len;
617 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
620 /* Some of this frame is VBI data and some is
622 int vbi_data_len = vbi_size - dev->vbi_read;
623 dev->vbi_read += vbi_data_len;
624 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
625 vbioutp, vbi_data_len);
626 dev->capture_type = 1;
632 if (dev->capture_type == 1) {
633 dev->capture_type = 2;
634 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
635 len, (p[2] & 1) ? "odd" : "even");
637 if (dev->progressive || !(dev->cur_field & 1)) {
639 buffer_filled(dev, dma_q, buf);
640 get_next_buf(dma_q, &buf);
644 outp = videobuf_to_vmalloc(&buf->vb);
647 if (dev->cur_field & 1)
655 if (buf != NULL && dev->capture_type == 2)
656 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
662 /* ------------------------------------------------------------------
664 ------------------------------------------------------------------*/
667 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
669 struct em28xx_fh *fh = vq->priv_data;
670 struct em28xx *dev = fh->dev;
671 struct v4l2_frequency f;
673 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
677 *count = EM28XX_DEF_BUF;
679 if (*count < EM28XX_MIN_BUF)
680 *count = EM28XX_MIN_BUF;
682 /* Ask tuner to go to analog or radio mode */
683 memset(&f, 0, sizeof(f));
684 f.frequency = dev->ctl_freq;
685 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
687 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
692 /* This is called *without* dev->slock held; please keep it that way */
693 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
695 struct em28xx_fh *fh = vq->priv_data;
696 struct em28xx *dev = fh->dev;
697 unsigned long flags = 0;
701 /* We used to wait for the buffer to finish here, but this didn't work
702 because, as we were keeping the state as VIDEOBUF_QUEUED,
703 videobuf_queue_cancel marked it as finished for us.
704 (Also, it could wedge forever if the hardware was misconfigured.)
706 This should be safe; by the time we get here, the buffer isn't
707 queued anymore. If we ever start marking the buffers as
708 VIDEOBUF_ACTIVE, it won't be, though.
710 spin_lock_irqsave(&dev->slock, flags);
711 if (dev->isoc_ctl.vid_buf == buf)
712 dev->isoc_ctl.vid_buf = NULL;
713 spin_unlock_irqrestore(&dev->slock, flags);
715 videobuf_vmalloc_free(&buf->vb);
716 buf->vb.state = VIDEOBUF_NEEDS_INIT;
720 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
721 enum v4l2_field field)
723 struct em28xx_fh *fh = vq->priv_data;
724 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
725 struct em28xx *dev = fh->dev;
726 int rc = 0, urb_init = 0;
728 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
731 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
734 buf->vb.width = dev->width;
735 buf->vb.height = dev->height;
736 buf->vb.field = field;
738 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
739 rc = videobuf_iolock(vq, &buf->vb, NULL);
744 if (!dev->isoc_ctl.num_bufs)
748 if (em28xx_vbi_supported(dev) == 1)
749 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
752 em28xx_isoc_copy_vbi);
754 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
762 buf->vb.state = VIDEOBUF_PREPARED;
766 free_buffer(vq, buf);
771 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
773 struct em28xx_buffer *buf = container_of(vb,
774 struct em28xx_buffer,
776 struct em28xx_fh *fh = vq->priv_data;
777 struct em28xx *dev = fh->dev;
778 struct em28xx_dmaqueue *vidq = &dev->vidq;
780 buf->vb.state = VIDEOBUF_QUEUED;
781 list_add_tail(&buf->vb.queue, &vidq->active);
785 static void buffer_release(struct videobuf_queue *vq,
786 struct videobuf_buffer *vb)
788 struct em28xx_buffer *buf = container_of(vb,
789 struct em28xx_buffer,
791 struct em28xx_fh *fh = vq->priv_data;
792 struct em28xx *dev = (struct em28xx *)fh->dev;
794 em28xx_isocdbg("em28xx: called buffer_release\n");
796 free_buffer(vq, buf);
799 static struct videobuf_queue_ops em28xx_video_qops = {
800 .buf_setup = buffer_setup,
801 .buf_prepare = buffer_prepare,
802 .buf_queue = buffer_queue,
803 .buf_release = buffer_release,
806 /********************* v4l2 interface **************************************/
808 static void video_mux(struct em28xx *dev, int index)
810 dev->ctl_input = index;
811 dev->ctl_ainput = INPUT(index)->amux;
812 dev->ctl_aoutput = INPUT(index)->aout;
814 if (!dev->ctl_aoutput)
815 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
817 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
818 INPUT(index)->vmux, 0, 0);
820 if (dev->board.has_msp34xx) {
821 if (dev->i2s_speed) {
822 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
823 s_i2s_clock_freq, dev->i2s_speed);
825 /* Note: this is msp3400 specific */
826 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
827 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
830 if (dev->board.adecoder != EM28XX_NOADECODER) {
831 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
832 dev->ctl_ainput, dev->ctl_aoutput, 0);
835 em28xx_audio_analog_set(dev);
838 /* Usage lock check functions */
839 static int res_get(struct em28xx_fh *fh, unsigned int bit)
841 struct em28xx *dev = fh->dev;
843 if (fh->resources & bit)
844 /* have it already allocated */
848 mutex_lock(&dev->lock);
849 if (dev->resources & bit) {
850 /* no, someone else uses it */
851 mutex_unlock(&dev->lock);
854 /* it's free, grab it */
855 fh->resources |= bit;
856 dev->resources |= bit;
857 em28xx_videodbg("res: get %d\n", bit);
858 mutex_unlock(&dev->lock);
862 static int res_check(struct em28xx_fh *fh, unsigned int bit)
864 return fh->resources & bit;
867 static int res_locked(struct em28xx *dev, unsigned int bit)
869 return dev->resources & bit;
872 static void res_free(struct em28xx_fh *fh, unsigned int bits)
874 struct em28xx *dev = fh->dev;
876 BUG_ON((fh->resources & bits) != bits);
878 mutex_lock(&dev->lock);
879 fh->resources &= ~bits;
880 dev->resources &= ~bits;
881 em28xx_videodbg("res: put %d\n", bits);
882 mutex_unlock(&dev->lock);
885 static int get_ressource(struct em28xx_fh *fh)
888 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
889 return EM28XX_RESOURCE_VIDEO;
890 case V4L2_BUF_TYPE_VBI_CAPTURE:
891 return EM28XX_RESOURCE_VBI;
900 * return the ac97 supported controls
902 static int ac97_queryctrl(struct v4l2_queryctrl *qc)
906 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
907 if (qc->id && qc->id == ac97_qctrl[i].id) {
908 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
913 /* Control is not ac97 related */
919 * return the current values for ac97 mute and volume
921 static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
924 case V4L2_CID_AUDIO_MUTE:
925 ctrl->value = dev->mute;
927 case V4L2_CID_AUDIO_VOLUME:
928 ctrl->value = dev->volume;
931 /* Control is not ac97 related */
938 * set values for ac97 mute and volume
940 static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
944 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
945 if (ctrl->id == ac97_qctrl[i].id)
948 /* Announce that hasn't handle it */
952 if (ctrl->value < ac97_qctrl[i].minimum ||
953 ctrl->value > ac97_qctrl[i].maximum)
957 case V4L2_CID_AUDIO_MUTE:
958 dev->mute = ctrl->value;
960 case V4L2_CID_AUDIO_VOLUME:
961 dev->volume = ctrl->value;
965 return em28xx_audio_analog_set(dev);
968 static int check_dev(struct em28xx *dev)
970 if (dev->state & DEV_DISCONNECTED) {
971 em28xx_errdev("v4l2 ioctl: device not present\n");
975 if (dev->state & DEV_MISCONFIGURED) {
976 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
977 "close and open it again\n");
983 static void get_scale(struct em28xx *dev,
984 unsigned int width, unsigned int height,
985 unsigned int *hscale, unsigned int *vscale)
987 unsigned int maxw = norm_maxw(dev);
988 unsigned int maxh = norm_maxh(dev);
990 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
991 if (*hscale >= 0x4000)
994 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
995 if (*vscale >= 0x4000)
999 /* ------------------------------------------------------------------
1000 IOCTL vidioc handling
1001 ------------------------------------------------------------------*/
1003 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1004 struct v4l2_format *f)
1006 struct em28xx_fh *fh = priv;
1007 struct em28xx *dev = fh->dev;
1009 mutex_lock(&dev->lock);
1011 f->fmt.pix.width = dev->width;
1012 f->fmt.pix.height = dev->height;
1013 f->fmt.pix.pixelformat = dev->format->fourcc;
1014 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
1015 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
1016 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1018 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1019 if (dev->progressive)
1020 f->fmt.pix.field = V4L2_FIELD_NONE;
1022 f->fmt.pix.field = dev->interlaced ?
1023 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1025 mutex_unlock(&dev->lock);
1029 static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1033 for (i = 0; i < ARRAY_SIZE(format); i++)
1034 if (format[i].fourcc == fourcc)
1040 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1041 struct v4l2_format *f)
1043 struct em28xx_fh *fh = priv;
1044 struct em28xx *dev = fh->dev;
1045 unsigned int width = f->fmt.pix.width;
1046 unsigned int height = f->fmt.pix.height;
1047 unsigned int maxw = norm_maxw(dev);
1048 unsigned int maxh = norm_maxh(dev);
1049 unsigned int hscale, vscale;
1050 struct em28xx_fmt *fmt;
1052 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1054 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1055 f->fmt.pix.pixelformat);
1059 if (dev->board.is_em2800) {
1060 /* the em2800 can only scale down to 50% */
1061 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1062 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
1063 /* According to empiatech support the MaxPacketSize is too small
1064 * to support framesizes larger than 640x480 @ 30 fps or 640x576
1065 * @ 25 fps. As this would cut of a part of the image we prefer
1066 * 360x576 or 360x480 for now */
1067 if (width == maxw && height == maxh)
1070 /* width must even because of the YUYV format
1071 height must be even because of interlacing */
1072 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1076 get_scale(dev, width, height, &hscale, &vscale);
1078 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1079 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1081 f->fmt.pix.width = width;
1082 f->fmt.pix.height = height;
1083 f->fmt.pix.pixelformat = fmt->fourcc;
1084 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1085 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1086 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1087 if (dev->progressive)
1088 f->fmt.pix.field = V4L2_FIELD_NONE;
1090 f->fmt.pix.field = dev->interlaced ?
1091 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1096 static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1097 unsigned width, unsigned height)
1099 struct em28xx_fmt *fmt;
1101 fmt = format_by_fourcc(fourcc);
1107 dev->height = height;
1109 /* set new image size */
1110 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1112 em28xx_set_alternate(dev);
1113 em28xx_resolution_set(dev);
1118 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1119 struct v4l2_format *f)
1121 struct em28xx_fh *fh = priv;
1122 struct em28xx *dev = fh->dev;
1125 rc = check_dev(dev);
1129 mutex_lock(&dev->lock);
1131 vidioc_try_fmt_vid_cap(file, priv, f);
1133 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1134 em28xx_errdev("%s queue busy\n", __func__);
1139 rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
1140 f->fmt.pix.width, f->fmt.pix.height);
1143 mutex_unlock(&dev->lock);
1147 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1149 struct em28xx_fh *fh = priv;
1150 struct em28xx *dev = fh->dev;
1153 rc = check_dev(dev);
1162 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
1164 struct em28xx_fh *fh = priv;
1165 struct em28xx *dev = fh->dev;
1166 struct v4l2_format f;
1169 rc = check_dev(dev);
1173 mutex_lock(&dev->lock);
1176 /* Adjusts width/height, if needed */
1177 f.fmt.pix.width = dev->width;
1178 f.fmt.pix.height = dev->height;
1179 vidioc_try_fmt_vid_cap(file, priv, &f);
1181 /* set new image size */
1182 dev->width = f.fmt.pix.width;
1183 dev->height = f.fmt.pix.height;
1184 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1186 em28xx_resolution_set(dev);
1187 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
1189 mutex_unlock(&dev->lock);
1193 static int vidioc_g_parm(struct file *file, void *priv,
1194 struct v4l2_streamparm *p)
1196 struct em28xx_fh *fh = priv;
1197 struct em28xx *dev = fh->dev;
1200 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1203 if (dev->board.is_webcam)
1204 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1207 v4l2_video_std_frame_period(dev->norm,
1208 &p->parm.capture.timeperframe);
1213 static int vidioc_s_parm(struct file *file, void *priv,
1214 struct v4l2_streamparm *p)
1216 struct em28xx_fh *fh = priv;
1217 struct em28xx *dev = fh->dev;
1219 if (!dev->board.is_webcam)
1222 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1225 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1228 static const char *iname[] = {
1229 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1230 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1231 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1232 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1233 [EM28XX_VMUX_SVIDEO] = "S-Video",
1234 [EM28XX_VMUX_TELEVISION] = "Television",
1235 [EM28XX_VMUX_CABLE] = "Cable TV",
1236 [EM28XX_VMUX_DVB] = "DVB",
1237 [EM28XX_VMUX_DEBUG] = "for debug only",
1240 static int vidioc_enum_input(struct file *file, void *priv,
1241 struct v4l2_input *i)
1243 struct em28xx_fh *fh = priv;
1244 struct em28xx *dev = fh->dev;
1248 if (n >= MAX_EM28XX_INPUT)
1250 if (0 == INPUT(n)->type)
1254 i->type = V4L2_INPUT_TYPE_CAMERA;
1256 strcpy(i->name, iname[INPUT(n)->type]);
1258 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1259 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1260 i->type = V4L2_INPUT_TYPE_TUNER;
1262 i->std = dev->vdev->tvnorms;
1267 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1269 struct em28xx_fh *fh = priv;
1270 struct em28xx *dev = fh->dev;
1272 *i = dev->ctl_input;
1277 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1279 struct em28xx_fh *fh = priv;
1280 struct em28xx *dev = fh->dev;
1283 rc = check_dev(dev);
1287 if (i >= MAX_EM28XX_INPUT)
1289 if (0 == INPUT(i)->type)
1294 mutex_lock(&dev->lock);
1295 video_mux(dev, dev->ctl_input);
1296 mutex_unlock(&dev->lock);
1300 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1302 struct em28xx_fh *fh = priv;
1303 struct em28xx *dev = fh->dev;
1305 if (!dev->audio_mode.has_audio)
1309 case EM28XX_AMUX_VIDEO:
1310 strcpy(a->name, "Television");
1312 case EM28XX_AMUX_LINE_IN:
1313 strcpy(a->name, "Line In");
1315 case EM28XX_AMUX_VIDEO2:
1316 strcpy(a->name, "Television alt");
1318 case EM28XX_AMUX_PHONE:
1319 strcpy(a->name, "Phone");
1321 case EM28XX_AMUX_MIC:
1322 strcpy(a->name, "Mic");
1324 case EM28XX_AMUX_CD:
1325 strcpy(a->name, "CD");
1327 case EM28XX_AMUX_AUX:
1328 strcpy(a->name, "Aux");
1330 case EM28XX_AMUX_PCM_OUT:
1331 strcpy(a->name, "PCM");
1337 a->index = dev->ctl_ainput;
1338 a->capability = V4L2_AUDCAP_STEREO;
1343 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1345 struct em28xx_fh *fh = priv;
1346 struct em28xx *dev = fh->dev;
1349 if (!dev->audio_mode.has_audio)
1352 if (a->index >= MAX_EM28XX_INPUT)
1354 if (0 == INPUT(a->index)->type)
1357 mutex_lock(&dev->lock);
1359 dev->ctl_ainput = INPUT(a->index)->amux;
1360 dev->ctl_aoutput = INPUT(a->index)->aout;
1362 if (!dev->ctl_aoutput)
1363 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
1365 mutex_unlock(&dev->lock);
1369 static int vidioc_queryctrl(struct file *file, void *priv,
1370 struct v4l2_queryctrl *qc)
1372 struct em28xx_fh *fh = priv;
1373 struct em28xx *dev = fh->dev;
1377 rc = check_dev(dev);
1381 memset(qc, 0, sizeof(*qc));
1385 /* enumberate AC97 controls */
1386 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1387 rc = ac97_queryctrl(qc);
1392 /* enumberate V4L2 device controls */
1393 mutex_lock(&dev->lock);
1394 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
1395 mutex_unlock(&dev->lock);
1403 static int vidioc_g_ctrl(struct file *file, void *priv,
1404 struct v4l2_control *ctrl)
1406 struct em28xx_fh *fh = priv;
1407 struct em28xx *dev = fh->dev;
1410 rc = check_dev(dev);
1415 mutex_lock(&dev->lock);
1417 /* Set an AC97 control */
1418 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1419 rc = ac97_get_ctrl(dev, ctrl);
1423 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1425 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1429 mutex_unlock(&dev->lock);
1433 static int vidioc_s_ctrl(struct file *file, void *priv,
1434 struct v4l2_control *ctrl)
1436 struct em28xx_fh *fh = priv;
1437 struct em28xx *dev = fh->dev;
1440 rc = check_dev(dev);
1444 mutex_lock(&dev->lock);
1446 /* Set an AC97 control */
1447 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1448 rc = ac97_set_ctrl(dev, ctrl);
1452 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
1454 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
1457 * In the case of non-AC97 volume controls, we still need
1458 * to do some setups at em28xx, in order to mute/unmute
1459 * and to adjust audio volume. However, the value ranges
1460 * should be checked by the corresponding V4L subdriver.
1463 case V4L2_CID_AUDIO_MUTE:
1464 dev->mute = ctrl->value;
1465 rc = em28xx_audio_analog_set(dev);
1467 case V4L2_CID_AUDIO_VOLUME:
1468 dev->volume = ctrl->value;
1469 rc = em28xx_audio_analog_set(dev);
1473 mutex_unlock(&dev->lock);
1477 static int vidioc_g_tuner(struct file *file, void *priv,
1478 struct v4l2_tuner *t)
1480 struct em28xx_fh *fh = priv;
1481 struct em28xx *dev = fh->dev;
1484 rc = check_dev(dev);
1491 strcpy(t->name, "Tuner");
1493 mutex_lock(&dev->lock);
1494 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1495 mutex_unlock(&dev->lock);
1500 static int vidioc_s_tuner(struct file *file, void *priv,
1501 struct v4l2_tuner *t)
1503 struct em28xx_fh *fh = priv;
1504 struct em28xx *dev = fh->dev;
1507 rc = check_dev(dev);
1514 mutex_lock(&dev->lock);
1515 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1516 mutex_unlock(&dev->lock);
1521 static int vidioc_g_frequency(struct file *file, void *priv,
1522 struct v4l2_frequency *f)
1524 struct em28xx_fh *fh = priv;
1525 struct em28xx *dev = fh->dev;
1527 mutex_lock(&dev->lock);
1528 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1529 f->frequency = dev->ctl_freq;
1530 mutex_unlock(&dev->lock);
1535 static int vidioc_s_frequency(struct file *file, void *priv,
1536 struct v4l2_frequency *f)
1538 struct em28xx_fh *fh = priv;
1539 struct em28xx *dev = fh->dev;
1542 rc = check_dev(dev);
1549 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1551 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1554 mutex_lock(&dev->lock);
1556 dev->ctl_freq = f->frequency;
1557 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1559 mutex_unlock(&dev->lock);
1564 #ifdef CONFIG_VIDEO_ADV_DEBUG
1565 static int em28xx_reg_len(int reg)
1568 case EM28XX_R40_AC97LSB:
1569 case EM28XX_R30_HSCALELOW:
1570 case EM28XX_R32_VSCALELOW:
1577 static int vidioc_g_chip_ident(struct file *file, void *priv,
1578 struct v4l2_dbg_chip_ident *chip)
1580 struct em28xx_fh *fh = priv;
1581 struct em28xx *dev = fh->dev;
1583 chip->ident = V4L2_IDENT_NONE;
1586 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
1592 static int vidioc_g_register(struct file *file, void *priv,
1593 struct v4l2_dbg_register *reg)
1595 struct em28xx_fh *fh = priv;
1596 struct em28xx *dev = fh->dev;
1599 switch (reg->match.type) {
1600 case V4L2_CHIP_MATCH_AC97:
1601 mutex_lock(&dev->lock);
1602 ret = em28xx_read_ac97(dev, reg->reg);
1603 mutex_unlock(&dev->lock);
1610 case V4L2_CHIP_MATCH_I2C_DRIVER:
1611 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1613 case V4L2_CHIP_MATCH_I2C_ADDR:
1614 /* TODO: is this correct? */
1615 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1618 if (!v4l2_chip_match_host(®->match))
1623 reg->size = em28xx_reg_len(reg->reg);
1624 if (reg->size == 1) {
1625 mutex_lock(&dev->lock);
1626 ret = em28xx_read_reg(dev, reg->reg);
1627 mutex_unlock(&dev->lock);
1635 mutex_lock(&dev->lock);
1636 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1637 reg->reg, (char *)&val, 2);
1638 mutex_unlock(&dev->lock);
1642 reg->val = le16_to_cpu(val);
1648 static int vidioc_s_register(struct file *file, void *priv,
1649 struct v4l2_dbg_register *reg)
1651 struct em28xx_fh *fh = priv;
1652 struct em28xx *dev = fh->dev;
1656 switch (reg->match.type) {
1657 case V4L2_CHIP_MATCH_AC97:
1658 mutex_lock(&dev->lock);
1659 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1660 mutex_unlock(&dev->lock);
1663 case V4L2_CHIP_MATCH_I2C_DRIVER:
1664 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1666 case V4L2_CHIP_MATCH_I2C_ADDR:
1667 /* TODO: is this correct? */
1668 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1671 if (!v4l2_chip_match_host(®->match))
1676 buf = cpu_to_le16(reg->val);
1678 mutex_lock(&dev->lock);
1679 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1680 em28xx_reg_len(reg->reg));
1681 mutex_unlock(&dev->lock);
1688 static int vidioc_cropcap(struct file *file, void *priv,
1689 struct v4l2_cropcap *cc)
1691 struct em28xx_fh *fh = priv;
1692 struct em28xx *dev = fh->dev;
1694 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1697 cc->bounds.left = 0;
1699 cc->bounds.width = dev->width;
1700 cc->bounds.height = dev->height;
1701 cc->defrect = cc->bounds;
1702 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1703 cc->pixelaspect.denominator = 59;
1708 static int vidioc_streamon(struct file *file, void *priv,
1709 enum v4l2_buf_type type)
1711 struct em28xx_fh *fh = priv;
1712 struct em28xx *dev = fh->dev;
1715 rc = check_dev(dev);
1719 if (unlikely(type != fh->type))
1722 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1723 fh, type, fh->resources, dev->resources);
1725 if (unlikely(!res_get(fh, get_ressource(fh))))
1728 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1729 rc = videobuf_streamon(&fh->vb_vidq);
1730 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1731 rc = videobuf_streamon(&fh->vb_vbiq);
1736 static int vidioc_streamoff(struct file *file, void *priv,
1737 enum v4l2_buf_type type)
1739 struct em28xx_fh *fh = priv;
1740 struct em28xx *dev = fh->dev;
1743 rc = check_dev(dev);
1747 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1748 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
1750 if (type != fh->type)
1753 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1754 fh, type, fh->resources, dev->resources);
1756 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1757 videobuf_streamoff(&fh->vb_vidq);
1758 res_free(fh, EM28XX_RESOURCE_VIDEO);
1759 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1760 videobuf_streamoff(&fh->vb_vbiq);
1761 res_free(fh, EM28XX_RESOURCE_VBI);
1767 static int vidioc_querycap(struct file *file, void *priv,
1768 struct v4l2_capability *cap)
1770 struct em28xx_fh *fh = priv;
1771 struct em28xx *dev = fh->dev;
1773 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1774 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1775 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1777 cap->version = EM28XX_VERSION_CODE;
1780 V4L2_CAP_SLICED_VBI_CAPTURE |
1781 V4L2_CAP_VIDEO_CAPTURE |
1782 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1785 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1787 if (dev->audio_mode.has_audio)
1788 cap->capabilities |= V4L2_CAP_AUDIO;
1790 if (dev->tuner_type != TUNER_ABSENT)
1791 cap->capabilities |= V4L2_CAP_TUNER;
1796 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1797 struct v4l2_fmtdesc *f)
1799 if (unlikely(f->index >= ARRAY_SIZE(format)))
1802 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1803 f->pixelformat = format[f->index].fourcc;
1808 /* Sliced VBI ioctls */
1809 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1810 struct v4l2_format *f)
1812 struct em28xx_fh *fh = priv;
1813 struct em28xx *dev = fh->dev;
1816 rc = check_dev(dev);
1820 mutex_lock(&dev->lock);
1822 f->fmt.sliced.service_set = 0;
1823 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
1825 if (f->fmt.sliced.service_set == 0)
1828 mutex_unlock(&dev->lock);
1833 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1834 struct v4l2_format *f)
1836 struct em28xx_fh *fh = priv;
1837 struct em28xx *dev = fh->dev;
1840 rc = check_dev(dev);
1844 mutex_lock(&dev->lock);
1845 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
1846 mutex_unlock(&dev->lock);
1848 if (f->fmt.sliced.service_set == 0)
1854 /* RAW VBI ioctls */
1856 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1857 struct v4l2_format *format)
1859 format->fmt.vbi.samples_per_line = 720;
1860 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1861 format->fmt.vbi.offset = 0;
1862 format->fmt.vbi.flags = 0;
1864 /* Varies by video standard (NTSC, PAL, etc.) */
1865 /* FIXME: hard-coded for NTSC support */
1866 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; /* FIXME: ??? */
1867 format->fmt.vbi.count[0] = 12;
1868 format->fmt.vbi.count[1] = 12;
1869 format->fmt.vbi.start[0] = 10;
1870 format->fmt.vbi.start[1] = 273;
1875 static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1876 struct v4l2_format *format)
1878 format->fmt.vbi.samples_per_line = 720;
1879 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1880 format->fmt.vbi.offset = 0;
1881 format->fmt.vbi.flags = 0;
1883 /* Varies by video standard (NTSC, PAL, etc.) */
1884 /* FIXME: hard-coded for NTSC support */
1885 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; /* FIXME: ??? */
1886 format->fmt.vbi.count[0] = 12;
1887 format->fmt.vbi.count[1] = 12;
1888 format->fmt.vbi.start[0] = 10;
1889 format->fmt.vbi.start[1] = 273;
1894 static int vidioc_reqbufs(struct file *file, void *priv,
1895 struct v4l2_requestbuffers *rb)
1897 struct em28xx_fh *fh = priv;
1898 struct em28xx *dev = fh->dev;
1901 rc = check_dev(dev);
1905 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1906 return videobuf_reqbufs(&fh->vb_vidq, rb);
1908 return videobuf_reqbufs(&fh->vb_vbiq, rb);
1911 static int vidioc_querybuf(struct file *file, void *priv,
1912 struct v4l2_buffer *b)
1914 struct em28xx_fh *fh = priv;
1915 struct em28xx *dev = fh->dev;
1918 rc = check_dev(dev);
1922 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1923 return videobuf_querybuf(&fh->vb_vidq, b);
1925 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1926 the videobuf framework, but we probably shouldn't be
1927 returning a buffer larger than that which was asked for.
1928 At a minimum, it causes a crash in zvbi since it does
1929 a memcpy based on the source buffer length */
1930 int result = videobuf_querybuf(&fh->vb_vbiq, b);
1936 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1938 struct em28xx_fh *fh = priv;
1939 struct em28xx *dev = fh->dev;
1942 rc = check_dev(dev);
1946 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1947 return videobuf_qbuf(&fh->vb_vidq, b);
1949 return videobuf_qbuf(&fh->vb_vbiq, b);
1952 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1954 struct em28xx_fh *fh = priv;
1955 struct em28xx *dev = fh->dev;
1958 rc = check_dev(dev);
1962 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1963 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1966 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1970 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1971 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1973 struct em28xx_fh *fh = priv;
1975 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1976 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1978 return videobuf_cgmbuf(&fh->vb_vbiq, mbuf, 8);
1983 /* ----------------------------------------------------------- */
1984 /* RADIO ESPECIFIC IOCTLS */
1985 /* ----------------------------------------------------------- */
1987 static int radio_querycap(struct file *file, void *priv,
1988 struct v4l2_capability *cap)
1990 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1992 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1993 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1994 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1996 cap->version = EM28XX_VERSION_CODE;
1997 cap->capabilities = V4L2_CAP_TUNER;
2001 static int radio_g_tuner(struct file *file, void *priv,
2002 struct v4l2_tuner *t)
2004 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2006 if (unlikely(t->index > 0))
2009 strcpy(t->name, "Radio");
2010 t->type = V4L2_TUNER_RADIO;
2012 mutex_lock(&dev->lock);
2013 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
2014 mutex_unlock(&dev->lock);
2019 static int radio_enum_input(struct file *file, void *priv,
2020 struct v4l2_input *i)
2024 strcpy(i->name, "Radio");
2025 i->type = V4L2_INPUT_TYPE_TUNER;
2030 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2032 if (unlikely(a->index))
2035 strcpy(a->name, "Radio");
2039 static int radio_s_tuner(struct file *file, void *priv,
2040 struct v4l2_tuner *t)
2042 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2047 mutex_lock(&dev->lock);
2048 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
2049 mutex_unlock(&dev->lock);
2054 static int radio_s_audio(struct file *file, void *fh,
2055 struct v4l2_audio *a)
2060 static int radio_s_input(struct file *file, void *fh, unsigned int i)
2065 static int radio_queryctrl(struct file *file, void *priv,
2066 struct v4l2_queryctrl *qc)
2070 if (qc->id < V4L2_CID_BASE ||
2071 qc->id >= V4L2_CID_LASTP1)
2074 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2075 if (qc->id && qc->id == ac97_qctrl[i].id) {
2076 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
2085 * em28xx_v4l2_open()
2086 * inits the device and starts isoc transfer
2088 static int em28xx_v4l2_open(struct file *filp)
2090 int minor = video_devdata(filp)->minor;
2091 int errCode = 0, radio;
2093 enum v4l2_buf_type fh_type;
2094 struct em28xx_fh *fh;
2095 enum v4l2_field field;
2097 dev = em28xx_get_device(minor, &fh_type, &radio);
2102 mutex_lock(&dev->lock);
2104 em28xx_videodbg("open minor=%d type=%s users=%d\n",
2105 minor, v4l2_type_names[fh_type], dev->users);
2108 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
2110 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
2111 mutex_unlock(&dev->lock);
2117 filp->private_data = fh;
2119 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
2120 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
2121 em28xx_set_alternate(dev);
2122 em28xx_resolution_set(dev);
2124 /* Needed, since GPIO might have disabled power of
2127 em28xx_wake_i2c(dev);
2131 em28xx_videodbg("video_open: setting radio device\n");
2132 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
2137 if (dev->progressive)
2138 field = V4L2_FIELD_NONE;
2140 field = V4L2_FIELD_INTERLACED;
2142 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2144 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
2145 sizeof(struct em28xx_buffer), fh);
2147 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2149 V4L2_BUF_TYPE_VBI_CAPTURE,
2151 sizeof(struct em28xx_buffer), fh);
2153 mutex_unlock(&dev->lock);
2159 * em28xx_realease_resources()
2160 * unregisters the v4l2,i2c and usb devices
2161 * called when the device gets disconected or at module unload
2163 void em28xx_release_analog_resources(struct em28xx *dev)
2166 /*FIXME: I2C IR should be disconnected */
2168 if (dev->radio_dev) {
2169 if (-1 != dev->radio_dev->minor)
2170 video_unregister_device(dev->radio_dev);
2172 video_device_release(dev->radio_dev);
2173 dev->radio_dev = NULL;
2176 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
2178 if (-1 != dev->vbi_dev->minor)
2179 video_unregister_device(dev->vbi_dev);
2181 video_device_release(dev->vbi_dev);
2182 dev->vbi_dev = NULL;
2185 em28xx_info("V4L2 device /dev/video%d deregistered\n",
2187 if (-1 != dev->vdev->minor)
2188 video_unregister_device(dev->vdev);
2190 video_device_release(dev->vdev);
2196 * em28xx_v4l2_close()
2197 * stops streaming and deallocates all resources allocated by the v4l2
2200 static int em28xx_v4l2_close(struct file *filp)
2202 struct em28xx_fh *fh = filp->private_data;
2203 struct em28xx *dev = fh->dev;
2206 em28xx_videodbg("users=%d\n", dev->users);
2208 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
2209 videobuf_stop(&fh->vb_vidq);
2210 res_free(fh, EM28XX_RESOURCE_VIDEO);
2213 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2214 videobuf_stop(&fh->vb_vbiq);
2215 res_free(fh, EM28XX_RESOURCE_VBI);
2218 if (dev->users == 1) {
2219 /* the device is already disconnect,
2220 free the remaining resources */
2221 if (dev->state & DEV_DISCONNECTED) {
2222 em28xx_release_resources(dev);
2227 /* Save some power by putting tuner to sleep */
2228 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_standby);
2230 /* do this before setting alternate! */
2231 em28xx_uninit_isoc(dev);
2232 em28xx_set_mode(dev, EM28XX_SUSPEND);
2234 /* set alternate 0 */
2236 em28xx_videodbg("setting alternate 0\n");
2237 errCode = usb_set_interface(dev->udev, 0, 0);
2239 em28xx_errdev("cannot change alternate number to "
2240 "0 (error=%i)\n", errCode);
2244 videobuf_mmap_free(&fh->vb_vidq);
2245 videobuf_mmap_free(&fh->vb_vbiq);
2248 wake_up_interruptible_nr(&dev->open, 1);
2253 * em28xx_v4l2_read()
2254 * will allocate buffers when called for the first time
2257 em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2260 struct em28xx_fh *fh = filp->private_data;
2261 struct em28xx *dev = fh->dev;
2264 rc = check_dev(dev);
2268 /* FIXME: read() is not prepared to allow changing the video
2269 resolution while streaming. Seems a bug at em28xx_set_fmt
2272 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2273 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
2276 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2277 filp->f_flags & O_NONBLOCK);
2281 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2282 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2285 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
2286 filp->f_flags & O_NONBLOCK);
2293 * em28xx_v4l2_poll()
2294 * will allocate buffers when called for the first time
2296 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2298 struct em28xx_fh *fh = filp->private_data;
2299 struct em28xx *dev = fh->dev;
2302 rc = check_dev(dev);
2306 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2307 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2309 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2310 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2311 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2313 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
2320 * em28xx_v4l2_mmap()
2322 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2324 struct em28xx_fh *fh = filp->private_data;
2325 struct em28xx *dev = fh->dev;
2328 rc = check_dev(dev);
2332 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2333 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2334 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2335 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
2337 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2338 (unsigned long)vma->vm_start,
2339 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2345 static const struct v4l2_file_operations em28xx_v4l_fops = {
2346 .owner = THIS_MODULE,
2347 .open = em28xx_v4l2_open,
2348 .release = em28xx_v4l2_close,
2349 .read = em28xx_v4l2_read,
2350 .poll = em28xx_v4l2_poll,
2351 .mmap = em28xx_v4l2_mmap,
2352 .ioctl = video_ioctl2,
2355 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2356 .vidioc_querycap = vidioc_querycap,
2357 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2358 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2359 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2360 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2361 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2362 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
2363 .vidioc_g_audio = vidioc_g_audio,
2364 .vidioc_s_audio = vidioc_s_audio,
2365 .vidioc_cropcap = vidioc_cropcap,
2367 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2368 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2369 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2371 .vidioc_reqbufs = vidioc_reqbufs,
2372 .vidioc_querybuf = vidioc_querybuf,
2373 .vidioc_qbuf = vidioc_qbuf,
2374 .vidioc_dqbuf = vidioc_dqbuf,
2375 .vidioc_g_std = vidioc_g_std,
2376 .vidioc_s_std = vidioc_s_std,
2377 .vidioc_g_parm = vidioc_g_parm,
2378 .vidioc_s_parm = vidioc_s_parm,
2379 .vidioc_enum_input = vidioc_enum_input,
2380 .vidioc_g_input = vidioc_g_input,
2381 .vidioc_s_input = vidioc_s_input,
2382 .vidioc_queryctrl = vidioc_queryctrl,
2383 .vidioc_g_ctrl = vidioc_g_ctrl,
2384 .vidioc_s_ctrl = vidioc_s_ctrl,
2385 .vidioc_streamon = vidioc_streamon,
2386 .vidioc_streamoff = vidioc_streamoff,
2387 .vidioc_g_tuner = vidioc_g_tuner,
2388 .vidioc_s_tuner = vidioc_s_tuner,
2389 .vidioc_g_frequency = vidioc_g_frequency,
2390 .vidioc_s_frequency = vidioc_s_frequency,
2391 #ifdef CONFIG_VIDEO_ADV_DEBUG
2392 .vidioc_g_register = vidioc_g_register,
2393 .vidioc_s_register = vidioc_s_register,
2394 .vidioc_g_chip_ident = vidioc_g_chip_ident,
2396 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2397 .vidiocgmbuf = vidiocgmbuf,
2401 static const struct video_device em28xx_video_template = {
2402 .fops = &em28xx_v4l_fops,
2403 .release = video_device_release,
2404 .ioctl_ops = &video_ioctl_ops,
2408 .tvnorms = V4L2_STD_ALL,
2409 .current_norm = V4L2_STD_PAL,
2412 static const struct v4l2_file_operations radio_fops = {
2413 .owner = THIS_MODULE,
2414 .open = em28xx_v4l2_open,
2415 .release = em28xx_v4l2_close,
2416 .ioctl = video_ioctl2,
2419 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2420 .vidioc_querycap = radio_querycap,
2421 .vidioc_g_tuner = radio_g_tuner,
2422 .vidioc_enum_input = radio_enum_input,
2423 .vidioc_g_audio = radio_g_audio,
2424 .vidioc_s_tuner = radio_s_tuner,
2425 .vidioc_s_audio = radio_s_audio,
2426 .vidioc_s_input = radio_s_input,
2427 .vidioc_queryctrl = radio_queryctrl,
2428 .vidioc_g_ctrl = vidioc_g_ctrl,
2429 .vidioc_s_ctrl = vidioc_s_ctrl,
2430 .vidioc_g_frequency = vidioc_g_frequency,
2431 .vidioc_s_frequency = vidioc_s_frequency,
2432 #ifdef CONFIG_VIDEO_ADV_DEBUG
2433 .vidioc_g_register = vidioc_g_register,
2434 .vidioc_s_register = vidioc_s_register,
2438 static struct video_device em28xx_radio_template = {
2439 .name = "em28xx-radio",
2440 .fops = &radio_fops,
2441 .ioctl_ops = &radio_ioctl_ops,
2445 /******************************** usb interface ******************************/
2449 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2450 const struct video_device *template,
2451 const char *type_name)
2453 struct video_device *vfd;
2455 vfd = video_device_alloc();
2461 vfd->v4l2_dev = &dev->v4l2_dev;
2462 vfd->release = video_device_release;
2463 vfd->debug = video_debug;
2465 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2466 dev->name, type_name);
2471 int em28xx_register_analog_devices(struct em28xx *dev)
2476 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
2478 (EM28XX_VERSION_CODE >> 16) & 0xff,
2479 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2481 /* set default norm */
2482 dev->norm = em28xx_video_template.current_norm;
2483 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
2486 /* Analog specific initialization */
2487 dev->format = &format[0];
2488 em28xx_set_video_format(dev, format[0].fourcc,
2489 norm_maxw(dev), norm_maxh(dev));
2491 video_mux(dev, dev->ctl_input);
2493 /* Audio defaults */
2497 /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
2498 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2499 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2500 (EM28XX_XCLK_AUDIO_UNMUTE | val));
2502 em28xx_set_outfmt(dev);
2503 em28xx_colorlevels_set_default(dev);
2504 em28xx_compression_disable(dev);
2506 /* allocate and fill video video_device struct */
2507 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2509 em28xx_errdev("cannot allocate video_device.\n");
2513 /* register v4l2 video video_device */
2514 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2515 video_nr[dev->devno]);
2517 em28xx_errdev("unable to register video device (error=%i).\n",
2522 /* Allocate and fill vbi video_device struct */
2523 if (em28xx_vbi_supported(dev) == 1) {
2524 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2527 /* register v4l2 vbi video_device */
2528 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2529 vbi_nr[dev->devno]);
2531 em28xx_errdev("unable to register vbi device\n");
2536 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2537 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2539 if (!dev->radio_dev) {
2540 em28xx_errdev("cannot allocate video_device.\n");
2543 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2544 radio_nr[dev->devno]);
2546 em28xx_errdev("can't register radio device\n");
2549 em28xx_info("Registered radio device as /dev/radio%d\n",
2550 dev->radio_dev->num);
2553 em28xx_info("V4L2 video device registered as /dev/video%d\n",
2557 em28xx_info("V4L2 VBI device registered as /dev/vbi%d\n",