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>
38 #include <linux/slab.h>
41 #include <media/v4l2-common.h>
42 #include <media/v4l2-ioctl.h>
43 #include <media/v4l2-chip-ident.h>
44 #include <media/msp3400.h>
45 #include <media/tuner.h>
47 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
48 "Markus Rechberger <mrechberger@gmail.com>, " \
49 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
50 "Sascha Sommer <saschasommer@freenet.de>"
52 #define DRIVER_DESC "Empia em28xx based USB video device driver"
53 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
55 #define em28xx_videodbg(fmt, arg...) do {\
57 printk(KERN_INFO "%s %s :"fmt, \
58 dev->name, __func__ , ##arg); } while (0)
60 static unsigned int isoc_debug
;
61 module_param(isoc_debug
, int, 0644);
62 MODULE_PARM_DESC(isoc_debug
, "enable debug messages [isoc transfers]");
64 #define em28xx_isocdbg(fmt, arg...) \
67 printk(KERN_INFO "%s %s :"fmt, \
68 dev->name, __func__ , ##arg); \
72 MODULE_AUTHOR(DRIVER_AUTHOR
);
73 MODULE_DESCRIPTION(DRIVER_DESC
);
74 MODULE_LICENSE("GPL");
76 static unsigned int video_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
77 static unsigned int vbi_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
78 static unsigned int radio_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
80 module_param_array(video_nr
, int, NULL
, 0444);
81 module_param_array(vbi_nr
, int, NULL
, 0444);
82 module_param_array(radio_nr
, int, NULL
, 0444);
83 MODULE_PARM_DESC(video_nr
, "video device numbers");
84 MODULE_PARM_DESC(vbi_nr
, "vbi device numbers");
85 MODULE_PARM_DESC(radio_nr
, "radio device numbers");
87 static unsigned int video_debug
;
88 module_param(video_debug
, int, 0644);
89 MODULE_PARM_DESC(video_debug
, "enable debug messages [video]");
91 /* supported video standards */
92 static struct em28xx_fmt format
[] = {
94 .name
= "16 bpp YUY2, 4:2:2, packed",
95 .fourcc
= V4L2_PIX_FMT_YUYV
,
97 .reg
= EM28XX_OUTFMT_YUV422_Y0UY1V
,
99 .name
= "16 bpp RGB 565, LE",
100 .fourcc
= V4L2_PIX_FMT_RGB565
,
102 .reg
= EM28XX_OUTFMT_RGB_16_656
,
104 .name
= "8 bpp Bayer BGBG..GRGR",
105 .fourcc
= V4L2_PIX_FMT_SBGGR8
,
107 .reg
= EM28XX_OUTFMT_RGB_8_BGBG
,
109 .name
= "8 bpp Bayer GRGR..BGBG",
110 .fourcc
= V4L2_PIX_FMT_SGRBG8
,
112 .reg
= EM28XX_OUTFMT_RGB_8_GRGR
,
114 .name
= "8 bpp Bayer GBGB..RGRG",
115 .fourcc
= V4L2_PIX_FMT_SGBRG8
,
117 .reg
= EM28XX_OUTFMT_RGB_8_GBGB
,
119 .name
= "12 bpp YUV411",
120 .fourcc
= V4L2_PIX_FMT_YUV411P
,
122 .reg
= EM28XX_OUTFMT_YUV411
,
126 /* supported controls */
127 /* Common to all boards */
128 static struct v4l2_queryctrl ac97_qctrl
[] = {
130 .id
= V4L2_CID_AUDIO_VOLUME
,
131 .type
= V4L2_CTRL_TYPE_INTEGER
,
136 .default_value
= 0x1f,
137 .flags
= V4L2_CTRL_FLAG_SLIDER
,
139 .id
= V4L2_CID_AUDIO_MUTE
,
140 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
150 /* ------------------------------------------------------------------
151 DMA and thread functions
152 ------------------------------------------------------------------*/
155 * Announces that a buffer were filled and request the next
157 static inline void buffer_filled(struct em28xx
*dev
,
158 struct em28xx_dmaqueue
*dma_q
,
159 struct em28xx_buffer
*buf
)
161 /* Advice that buffer was filled */
162 em28xx_isocdbg("[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
163 buf
->vb
.state
= VIDEOBUF_DONE
;
164 buf
->vb
.field_count
++;
165 do_gettimeofday(&buf
->vb
.ts
);
167 dev
->isoc_ctl
.vid_buf
= NULL
;
169 list_del(&buf
->vb
.queue
);
170 wake_up(&buf
->vb
.done
);
173 static inline void vbi_buffer_filled(struct em28xx
*dev
,
174 struct em28xx_dmaqueue
*dma_q
,
175 struct em28xx_buffer
*buf
)
177 /* Advice that buffer was filled */
178 em28xx_isocdbg("[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
180 buf
->vb
.state
= VIDEOBUF_DONE
;
181 buf
->vb
.field_count
++;
182 do_gettimeofday(&buf
->vb
.ts
);
184 dev
->isoc_ctl
.vbi_buf
= NULL
;
186 list_del(&buf
->vb
.queue
);
187 wake_up(&buf
->vb
.done
);
191 * Identify the buffer header type and properly handles
193 static void em28xx_copy_video(struct em28xx
*dev
,
194 struct em28xx_dmaqueue
*dma_q
,
195 struct em28xx_buffer
*buf
,
197 unsigned char *outp
, unsigned long len
)
199 void *fieldstart
, *startwrite
, *startread
;
200 int linesdone
, currlinedone
, offset
, lencopy
, remain
;
201 int bytesperline
= dev
->width
<< 1;
203 if (dma_q
->pos
+ len
> buf
->vb
.size
)
204 len
= buf
->vb
.size
- dma_q
->pos
;
206 if (p
[0] != 0x88 && p
[0] != 0x22) {
207 em28xx_isocdbg("frame is not complete\n");
215 if (dev
->progressive
)
218 /* Interlaces two half frames */
222 fieldstart
= outp
+ bytesperline
;
225 linesdone
= dma_q
->pos
/ bytesperline
;
226 currlinedone
= dma_q
->pos
% bytesperline
;
228 if (dev
->progressive
)
229 offset
= linesdone
* bytesperline
+ currlinedone
;
231 offset
= linesdone
* bytesperline
* 2 + currlinedone
;
233 startwrite
= fieldstart
+ offset
;
234 lencopy
= bytesperline
- currlinedone
;
235 lencopy
= lencopy
> remain
? remain
: lencopy
;
237 if ((char *)startwrite
+ lencopy
> (char *)outp
+ buf
->vb
.size
) {
238 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
239 ((char *)startwrite
+ lencopy
) -
240 ((char *)outp
+ buf
->vb
.size
));
241 remain
= (char *)outp
+ buf
->vb
.size
- (char *)startwrite
;
246 memcpy(startwrite
, startread
, lencopy
);
251 startwrite
+= lencopy
+ bytesperline
;
252 startread
+= lencopy
;
253 if (bytesperline
> remain
)
256 lencopy
= bytesperline
;
258 if ((char *)startwrite
+ lencopy
> (char *)outp
+
260 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
262 ((char *)startwrite
+ lencopy
) -
263 ((char *)outp
+ buf
->vb
.size
));
264 lencopy
= remain
= (char *)outp
+ buf
->vb
.size
-
270 memcpy(startwrite
, startread
, lencopy
);
278 static void em28xx_copy_vbi(struct em28xx
*dev
,
279 struct em28xx_dmaqueue
*dma_q
,
280 struct em28xx_buffer
*buf
,
282 unsigned char *outp
, unsigned long len
)
284 void *startwrite
, *startread
;
286 int bytesperline
= dev
->vbi_width
;
289 em28xx_isocdbg("dev is null\n");
294 em28xx_isocdbg("dma_q is null\n");
301 em28xx_isocdbg("p is null\n");
305 em28xx_isocdbg("outp is null\n");
309 if (dma_q
->pos
+ len
> buf
->vb
.size
)
310 len
= buf
->vb
.size
- dma_q
->pos
;
312 if ((p
[0] == 0x33 && p
[1] == 0x95) ||
313 (p
[0] == 0x88 && p
[1] == 0x88)) {
314 /* Header field, advance past it */
322 startwrite
= outp
+ dma_q
->pos
;
325 /* Make sure the bottom field populates the second half of the frame */
326 if (buf
->top_field
== 0) {
327 startwrite
+= bytesperline
* dev
->vbi_height
;
328 offset
+= bytesperline
* dev
->vbi_height
;
331 memcpy(startwrite
, startread
, len
);
335 static inline void print_err_status(struct em28xx
*dev
,
336 int packet
, int status
)
338 char *errmsg
= "Unknown";
342 errmsg
= "unlinked synchronuously";
345 errmsg
= "unlinked asynchronuously";
348 errmsg
= "Buffer error (overrun)";
351 errmsg
= "Stalled (device not responding)";
354 errmsg
= "Babble (bad cable?)";
357 errmsg
= "Bit-stuff error (bad cable?)";
360 errmsg
= "CRC/Timeout (could be anything)";
363 errmsg
= "Device does not respond";
367 em28xx_isocdbg("URB status %d [%s].\n", status
, errmsg
);
369 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
370 packet
, status
, errmsg
);
375 * video-buf generic routine to get the next available buffer
377 static inline void get_next_buf(struct em28xx_dmaqueue
*dma_q
,
378 struct em28xx_buffer
**buf
)
380 struct em28xx
*dev
= container_of(dma_q
, struct em28xx
, vidq
);
383 if (list_empty(&dma_q
->active
)) {
384 em28xx_isocdbg("No active queue to serve\n");
385 dev
->isoc_ctl
.vid_buf
= NULL
;
390 /* Get the next buffer */
391 *buf
= list_entry(dma_q
->active
.next
, struct em28xx_buffer
, vb
.queue
);
393 /* Cleans up buffer - Usefull for testing for frame/URB loss */
394 outp
= videobuf_to_vmalloc(&(*buf
)->vb
);
395 memset(outp
, 0, (*buf
)->vb
.size
);
397 dev
->isoc_ctl
.vid_buf
= *buf
;
403 * video-buf generic routine to get the next available VBI buffer
405 static inline void vbi_get_next_buf(struct em28xx_dmaqueue
*dma_q
,
406 struct em28xx_buffer
**buf
)
408 struct em28xx
*dev
= container_of(dma_q
, struct em28xx
, vbiq
);
411 if (list_empty(&dma_q
->active
)) {
412 em28xx_isocdbg("No active queue to serve\n");
413 dev
->isoc_ctl
.vbi_buf
= NULL
;
418 /* Get the next buffer */
419 *buf
= list_entry(dma_q
->active
.next
, struct em28xx_buffer
, vb
.queue
);
420 /* Cleans up buffer - Usefull for testing for frame/URB loss */
421 outp
= videobuf_to_vmalloc(&(*buf
)->vb
);
422 memset(outp
, 0x00, (*buf
)->vb
.size
);
424 dev
->isoc_ctl
.vbi_buf
= *buf
;
430 * Controls the isoc copy of each urb packet
432 static inline int em28xx_isoc_copy(struct em28xx
*dev
, struct urb
*urb
)
434 struct em28xx_buffer
*buf
;
435 struct em28xx_dmaqueue
*dma_q
= &dev
->vidq
;
436 unsigned char *outp
= NULL
;
437 int i
, len
= 0, rc
= 1;
443 if ((dev
->state
& DEV_DISCONNECTED
) || (dev
->state
& DEV_MISCONFIGURED
))
446 if (urb
->status
< 0) {
447 print_err_status(dev
, -1, urb
->status
);
448 if (urb
->status
== -ENOENT
)
452 buf
= dev
->isoc_ctl
.vid_buf
;
454 outp
= videobuf_to_vmalloc(&buf
->vb
);
456 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
457 int status
= urb
->iso_frame_desc
[i
].status
;
460 print_err_status(dev
, i
, status
);
461 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
465 len
= urb
->iso_frame_desc
[i
].actual_length
- 4;
467 if (urb
->iso_frame_desc
[i
].actual_length
<= 0) {
468 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
471 if (urb
->iso_frame_desc
[i
].actual_length
>
473 em28xx_isocdbg("packet bigger than packet size");
477 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
479 /* FIXME: incomplete buffer checks where removed to make
480 logic simpler. Impacts of those changes should be evaluated
482 if (p
[0] == 0x33 && p
[1] == 0x95 && p
[2] == 0x00) {
483 em28xx_isocdbg("VBI HEADER!!!\n");
484 /* FIXME: Should add vbi copy */
487 if (p
[0] == 0x22 && p
[1] == 0x5a) {
488 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p
[2],
489 len
, (p
[2] & 1) ? "odd" : "even");
491 if (dev
->progressive
|| !(p
[2] & 1)) {
493 buffer_filled(dev
, dma_q
, buf
);
494 get_next_buf(dma_q
, &buf
);
498 outp
= videobuf_to_vmalloc(&buf
->vb
);
511 em28xx_copy_video(dev
, dma_q
, buf
, p
, outp
, len
);
516 /* Version of isoc handler that takes into account a mixture of video and
518 static inline int em28xx_isoc_copy_vbi(struct em28xx
*dev
, struct urb
*urb
)
520 struct em28xx_buffer
*buf
, *vbi_buf
;
521 struct em28xx_dmaqueue
*dma_q
= &dev
->vidq
;
522 struct em28xx_dmaqueue
*vbi_dma_q
= &dev
->vbiq
;
523 unsigned char *outp
= NULL
;
524 unsigned char *vbioutp
= NULL
;
525 int i
, len
= 0, rc
= 1;
532 if ((dev
->state
& DEV_DISCONNECTED
) || (dev
->state
& DEV_MISCONFIGURED
))
535 if (urb
->status
< 0) {
536 print_err_status(dev
, -1, urb
->status
);
537 if (urb
->status
== -ENOENT
)
541 buf
= dev
->isoc_ctl
.vid_buf
;
543 outp
= videobuf_to_vmalloc(&buf
->vb
);
545 vbi_buf
= dev
->isoc_ctl
.vbi_buf
;
547 vbioutp
= videobuf_to_vmalloc(&vbi_buf
->vb
);
549 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
550 int status
= urb
->iso_frame_desc
[i
].status
;
553 print_err_status(dev
, i
, status
);
554 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
558 len
= urb
->iso_frame_desc
[i
].actual_length
- 4;
560 if (urb
->iso_frame_desc
[i
].actual_length
<= 0) {
561 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
564 if (urb
->iso_frame_desc
[i
].actual_length
>
566 em28xx_isocdbg("packet bigger than packet size");
570 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
572 /* capture type 0 = vbi start
573 capture type 1 = video start
574 capture type 2 = video in progress */
575 if (p
[0] == 0x33 && p
[1] == 0x95) {
576 dev
->capture_type
= 0;
578 em28xx_isocdbg("VBI START HEADER!!!\n");
579 dev
->cur_field
= p
[2];
582 vbi_size
= dev
->vbi_width
* dev
->vbi_height
;
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;
1064 /* width must even because of the YUYV format
1065 height must be even because of interlacing */
1066 v4l_bound_align_image(&width
, 48, maxw
, 1, &height
, 32, maxh
,
1070 get_scale(dev
, width
, height
, &hscale
, &vscale
);
1072 width
= (((unsigned long)maxw
) << 12) / (hscale
+ 4096L);
1073 height
= (((unsigned long)maxh
) << 12) / (vscale
+ 4096L);
1075 f
->fmt
.pix
.width
= width
;
1076 f
->fmt
.pix
.height
= height
;
1077 f
->fmt
.pix
.pixelformat
= fmt
->fourcc
;
1078 f
->fmt
.pix
.bytesperline
= (dev
->width
* fmt
->depth
+ 7) >> 3;
1079 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
* height
;
1080 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1081 if (dev
->progressive
)
1082 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
1084 f
->fmt
.pix
.field
= dev
->interlaced
?
1085 V4L2_FIELD_INTERLACED
: V4L2_FIELD_TOP
;
1090 static int em28xx_set_video_format(struct em28xx
*dev
, unsigned int fourcc
,
1091 unsigned width
, unsigned height
)
1093 struct em28xx_fmt
*fmt
;
1095 fmt
= format_by_fourcc(fourcc
);
1101 dev
->height
= height
;
1103 /* set new image size */
1104 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
1106 em28xx_set_alternate(dev
);
1107 em28xx_resolution_set(dev
);
1112 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1113 struct v4l2_format
*f
)
1115 struct em28xx_fh
*fh
= priv
;
1116 struct em28xx
*dev
= fh
->dev
;
1119 rc
= check_dev(dev
);
1123 mutex_lock(&dev
->lock
);
1125 vidioc_try_fmt_vid_cap(file
, priv
, f
);
1127 if (videobuf_queue_is_busy(&fh
->vb_vidq
)) {
1128 em28xx_errdev("%s queue busy\n", __func__
);
1133 rc
= em28xx_set_video_format(dev
, f
->fmt
.pix
.pixelformat
,
1134 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
);
1137 mutex_unlock(&dev
->lock
);
1141 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
1143 struct em28xx_fh
*fh
= priv
;
1144 struct em28xx
*dev
= fh
->dev
;
1147 rc
= check_dev(dev
);
1156 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
1158 struct em28xx_fh
*fh
= priv
;
1159 struct em28xx
*dev
= fh
->dev
;
1160 struct v4l2_format f
;
1163 rc
= check_dev(dev
);
1167 mutex_lock(&dev
->lock
);
1170 /* Adjusts width/height, if needed */
1171 f
.fmt
.pix
.width
= dev
->width
;
1172 f
.fmt
.pix
.height
= dev
->height
;
1173 vidioc_try_fmt_vid_cap(file
, priv
, &f
);
1175 /* set new image size */
1176 dev
->width
= f
.fmt
.pix
.width
;
1177 dev
->height
= f
.fmt
.pix
.height
;
1178 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
1180 em28xx_resolution_set(dev
);
1181 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_std
, dev
->norm
);
1183 mutex_unlock(&dev
->lock
);
1187 static int vidioc_g_parm(struct file
*file
, void *priv
,
1188 struct v4l2_streamparm
*p
)
1190 struct em28xx_fh
*fh
= priv
;
1191 struct em28xx
*dev
= fh
->dev
;
1194 if (p
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1197 if (dev
->board
.is_webcam
)
1198 rc
= v4l2_device_call_until_err(&dev
->v4l2_dev
, 0,
1201 v4l2_video_std_frame_period(dev
->norm
,
1202 &p
->parm
.capture
.timeperframe
);
1207 static int vidioc_s_parm(struct file
*file
, void *priv
,
1208 struct v4l2_streamparm
*p
)
1210 struct em28xx_fh
*fh
= priv
;
1211 struct em28xx
*dev
= fh
->dev
;
1213 if (!dev
->board
.is_webcam
)
1216 if (p
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1219 return v4l2_device_call_until_err(&dev
->v4l2_dev
, 0, video
, s_parm
, p
);
1222 static const char *iname
[] = {
1223 [EM28XX_VMUX_COMPOSITE1
] = "Composite1",
1224 [EM28XX_VMUX_COMPOSITE2
] = "Composite2",
1225 [EM28XX_VMUX_COMPOSITE3
] = "Composite3",
1226 [EM28XX_VMUX_COMPOSITE4
] = "Composite4",
1227 [EM28XX_VMUX_SVIDEO
] = "S-Video",
1228 [EM28XX_VMUX_TELEVISION
] = "Television",
1229 [EM28XX_VMUX_CABLE
] = "Cable TV",
1230 [EM28XX_VMUX_DVB
] = "DVB",
1231 [EM28XX_VMUX_DEBUG
] = "for debug only",
1234 static int vidioc_enum_input(struct file
*file
, void *priv
,
1235 struct v4l2_input
*i
)
1237 struct em28xx_fh
*fh
= priv
;
1238 struct em28xx
*dev
= fh
->dev
;
1242 if (n
>= MAX_EM28XX_INPUT
)
1244 if (0 == INPUT(n
)->type
)
1248 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1250 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
1252 if ((EM28XX_VMUX_TELEVISION
== INPUT(n
)->type
) ||
1253 (EM28XX_VMUX_CABLE
== INPUT(n
)->type
))
1254 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1256 i
->std
= dev
->vdev
->tvnorms
;
1261 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1263 struct em28xx_fh
*fh
= priv
;
1264 struct em28xx
*dev
= fh
->dev
;
1266 *i
= dev
->ctl_input
;
1271 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1273 struct em28xx_fh
*fh
= priv
;
1274 struct em28xx
*dev
= fh
->dev
;
1277 rc
= check_dev(dev
);
1281 if (i
>= MAX_EM28XX_INPUT
)
1283 if (0 == INPUT(i
)->type
)
1288 mutex_lock(&dev
->lock
);
1289 video_mux(dev
, dev
->ctl_input
);
1290 mutex_unlock(&dev
->lock
);
1294 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1296 struct em28xx_fh
*fh
= priv
;
1297 struct em28xx
*dev
= fh
->dev
;
1299 if (!dev
->audio_mode
.has_audio
)
1303 case EM28XX_AMUX_VIDEO
:
1304 strcpy(a
->name
, "Television");
1306 case EM28XX_AMUX_LINE_IN
:
1307 strcpy(a
->name
, "Line In");
1309 case EM28XX_AMUX_VIDEO2
:
1310 strcpy(a
->name
, "Television alt");
1312 case EM28XX_AMUX_PHONE
:
1313 strcpy(a
->name
, "Phone");
1315 case EM28XX_AMUX_MIC
:
1316 strcpy(a
->name
, "Mic");
1318 case EM28XX_AMUX_CD
:
1319 strcpy(a
->name
, "CD");
1321 case EM28XX_AMUX_AUX
:
1322 strcpy(a
->name
, "Aux");
1324 case EM28XX_AMUX_PCM_OUT
:
1325 strcpy(a
->name
, "PCM");
1331 a
->index
= dev
->ctl_ainput
;
1332 a
->capability
= V4L2_AUDCAP_STEREO
;
1337 static int vidioc_s_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1339 struct em28xx_fh
*fh
= priv
;
1340 struct em28xx
*dev
= fh
->dev
;
1343 if (!dev
->audio_mode
.has_audio
)
1346 if (a
->index
>= MAX_EM28XX_INPUT
)
1348 if (0 == INPUT(a
->index
)->type
)
1351 mutex_lock(&dev
->lock
);
1353 dev
->ctl_ainput
= INPUT(a
->index
)->amux
;
1354 dev
->ctl_aoutput
= INPUT(a
->index
)->aout
;
1356 if (!dev
->ctl_aoutput
)
1357 dev
->ctl_aoutput
= EM28XX_AOUT_MASTER
;
1359 mutex_unlock(&dev
->lock
);
1363 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1364 struct v4l2_queryctrl
*qc
)
1366 struct em28xx_fh
*fh
= priv
;
1367 struct em28xx
*dev
= fh
->dev
;
1371 rc
= check_dev(dev
);
1375 memset(qc
, 0, sizeof(*qc
));
1379 /* enumberate AC97 controls */
1380 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
) {
1381 rc
= ac97_queryctrl(qc
);
1386 /* enumberate V4L2 device controls */
1387 mutex_lock(&dev
->lock
);
1388 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, queryctrl
, qc
);
1389 mutex_unlock(&dev
->lock
);
1397 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1398 struct v4l2_control
*ctrl
)
1400 struct em28xx_fh
*fh
= priv
;
1401 struct em28xx
*dev
= fh
->dev
;
1404 rc
= check_dev(dev
);
1409 mutex_lock(&dev
->lock
);
1411 /* Set an AC97 control */
1412 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
)
1413 rc
= ac97_get_ctrl(dev
, ctrl
);
1417 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1419 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_ctrl
, ctrl
);
1423 mutex_unlock(&dev
->lock
);
1427 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1428 struct v4l2_control
*ctrl
)
1430 struct em28xx_fh
*fh
= priv
;
1431 struct em28xx
*dev
= fh
->dev
;
1434 rc
= check_dev(dev
);
1438 mutex_lock(&dev
->lock
);
1440 /* Set an AC97 control */
1441 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
)
1442 rc
= ac97_set_ctrl(dev
, ctrl
);
1446 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
1448 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_ctrl
, ctrl
);
1451 * In the case of non-AC97 volume controls, we still need
1452 * to do some setups at em28xx, in order to mute/unmute
1453 * and to adjust audio volume. However, the value ranges
1454 * should be checked by the corresponding V4L subdriver.
1457 case V4L2_CID_AUDIO_MUTE
:
1458 dev
->mute
= ctrl
->value
;
1459 rc
= em28xx_audio_analog_set(dev
);
1461 case V4L2_CID_AUDIO_VOLUME
:
1462 dev
->volume
= ctrl
->value
;
1463 rc
= em28xx_audio_analog_set(dev
);
1467 mutex_unlock(&dev
->lock
);
1471 static int vidioc_g_tuner(struct file
*file
, void *priv
,
1472 struct v4l2_tuner
*t
)
1474 struct em28xx_fh
*fh
= priv
;
1475 struct em28xx
*dev
= fh
->dev
;
1478 rc
= check_dev(dev
);
1485 strcpy(t
->name
, "Tuner");
1487 mutex_lock(&dev
->lock
);
1488 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1489 mutex_unlock(&dev
->lock
);
1494 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1495 struct v4l2_tuner
*t
)
1497 struct em28xx_fh
*fh
= priv
;
1498 struct em28xx
*dev
= fh
->dev
;
1501 rc
= check_dev(dev
);
1508 mutex_lock(&dev
->lock
);
1509 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
1510 mutex_unlock(&dev
->lock
);
1515 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1516 struct v4l2_frequency
*f
)
1518 struct em28xx_fh
*fh
= priv
;
1519 struct em28xx
*dev
= fh
->dev
;
1521 mutex_lock(&dev
->lock
);
1522 f
->type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1523 f
->frequency
= dev
->ctl_freq
;
1524 mutex_unlock(&dev
->lock
);
1529 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1530 struct v4l2_frequency
*f
)
1532 struct em28xx_fh
*fh
= priv
;
1533 struct em28xx
*dev
= fh
->dev
;
1536 rc
= check_dev(dev
);
1543 if (unlikely(0 == fh
->radio
&& f
->type
!= V4L2_TUNER_ANALOG_TV
))
1545 if (unlikely(1 == fh
->radio
&& f
->type
!= V4L2_TUNER_RADIO
))
1548 mutex_lock(&dev
->lock
);
1550 dev
->ctl_freq
= f
->frequency
;
1551 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, f
);
1553 mutex_unlock(&dev
->lock
);
1558 #ifdef CONFIG_VIDEO_ADV_DEBUG
1559 static int em28xx_reg_len(int reg
)
1562 case EM28XX_R40_AC97LSB
:
1563 case EM28XX_R30_HSCALELOW
:
1564 case EM28XX_R32_VSCALELOW
:
1571 static int vidioc_g_chip_ident(struct file
*file
, void *priv
,
1572 struct v4l2_dbg_chip_ident
*chip
)
1574 struct em28xx_fh
*fh
= priv
;
1575 struct em28xx
*dev
= fh
->dev
;
1577 chip
->ident
= V4L2_IDENT_NONE
;
1580 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_chip_ident
, chip
);
1586 static int vidioc_g_register(struct file
*file
, void *priv
,
1587 struct v4l2_dbg_register
*reg
)
1589 struct em28xx_fh
*fh
= priv
;
1590 struct em28xx
*dev
= fh
->dev
;
1593 switch (reg
->match
.type
) {
1594 case V4L2_CHIP_MATCH_AC97
:
1595 mutex_lock(&dev
->lock
);
1596 ret
= em28xx_read_ac97(dev
, reg
->reg
);
1597 mutex_unlock(&dev
->lock
);
1604 case V4L2_CHIP_MATCH_I2C_DRIVER
:
1605 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_register
, reg
);
1607 case V4L2_CHIP_MATCH_I2C_ADDR
:
1608 /* TODO: is this correct? */
1609 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_register
, reg
);
1612 if (!v4l2_chip_match_host(®
->match
))
1617 reg
->size
= em28xx_reg_len(reg
->reg
);
1618 if (reg
->size
== 1) {
1619 mutex_lock(&dev
->lock
);
1620 ret
= em28xx_read_reg(dev
, reg
->reg
);
1621 mutex_unlock(&dev
->lock
);
1629 mutex_lock(&dev
->lock
);
1630 ret
= em28xx_read_reg_req_len(dev
, USB_REQ_GET_STATUS
,
1631 reg
->reg
, (char *)&val
, 2);
1632 mutex_unlock(&dev
->lock
);
1636 reg
->val
= le16_to_cpu(val
);
1642 static int vidioc_s_register(struct file
*file
, void *priv
,
1643 struct v4l2_dbg_register
*reg
)
1645 struct em28xx_fh
*fh
= priv
;
1646 struct em28xx
*dev
= fh
->dev
;
1650 switch (reg
->match
.type
) {
1651 case V4L2_CHIP_MATCH_AC97
:
1652 mutex_lock(&dev
->lock
);
1653 rc
= em28xx_write_ac97(dev
, reg
->reg
, reg
->val
);
1654 mutex_unlock(&dev
->lock
);
1657 case V4L2_CHIP_MATCH_I2C_DRIVER
:
1658 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_register
, reg
);
1660 case V4L2_CHIP_MATCH_I2C_ADDR
:
1661 /* TODO: is this correct? */
1662 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_register
, reg
);
1665 if (!v4l2_chip_match_host(®
->match
))
1670 buf
= cpu_to_le16(reg
->val
);
1672 mutex_lock(&dev
->lock
);
1673 rc
= em28xx_write_regs(dev
, reg
->reg
, (char *)&buf
,
1674 em28xx_reg_len(reg
->reg
));
1675 mutex_unlock(&dev
->lock
);
1682 static int vidioc_cropcap(struct file
*file
, void *priv
,
1683 struct v4l2_cropcap
*cc
)
1685 struct em28xx_fh
*fh
= priv
;
1686 struct em28xx
*dev
= fh
->dev
;
1688 if (cc
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1691 cc
->bounds
.left
= 0;
1693 cc
->bounds
.width
= dev
->width
;
1694 cc
->bounds
.height
= dev
->height
;
1695 cc
->defrect
= cc
->bounds
;
1696 cc
->pixelaspect
.numerator
= 54; /* 4:3 FIXME: remove magic numbers */
1697 cc
->pixelaspect
.denominator
= 59;
1702 static int vidioc_streamon(struct file
*file
, void *priv
,
1703 enum v4l2_buf_type type
)
1705 struct em28xx_fh
*fh
= priv
;
1706 struct em28xx
*dev
= fh
->dev
;
1709 rc
= check_dev(dev
);
1713 if (unlikely(type
!= fh
->type
))
1716 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1717 fh
, type
, fh
->resources
, dev
->resources
);
1719 if (unlikely(!res_get(fh
, get_ressource(fh
))))
1722 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1723 rc
= videobuf_streamon(&fh
->vb_vidq
);
1724 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1725 rc
= videobuf_streamon(&fh
->vb_vbiq
);
1730 static int vidioc_streamoff(struct file
*file
, void *priv
,
1731 enum v4l2_buf_type type
)
1733 struct em28xx_fh
*fh
= priv
;
1734 struct em28xx
*dev
= fh
->dev
;
1737 rc
= check_dev(dev
);
1741 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1742 fh
->type
!= V4L2_BUF_TYPE_VBI_CAPTURE
)
1744 if (type
!= fh
->type
)
1747 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1748 fh
, type
, fh
->resources
, dev
->resources
);
1750 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1751 videobuf_streamoff(&fh
->vb_vidq
);
1752 res_free(fh
, EM28XX_RESOURCE_VIDEO
);
1753 } else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1754 videobuf_streamoff(&fh
->vb_vbiq
);
1755 res_free(fh
, EM28XX_RESOURCE_VBI
);
1761 static int vidioc_querycap(struct file
*file
, void *priv
,
1762 struct v4l2_capability
*cap
)
1764 struct em28xx_fh
*fh
= priv
;
1765 struct em28xx
*dev
= fh
->dev
;
1767 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
1768 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
, sizeof(cap
->card
));
1769 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
1771 cap
->version
= EM28XX_VERSION_CODE
;
1774 V4L2_CAP_SLICED_VBI_CAPTURE
|
1775 V4L2_CAP_VIDEO_CAPTURE
|
1776 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1779 cap
->capabilities
|= V4L2_CAP_VBI_CAPTURE
;
1781 if (dev
->audio_mode
.has_audio
)
1782 cap
->capabilities
|= V4L2_CAP_AUDIO
;
1784 if (dev
->tuner_type
!= TUNER_ABSENT
)
1785 cap
->capabilities
|= V4L2_CAP_TUNER
;
1790 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1791 struct v4l2_fmtdesc
*f
)
1793 if (unlikely(f
->index
>= ARRAY_SIZE(format
)))
1796 strlcpy(f
->description
, format
[f
->index
].name
, sizeof(f
->description
));
1797 f
->pixelformat
= format
[f
->index
].fourcc
;
1802 /* Sliced VBI ioctls */
1803 static int vidioc_g_fmt_sliced_vbi_cap(struct file
*file
, void *priv
,
1804 struct v4l2_format
*f
)
1806 struct em28xx_fh
*fh
= priv
;
1807 struct em28xx
*dev
= fh
->dev
;
1810 rc
= check_dev(dev
);
1814 mutex_lock(&dev
->lock
);
1816 f
->fmt
.sliced
.service_set
= 0;
1817 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, g_fmt
, f
);
1819 if (f
->fmt
.sliced
.service_set
== 0)
1822 mutex_unlock(&dev
->lock
);
1827 static int vidioc_try_set_sliced_vbi_cap(struct file
*file
, void *priv
,
1828 struct v4l2_format
*f
)
1830 struct em28xx_fh
*fh
= priv
;
1831 struct em28xx
*dev
= fh
->dev
;
1834 rc
= check_dev(dev
);
1838 mutex_lock(&dev
->lock
);
1839 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, g_fmt
, f
);
1840 mutex_unlock(&dev
->lock
);
1842 if (f
->fmt
.sliced
.service_set
== 0)
1848 /* RAW VBI ioctls */
1850 static int vidioc_g_fmt_vbi_cap(struct file
*file
, void *priv
,
1851 struct v4l2_format
*format
)
1853 struct em28xx_fh
*fh
= priv
;
1854 struct em28xx
*dev
= fh
->dev
;
1856 format
->fmt
.vbi
.samples_per_line
= dev
->vbi_width
;
1857 format
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1858 format
->fmt
.vbi
.offset
= 0;
1859 format
->fmt
.vbi
.flags
= 0;
1860 format
->fmt
.vbi
.sampling_rate
= 6750000 * 4 / 2;
1861 format
->fmt
.vbi
.count
[0] = dev
->vbi_height
;
1862 format
->fmt
.vbi
.count
[1] = dev
->vbi_height
;
1864 /* Varies by video standard (NTSC, PAL, etc.) */
1865 if (dev
->norm
& V4L2_STD_525_60
) {
1867 format
->fmt
.vbi
.start
[0] = 10;
1868 format
->fmt
.vbi
.start
[1] = 273;
1869 } else if (dev
->norm
& V4L2_STD_625_50
) {
1871 format
->fmt
.vbi
.start
[0] = 6;
1872 format
->fmt
.vbi
.start
[1] = 318;
1878 static int vidioc_s_fmt_vbi_cap(struct file
*file
, void *priv
,
1879 struct v4l2_format
*format
)
1881 struct em28xx_fh
*fh
= priv
;
1882 struct em28xx
*dev
= fh
->dev
;
1884 format
->fmt
.vbi
.samples_per_line
= dev
->vbi_width
;
1885 format
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1886 format
->fmt
.vbi
.offset
= 0;
1887 format
->fmt
.vbi
.flags
= 0;
1888 format
->fmt
.vbi
.sampling_rate
= 6750000 * 4 / 2;
1889 format
->fmt
.vbi
.count
[0] = dev
->vbi_height
;
1890 format
->fmt
.vbi
.count
[1] = dev
->vbi_height
;
1892 /* Varies by video standard (NTSC, PAL, etc.) */
1893 if (dev
->norm
& V4L2_STD_525_60
) {
1895 format
->fmt
.vbi
.start
[0] = 10;
1896 format
->fmt
.vbi
.start
[1] = 273;
1897 } else if (dev
->norm
& V4L2_STD_625_50
) {
1899 format
->fmt
.vbi
.start
[0] = 6;
1900 format
->fmt
.vbi
.start
[1] = 318;
1906 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1907 struct v4l2_requestbuffers
*rb
)
1909 struct em28xx_fh
*fh
= priv
;
1910 struct em28xx
*dev
= fh
->dev
;
1913 rc
= check_dev(dev
);
1917 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1918 return videobuf_reqbufs(&fh
->vb_vidq
, rb
);
1920 return videobuf_reqbufs(&fh
->vb_vbiq
, rb
);
1923 static int vidioc_querybuf(struct file
*file
, void *priv
,
1924 struct v4l2_buffer
*b
)
1926 struct em28xx_fh
*fh
= priv
;
1927 struct em28xx
*dev
= fh
->dev
;
1930 rc
= check_dev(dev
);
1934 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1935 return videobuf_querybuf(&fh
->vb_vidq
, b
);
1937 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1938 the videobuf framework, but we probably shouldn't be
1939 returning a buffer larger than that which was asked for.
1940 At a minimum, it causes a crash in zvbi since it does
1941 a memcpy based on the source buffer length */
1942 int result
= videobuf_querybuf(&fh
->vb_vbiq
, b
);
1943 b
->length
= dev
->vbi_width
* dev
->vbi_height
* 2;
1949 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1951 struct em28xx_fh
*fh
= priv
;
1952 struct em28xx
*dev
= fh
->dev
;
1955 rc
= check_dev(dev
);
1959 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1960 return videobuf_qbuf(&fh
->vb_vidq
, b
);
1962 return videobuf_qbuf(&fh
->vb_vbiq
, b
);
1965 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1967 struct em28xx_fh
*fh
= priv
;
1968 struct em28xx
*dev
= fh
->dev
;
1971 rc
= check_dev(dev
);
1975 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1976 return videobuf_dqbuf(&fh
->vb_vidq
, b
, file
->f_flags
&
1979 return videobuf_dqbuf(&fh
->vb_vbiq
, b
, file
->f_flags
&
1983 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1984 static int vidiocgmbuf(struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
1986 struct em28xx_fh
*fh
= priv
;
1988 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1989 return videobuf_cgmbuf(&fh
->vb_vidq
, mbuf
, 8);
1991 return videobuf_cgmbuf(&fh
->vb_vbiq
, mbuf
, 8);
1996 /* ----------------------------------------------------------- */
1997 /* RADIO ESPECIFIC IOCTLS */
1998 /* ----------------------------------------------------------- */
2000 static int radio_querycap(struct file
*file
, void *priv
,
2001 struct v4l2_capability
*cap
)
2003 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
2005 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
2006 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
, sizeof(cap
->card
));
2007 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
2009 cap
->version
= EM28XX_VERSION_CODE
;
2010 cap
->capabilities
= V4L2_CAP_TUNER
;
2014 static int radio_g_tuner(struct file
*file
, void *priv
,
2015 struct v4l2_tuner
*t
)
2017 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
2019 if (unlikely(t
->index
> 0))
2022 strcpy(t
->name
, "Radio");
2023 t
->type
= V4L2_TUNER_RADIO
;
2025 mutex_lock(&dev
->lock
);
2026 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
2027 mutex_unlock(&dev
->lock
);
2032 static int radio_enum_input(struct file
*file
, void *priv
,
2033 struct v4l2_input
*i
)
2037 strcpy(i
->name
, "Radio");
2038 i
->type
= V4L2_INPUT_TYPE_TUNER
;
2043 static int radio_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
2045 if (unlikely(a
->index
))
2048 strcpy(a
->name
, "Radio");
2052 static int radio_s_tuner(struct file
*file
, void *priv
,
2053 struct v4l2_tuner
*t
)
2055 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
2060 mutex_lock(&dev
->lock
);
2061 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
2062 mutex_unlock(&dev
->lock
);
2067 static int radio_s_audio(struct file
*file
, void *fh
,
2068 struct v4l2_audio
*a
)
2073 static int radio_s_input(struct file
*file
, void *fh
, unsigned int i
)
2078 static int radio_queryctrl(struct file
*file
, void *priv
,
2079 struct v4l2_queryctrl
*qc
)
2083 if (qc
->id
< V4L2_CID_BASE
||
2084 qc
->id
>= V4L2_CID_LASTP1
)
2087 for (i
= 0; i
< ARRAY_SIZE(ac97_qctrl
); i
++) {
2088 if (qc
->id
&& qc
->id
== ac97_qctrl
[i
].id
) {
2089 memcpy(qc
, &(ac97_qctrl
[i
]), sizeof(*qc
));
2098 * em28xx_v4l2_open()
2099 * inits the device and starts isoc transfer
2101 static int em28xx_v4l2_open(struct file
*filp
)
2103 int errCode
= 0, radio
= 0;
2104 struct video_device
*vdev
= video_devdata(filp
);
2105 struct em28xx
*dev
= video_drvdata(filp
);
2106 enum v4l2_buf_type fh_type
= 0;
2107 struct em28xx_fh
*fh
;
2108 enum v4l2_field field
;
2110 switch (vdev
->vfl_type
) {
2111 case VFL_TYPE_GRABBER
:
2112 fh_type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2115 fh_type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
2117 case VFL_TYPE_RADIO
:
2122 mutex_lock(&dev
->lock
);
2124 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2125 video_device_node_name(vdev
), v4l2_type_names
[fh_type
],
2129 fh
= kzalloc(sizeof(struct em28xx_fh
), GFP_KERNEL
);
2131 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
2132 mutex_unlock(&dev
->lock
);
2138 filp
->private_data
= fh
;
2140 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&& dev
->users
== 0) {
2141 em28xx_set_mode(dev
, EM28XX_ANALOG_MODE
);
2142 em28xx_set_alternate(dev
);
2143 em28xx_resolution_set(dev
);
2145 /* Needed, since GPIO might have disabled power of
2148 em28xx_wake_i2c(dev
);
2152 em28xx_videodbg("video_open: setting radio device\n");
2153 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_radio
);
2158 if (dev
->progressive
)
2159 field
= V4L2_FIELD_NONE
;
2161 field
= V4L2_FIELD_INTERLACED
;
2163 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &em28xx_video_qops
,
2165 V4L2_BUF_TYPE_VIDEO_CAPTURE
, field
,
2166 sizeof(struct em28xx_buffer
), fh
);
2168 videobuf_queue_vmalloc_init(&fh
->vb_vbiq
, &em28xx_vbi_qops
,
2170 V4L2_BUF_TYPE_VBI_CAPTURE
,
2172 sizeof(struct em28xx_buffer
), fh
);
2174 mutex_unlock(&dev
->lock
);
2180 * em28xx_realease_resources()
2181 * unregisters the v4l2,i2c and usb devices
2182 * called when the device gets disconected or at module unload
2184 void em28xx_release_analog_resources(struct em28xx
*dev
)
2187 /*FIXME: I2C IR should be disconnected */
2189 if (dev
->radio_dev
) {
2190 if (video_is_registered(dev
->radio_dev
))
2191 video_unregister_device(dev
->radio_dev
);
2193 video_device_release(dev
->radio_dev
);
2194 dev
->radio_dev
= NULL
;
2197 em28xx_info("V4L2 device %s deregistered\n",
2198 video_device_node_name(dev
->vbi_dev
));
2199 if (video_is_registered(dev
->vbi_dev
))
2200 video_unregister_device(dev
->vbi_dev
);
2202 video_device_release(dev
->vbi_dev
);
2203 dev
->vbi_dev
= NULL
;
2206 em28xx_info("V4L2 device %s deregistered\n",
2207 video_device_node_name(dev
->vdev
));
2208 if (video_is_registered(dev
->vdev
))
2209 video_unregister_device(dev
->vdev
);
2211 video_device_release(dev
->vdev
);
2217 * em28xx_v4l2_close()
2218 * stops streaming and deallocates all resources allocated by the v4l2
2221 static int em28xx_v4l2_close(struct file
*filp
)
2223 struct em28xx_fh
*fh
= filp
->private_data
;
2224 struct em28xx
*dev
= fh
->dev
;
2227 em28xx_videodbg("users=%d\n", dev
->users
);
2229 if (res_check(fh
, EM28XX_RESOURCE_VIDEO
)) {
2230 videobuf_stop(&fh
->vb_vidq
);
2231 res_free(fh
, EM28XX_RESOURCE_VIDEO
);
2234 if (res_check(fh
, EM28XX_RESOURCE_VBI
)) {
2235 videobuf_stop(&fh
->vb_vbiq
);
2236 res_free(fh
, EM28XX_RESOURCE_VBI
);
2239 if (dev
->users
== 1) {
2240 /* the device is already disconnect,
2241 free the remaining resources */
2242 if (dev
->state
& DEV_DISCONNECTED
) {
2243 em28xx_release_resources(dev
);
2248 /* Save some power by putting tuner to sleep */
2249 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_power
, 0);
2251 /* do this before setting alternate! */
2252 em28xx_uninit_isoc(dev
);
2253 em28xx_set_mode(dev
, EM28XX_SUSPEND
);
2255 /* set alternate 0 */
2257 em28xx_videodbg("setting alternate 0\n");
2258 errCode
= usb_set_interface(dev
->udev
, 0, 0);
2260 em28xx_errdev("cannot change alternate number to "
2261 "0 (error=%i)\n", errCode
);
2265 videobuf_mmap_free(&fh
->vb_vidq
);
2266 videobuf_mmap_free(&fh
->vb_vbiq
);
2269 wake_up_interruptible_nr(&dev
->open
, 1);
2274 * em28xx_v4l2_read()
2275 * will allocate buffers when called for the first time
2278 em28xx_v4l2_read(struct file
*filp
, char __user
*buf
, size_t count
,
2281 struct em28xx_fh
*fh
= filp
->private_data
;
2282 struct em28xx
*dev
= fh
->dev
;
2285 rc
= check_dev(dev
);
2289 /* FIXME: read() is not prepared to allow changing the video
2290 resolution while streaming. Seems a bug at em28xx_set_fmt
2293 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
2294 if (res_locked(dev
, EM28XX_RESOURCE_VIDEO
))
2297 return videobuf_read_stream(&fh
->vb_vidq
, buf
, count
, pos
, 0,
2298 filp
->f_flags
& O_NONBLOCK
);
2302 if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
2303 if (!res_get(fh
, EM28XX_RESOURCE_VBI
))
2306 return videobuf_read_stream(&fh
->vb_vbiq
, buf
, count
, pos
, 0,
2307 filp
->f_flags
& O_NONBLOCK
);
2314 * em28xx_v4l2_poll()
2315 * will allocate buffers when called for the first time
2317 static unsigned int em28xx_v4l2_poll(struct file
*filp
, poll_table
*wait
)
2319 struct em28xx_fh
*fh
= filp
->private_data
;
2320 struct em28xx
*dev
= fh
->dev
;
2323 rc
= check_dev(dev
);
2327 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
2328 if (!res_get(fh
, EM28XX_RESOURCE_VIDEO
))
2330 return videobuf_poll_stream(filp
, &fh
->vb_vidq
, wait
);
2331 } else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
2332 if (!res_get(fh
, EM28XX_RESOURCE_VBI
))
2334 return videobuf_poll_stream(filp
, &fh
->vb_vbiq
, wait
);
2341 * em28xx_v4l2_mmap()
2343 static int em28xx_v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
2345 struct em28xx_fh
*fh
= filp
->private_data
;
2346 struct em28xx
*dev
= fh
->dev
;
2349 rc
= check_dev(dev
);
2353 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2354 rc
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
2355 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
2356 rc
= videobuf_mmap_mapper(&fh
->vb_vbiq
, vma
);
2358 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2359 (unsigned long)vma
->vm_start
,
2360 (unsigned long)vma
->vm_end
-(unsigned long)vma
->vm_start
,
2366 static const struct v4l2_file_operations em28xx_v4l_fops
= {
2367 .owner
= THIS_MODULE
,
2368 .open
= em28xx_v4l2_open
,
2369 .release
= em28xx_v4l2_close
,
2370 .read
= em28xx_v4l2_read
,
2371 .poll
= em28xx_v4l2_poll
,
2372 .mmap
= em28xx_v4l2_mmap
,
2373 .ioctl
= video_ioctl2
,
2376 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
2377 .vidioc_querycap
= vidioc_querycap
,
2378 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
2379 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
2380 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
2381 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
2382 .vidioc_g_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
2383 .vidioc_s_fmt_vbi_cap
= vidioc_s_fmt_vbi_cap
,
2384 .vidioc_g_audio
= vidioc_g_audio
,
2385 .vidioc_s_audio
= vidioc_s_audio
,
2386 .vidioc_cropcap
= vidioc_cropcap
,
2388 .vidioc_g_fmt_sliced_vbi_cap
= vidioc_g_fmt_sliced_vbi_cap
,
2389 .vidioc_try_fmt_sliced_vbi_cap
= vidioc_try_set_sliced_vbi_cap
,
2390 .vidioc_s_fmt_sliced_vbi_cap
= vidioc_try_set_sliced_vbi_cap
,
2392 .vidioc_reqbufs
= vidioc_reqbufs
,
2393 .vidioc_querybuf
= vidioc_querybuf
,
2394 .vidioc_qbuf
= vidioc_qbuf
,
2395 .vidioc_dqbuf
= vidioc_dqbuf
,
2396 .vidioc_g_std
= vidioc_g_std
,
2397 .vidioc_s_std
= vidioc_s_std
,
2398 .vidioc_g_parm
= vidioc_g_parm
,
2399 .vidioc_s_parm
= vidioc_s_parm
,
2400 .vidioc_enum_input
= vidioc_enum_input
,
2401 .vidioc_g_input
= vidioc_g_input
,
2402 .vidioc_s_input
= vidioc_s_input
,
2403 .vidioc_queryctrl
= vidioc_queryctrl
,
2404 .vidioc_g_ctrl
= vidioc_g_ctrl
,
2405 .vidioc_s_ctrl
= vidioc_s_ctrl
,
2406 .vidioc_streamon
= vidioc_streamon
,
2407 .vidioc_streamoff
= vidioc_streamoff
,
2408 .vidioc_g_tuner
= vidioc_g_tuner
,
2409 .vidioc_s_tuner
= vidioc_s_tuner
,
2410 .vidioc_g_frequency
= vidioc_g_frequency
,
2411 .vidioc_s_frequency
= vidioc_s_frequency
,
2412 #ifdef CONFIG_VIDEO_ADV_DEBUG
2413 .vidioc_g_register
= vidioc_g_register
,
2414 .vidioc_s_register
= vidioc_s_register
,
2415 .vidioc_g_chip_ident
= vidioc_g_chip_ident
,
2417 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2418 .vidiocgmbuf
= vidiocgmbuf
,
2422 static const struct video_device em28xx_video_template
= {
2423 .fops
= &em28xx_v4l_fops
,
2424 .release
= video_device_release
,
2425 .ioctl_ops
= &video_ioctl_ops
,
2427 .tvnorms
= V4L2_STD_ALL
,
2428 .current_norm
= V4L2_STD_PAL
,
2431 static const struct v4l2_file_operations radio_fops
= {
2432 .owner
= THIS_MODULE
,
2433 .open
= em28xx_v4l2_open
,
2434 .release
= em28xx_v4l2_close
,
2435 .ioctl
= video_ioctl2
,
2438 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
2439 .vidioc_querycap
= radio_querycap
,
2440 .vidioc_g_tuner
= radio_g_tuner
,
2441 .vidioc_enum_input
= radio_enum_input
,
2442 .vidioc_g_audio
= radio_g_audio
,
2443 .vidioc_s_tuner
= radio_s_tuner
,
2444 .vidioc_s_audio
= radio_s_audio
,
2445 .vidioc_s_input
= radio_s_input
,
2446 .vidioc_queryctrl
= radio_queryctrl
,
2447 .vidioc_g_ctrl
= vidioc_g_ctrl
,
2448 .vidioc_s_ctrl
= vidioc_s_ctrl
,
2449 .vidioc_g_frequency
= vidioc_g_frequency
,
2450 .vidioc_s_frequency
= vidioc_s_frequency
,
2451 #ifdef CONFIG_VIDEO_ADV_DEBUG
2452 .vidioc_g_register
= vidioc_g_register
,
2453 .vidioc_s_register
= vidioc_s_register
,
2457 static struct video_device em28xx_radio_template
= {
2458 .name
= "em28xx-radio",
2459 .fops
= &radio_fops
,
2460 .ioctl_ops
= &radio_ioctl_ops
,
2463 /******************************** usb interface ******************************/
2467 static struct video_device
*em28xx_vdev_init(struct em28xx
*dev
,
2468 const struct video_device
*template,
2469 const char *type_name
)
2471 struct video_device
*vfd
;
2473 vfd
= video_device_alloc();
2478 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
2479 vfd
->release
= video_device_release
;
2480 vfd
->debug
= video_debug
;
2482 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s",
2483 dev
->name
, type_name
);
2485 video_set_drvdata(vfd
, dev
);
2489 int em28xx_register_analog_devices(struct em28xx
*dev
)
2494 printk(KERN_INFO
"%s: v4l2 driver version %d.%d.%d\n",
2496 (EM28XX_VERSION_CODE
>> 16) & 0xff,
2497 (EM28XX_VERSION_CODE
>> 8) & 0xff, EM28XX_VERSION_CODE
& 0xff);
2499 /* set default norm */
2500 dev
->norm
= em28xx_video_template
.current_norm
;
2501 dev
->interlaced
= EM28XX_INTERLACED_DEFAULT
;
2504 /* Analog specific initialization */
2505 dev
->format
= &format
[0];
2506 em28xx_set_video_format(dev
, format
[0].fourcc
,
2507 norm_maxw(dev
), norm_maxh(dev
));
2509 video_mux(dev
, dev
->ctl_input
);
2511 /* Audio defaults */
2515 /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
2516 val
= (u8
)em28xx_read_reg(dev
, EM28XX_R0F_XCLK
);
2517 em28xx_write_reg(dev
, EM28XX_R0F_XCLK
,
2518 (EM28XX_XCLK_AUDIO_UNMUTE
| val
));
2520 em28xx_set_outfmt(dev
);
2521 em28xx_colorlevels_set_default(dev
);
2522 em28xx_compression_disable(dev
);
2524 /* allocate and fill video video_device struct */
2525 dev
->vdev
= em28xx_vdev_init(dev
, &em28xx_video_template
, "video");
2527 em28xx_errdev("cannot allocate video_device.\n");
2531 /* register v4l2 video video_device */
2532 ret
= video_register_device(dev
->vdev
, VFL_TYPE_GRABBER
,
2533 video_nr
[dev
->devno
]);
2535 em28xx_errdev("unable to register video device (error=%i).\n",
2540 /* Allocate and fill vbi video_device struct */
2541 if (em28xx_vbi_supported(dev
) == 1) {
2542 dev
->vbi_dev
= em28xx_vdev_init(dev
, &em28xx_video_template
,
2545 /* register v4l2 vbi video_device */
2546 ret
= video_register_device(dev
->vbi_dev
, VFL_TYPE_VBI
,
2547 vbi_nr
[dev
->devno
]);
2549 em28xx_errdev("unable to register vbi device\n");
2554 if (em28xx_boards
[dev
->model
].radio
.type
== EM28XX_RADIO
) {
2555 dev
->radio_dev
= em28xx_vdev_init(dev
, &em28xx_radio_template
,
2557 if (!dev
->radio_dev
) {
2558 em28xx_errdev("cannot allocate video_device.\n");
2561 ret
= video_register_device(dev
->radio_dev
, VFL_TYPE_RADIO
,
2562 radio_nr
[dev
->devno
]);
2564 em28xx_errdev("can't register radio device\n");
2567 em28xx_info("Registered radio device as %s\n",
2568 video_device_node_name(dev
->radio_dev
));
2571 em28xx_info("V4L2 video device registered as %s\n",
2572 video_device_node_name(dev
->vdev
));
2575 em28xx_info("V4L2 VBI device registered as %s\n",
2576 video_device_node_name(dev
->vbi_dev
));