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
;
209 if (dev
->progressive
)
212 /* Interlaces two half frames */
216 fieldstart
= outp
+ bytesperline
;
219 linesdone
= dma_q
->pos
/ bytesperline
;
220 currlinedone
= dma_q
->pos
% bytesperline
;
222 if (dev
->progressive
)
223 offset
= linesdone
* bytesperline
+ currlinedone
;
225 offset
= linesdone
* bytesperline
* 2 + currlinedone
;
227 startwrite
= fieldstart
+ offset
;
228 lencopy
= bytesperline
- currlinedone
;
229 lencopy
= lencopy
> remain
? remain
: lencopy
;
231 if ((char *)startwrite
+ lencopy
> (char *)outp
+ buf
->vb
.size
) {
232 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
233 ((char *)startwrite
+ lencopy
) -
234 ((char *)outp
+ buf
->vb
.size
));
235 remain
= (char *)outp
+ buf
->vb
.size
- (char *)startwrite
;
240 memcpy(startwrite
, startread
, lencopy
);
245 startwrite
+= lencopy
+ bytesperline
;
246 startread
+= lencopy
;
247 if (bytesperline
> remain
)
250 lencopy
= bytesperline
;
252 if ((char *)startwrite
+ lencopy
> (char *)outp
+
254 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
256 ((char *)startwrite
+ lencopy
) -
257 ((char *)outp
+ buf
->vb
.size
));
258 lencopy
= remain
= (char *)outp
+ buf
->vb
.size
-
264 memcpy(startwrite
, startread
, lencopy
);
272 static void em28xx_copy_vbi(struct em28xx
*dev
,
273 struct em28xx_dmaqueue
*dma_q
,
274 struct em28xx_buffer
*buf
,
276 unsigned char *outp
, unsigned long len
)
278 void *startwrite
, *startread
;
280 int bytesperline
= dev
->vbi_width
;
283 em28xx_isocdbg("dev is null\n");
288 em28xx_isocdbg("dma_q is null\n");
295 em28xx_isocdbg("p is null\n");
299 em28xx_isocdbg("outp is null\n");
303 if (dma_q
->pos
+ len
> buf
->vb
.size
)
304 len
= buf
->vb
.size
- dma_q
->pos
;
308 startwrite
= outp
+ dma_q
->pos
;
311 /* Make sure the bottom field populates the second half of the frame */
312 if (buf
->top_field
== 0) {
313 startwrite
+= bytesperline
* dev
->vbi_height
;
314 offset
+= bytesperline
* dev
->vbi_height
;
317 memcpy(startwrite
, startread
, len
);
321 static inline void print_err_status(struct em28xx
*dev
,
322 int packet
, int status
)
324 char *errmsg
= "Unknown";
328 errmsg
= "unlinked synchronuously";
331 errmsg
= "unlinked asynchronuously";
334 errmsg
= "Buffer error (overrun)";
337 errmsg
= "Stalled (device not responding)";
340 errmsg
= "Babble (bad cable?)";
343 errmsg
= "Bit-stuff error (bad cable?)";
346 errmsg
= "CRC/Timeout (could be anything)";
349 errmsg
= "Device does not respond";
353 em28xx_isocdbg("URB status %d [%s].\n", status
, errmsg
);
355 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
356 packet
, status
, errmsg
);
361 * video-buf generic routine to get the next available buffer
363 static inline void get_next_buf(struct em28xx_dmaqueue
*dma_q
,
364 struct em28xx_buffer
**buf
)
366 struct em28xx
*dev
= container_of(dma_q
, struct em28xx
, vidq
);
369 if (list_empty(&dma_q
->active
)) {
370 em28xx_isocdbg("No active queue to serve\n");
371 dev
->isoc_ctl
.vid_buf
= NULL
;
376 /* Get the next buffer */
377 *buf
= list_entry(dma_q
->active
.next
, struct em28xx_buffer
, vb
.queue
);
379 /* Cleans up buffer - Usefull for testing for frame/URB loss */
380 outp
= videobuf_to_vmalloc(&(*buf
)->vb
);
381 memset(outp
, 0, (*buf
)->vb
.size
);
383 dev
->isoc_ctl
.vid_buf
= *buf
;
389 * video-buf generic routine to get the next available VBI buffer
391 static inline void vbi_get_next_buf(struct em28xx_dmaqueue
*dma_q
,
392 struct em28xx_buffer
**buf
)
394 struct em28xx
*dev
= container_of(dma_q
, struct em28xx
, vbiq
);
397 if (list_empty(&dma_q
->active
)) {
398 em28xx_isocdbg("No active queue to serve\n");
399 dev
->isoc_ctl
.vbi_buf
= NULL
;
404 /* Get the next buffer */
405 *buf
= list_entry(dma_q
->active
.next
, struct em28xx_buffer
, vb
.queue
);
406 /* Cleans up buffer - Usefull for testing for frame/URB loss */
407 outp
= videobuf_to_vmalloc(&(*buf
)->vb
);
408 memset(outp
, 0x00, (*buf
)->vb
.size
);
410 dev
->isoc_ctl
.vbi_buf
= *buf
;
416 * Controls the isoc copy of each urb packet
418 static inline int em28xx_isoc_copy(struct em28xx
*dev
, struct urb
*urb
)
420 struct em28xx_buffer
*buf
;
421 struct em28xx_dmaqueue
*dma_q
= &dev
->vidq
;
422 unsigned char *outp
= NULL
;
423 int i
, len
= 0, rc
= 1;
429 if ((dev
->state
& DEV_DISCONNECTED
) || (dev
->state
& DEV_MISCONFIGURED
))
432 if (urb
->status
< 0) {
433 print_err_status(dev
, -1, urb
->status
);
434 if (urb
->status
== -ENOENT
)
438 buf
= dev
->isoc_ctl
.vid_buf
;
440 outp
= videobuf_to_vmalloc(&buf
->vb
);
442 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
443 int status
= urb
->iso_frame_desc
[i
].status
;
446 print_err_status(dev
, i
, status
);
447 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
451 len
= urb
->iso_frame_desc
[i
].actual_length
- 4;
453 if (urb
->iso_frame_desc
[i
].actual_length
<= 0) {
454 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
457 if (urb
->iso_frame_desc
[i
].actual_length
>
459 em28xx_isocdbg("packet bigger than packet size");
463 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
465 /* FIXME: incomplete buffer checks where removed to make
466 logic simpler. Impacts of those changes should be evaluated
468 if (p
[0] == 0x33 && p
[1] == 0x95 && p
[2] == 0x00) {
469 em28xx_isocdbg("VBI HEADER!!!\n");
470 /* FIXME: Should add vbi copy */
473 if (p
[0] == 0x22 && p
[1] == 0x5a) {
474 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p
[2],
475 len
, (p
[2] & 1) ? "odd" : "even");
477 if (dev
->progressive
|| !(p
[2] & 1)) {
479 buffer_filled(dev
, dma_q
, buf
);
480 get_next_buf(dma_q
, &buf
);
484 outp
= videobuf_to_vmalloc(&buf
->vb
);
497 if (p
[0] != 0x88 && p
[0] != 0x22) {
498 em28xx_isocdbg("frame is not complete\n");
503 em28xx_copy_video(dev
, dma_q
, buf
, p
, outp
, len
);
509 /* Version of isoc handler that takes into account a mixture of video and
511 static inline int em28xx_isoc_copy_vbi(struct em28xx
*dev
, struct urb
*urb
)
513 struct em28xx_buffer
*buf
, *vbi_buf
;
514 struct em28xx_dmaqueue
*dma_q
= &dev
->vidq
;
515 struct em28xx_dmaqueue
*vbi_dma_q
= &dev
->vbiq
;
516 unsigned char *outp
= NULL
;
517 unsigned char *vbioutp
= NULL
;
518 int i
, len
= 0, rc
= 1;
525 if ((dev
->state
& DEV_DISCONNECTED
) || (dev
->state
& DEV_MISCONFIGURED
))
528 if (urb
->status
< 0) {
529 print_err_status(dev
, -1, urb
->status
);
530 if (urb
->status
== -ENOENT
)
534 buf
= dev
->isoc_ctl
.vid_buf
;
536 outp
= videobuf_to_vmalloc(&buf
->vb
);
538 vbi_buf
= dev
->isoc_ctl
.vbi_buf
;
540 vbioutp
= videobuf_to_vmalloc(&vbi_buf
->vb
);
542 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
543 int status
= urb
->iso_frame_desc
[i
].status
;
546 print_err_status(dev
, i
, status
);
547 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
551 len
= urb
->iso_frame_desc
[i
].actual_length
;
552 if (urb
->iso_frame_desc
[i
].actual_length
<= 0) {
553 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
556 if (urb
->iso_frame_desc
[i
].actual_length
>
558 em28xx_isocdbg("packet bigger than packet size");
562 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
564 /* capture type 0 = vbi start
565 capture type 1 = video start
566 capture type 2 = video in progress */
567 if (p
[0] == 0x33 && p
[1] == 0x95) {
568 dev
->capture_type
= 0;
570 em28xx_isocdbg("VBI START HEADER!!!\n");
571 dev
->cur_field
= p
[2];
574 } else if (p
[0] == 0x88 && p
[1] == 0x88 &&
575 p
[2] == 0x88 && p
[3] == 0x88) {
579 } else if (p
[0] == 0x22 && p
[1] == 0x5a) {
585 vbi_size
= dev
->vbi_width
* dev
->vbi_height
;
587 if (dev
->capture_type
== 0) {
588 if (dev
->vbi_read
>= vbi_size
) {
589 /* We've already read all the VBI data, so
590 treat the rest as video */
591 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
592 } else if ((dev
->vbi_read
+ len
) < vbi_size
) {
593 /* This entire frame is VBI data */
594 if (dev
->vbi_read
== 0 &&
595 (!(dev
->cur_field
& 1))) {
596 /* Brand new frame */
598 vbi_buffer_filled(dev
,
601 vbi_get_next_buf(vbi_dma_q
, &vbi_buf
);
605 vbioutp
= videobuf_to_vmalloc(
609 if (dev
->vbi_read
== 0) {
611 if (vbi_buf
!= NULL
) {
612 if (dev
->cur_field
& 1)
613 vbi_buf
->top_field
= 0;
615 vbi_buf
->top_field
= 1;
619 dev
->vbi_read
+= len
;
620 em28xx_copy_vbi(dev
, vbi_dma_q
, vbi_buf
, p
,
623 /* Some of this frame is VBI data and some is
625 int vbi_data_len
= vbi_size
- dev
->vbi_read
;
626 dev
->vbi_read
+= vbi_data_len
;
627 em28xx_copy_vbi(dev
, vbi_dma_q
, vbi_buf
, p
,
628 vbioutp
, vbi_data_len
);
629 dev
->capture_type
= 1;
635 if (dev
->capture_type
== 1) {
636 dev
->capture_type
= 2;
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)
656 if (buf
!= NULL
&& dev
->capture_type
== 2) {
657 if (len
> 4 && p
[0] == 0x88 && p
[1] == 0x88 &&
658 p
[2] == 0x88 && p
[3] == 0x88) {
662 if (len
> 4 && p
[0] == 0x22 && p
[1] == 0x5a) {
663 em28xx_isocdbg("Video frame %d, len=%i, %s\n",
664 p
[2], len
, (p
[2] & 1) ?
671 em28xx_copy_video(dev
, dma_q
, buf
, p
, outp
,
679 /* ------------------------------------------------------------------
681 ------------------------------------------------------------------*/
684 buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
, unsigned int *size
)
686 struct em28xx_fh
*fh
= vq
->priv_data
;
687 struct em28xx
*dev
= fh
->dev
;
688 struct v4l2_frequency f
;
690 *size
= (fh
->dev
->width
* fh
->dev
->height
* dev
->format
->depth
+ 7)
694 *count
= EM28XX_DEF_BUF
;
696 if (*count
< EM28XX_MIN_BUF
)
697 *count
= EM28XX_MIN_BUF
;
699 /* Ask tuner to go to analog or radio mode */
700 memset(&f
, 0, sizeof(f
));
701 f
.frequency
= dev
->ctl_freq
;
702 f
.type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
704 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, &f
);
709 /* This is called *without* dev->slock held; please keep it that way */
710 static void free_buffer(struct videobuf_queue
*vq
, struct em28xx_buffer
*buf
)
712 struct em28xx_fh
*fh
= vq
->priv_data
;
713 struct em28xx
*dev
= fh
->dev
;
714 unsigned long flags
= 0;
718 /* We used to wait for the buffer to finish here, but this didn't work
719 because, as we were keeping the state as VIDEOBUF_QUEUED,
720 videobuf_queue_cancel marked it as finished for us.
721 (Also, it could wedge forever if the hardware was misconfigured.)
723 This should be safe; by the time we get here, the buffer isn't
724 queued anymore. If we ever start marking the buffers as
725 VIDEOBUF_ACTIVE, it won't be, though.
727 spin_lock_irqsave(&dev
->slock
, flags
);
728 if (dev
->isoc_ctl
.vid_buf
== buf
)
729 dev
->isoc_ctl
.vid_buf
= NULL
;
730 spin_unlock_irqrestore(&dev
->slock
, flags
);
732 videobuf_vmalloc_free(&buf
->vb
);
733 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
737 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
738 enum v4l2_field field
)
740 struct em28xx_fh
*fh
= vq
->priv_data
;
741 struct em28xx_buffer
*buf
= container_of(vb
, struct em28xx_buffer
, vb
);
742 struct em28xx
*dev
= fh
->dev
;
743 int rc
= 0, urb_init
= 0;
745 buf
->vb
.size
= (fh
->dev
->width
* fh
->dev
->height
* dev
->format
->depth
748 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
751 buf
->vb
.width
= dev
->width
;
752 buf
->vb
.height
= dev
->height
;
753 buf
->vb
.field
= field
;
755 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
756 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
761 if (!dev
->isoc_ctl
.num_bufs
)
765 if (em28xx_vbi_supported(dev
) == 1)
766 rc
= em28xx_init_isoc(dev
, EM28XX_NUM_PACKETS
,
769 em28xx_isoc_copy_vbi
);
771 rc
= em28xx_init_isoc(dev
, EM28XX_NUM_PACKETS
,
779 buf
->vb
.state
= VIDEOBUF_PREPARED
;
783 free_buffer(vq
, buf
);
788 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
790 struct em28xx_buffer
*buf
= container_of(vb
,
791 struct em28xx_buffer
,
793 struct em28xx_fh
*fh
= vq
->priv_data
;
794 struct em28xx
*dev
= fh
->dev
;
795 struct em28xx_dmaqueue
*vidq
= &dev
->vidq
;
797 buf
->vb
.state
= VIDEOBUF_QUEUED
;
798 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
802 static void buffer_release(struct videobuf_queue
*vq
,
803 struct videobuf_buffer
*vb
)
805 struct em28xx_buffer
*buf
= container_of(vb
,
806 struct em28xx_buffer
,
808 struct em28xx_fh
*fh
= vq
->priv_data
;
809 struct em28xx
*dev
= (struct em28xx
*)fh
->dev
;
811 em28xx_isocdbg("em28xx: called buffer_release\n");
813 free_buffer(vq
, buf
);
816 static struct videobuf_queue_ops em28xx_video_qops
= {
817 .buf_setup
= buffer_setup
,
818 .buf_prepare
= buffer_prepare
,
819 .buf_queue
= buffer_queue
,
820 .buf_release
= buffer_release
,
823 /********************* v4l2 interface **************************************/
825 static void video_mux(struct em28xx
*dev
, int index
)
827 dev
->ctl_input
= index
;
828 dev
->ctl_ainput
= INPUT(index
)->amux
;
829 dev
->ctl_aoutput
= INPUT(index
)->aout
;
831 if (!dev
->ctl_aoutput
)
832 dev
->ctl_aoutput
= EM28XX_AOUT_MASTER
;
834 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_routing
,
835 INPUT(index
)->vmux
, 0, 0);
837 if (dev
->board
.has_msp34xx
) {
838 if (dev
->i2s_speed
) {
839 v4l2_device_call_all(&dev
->v4l2_dev
, 0, audio
,
840 s_i2s_clock_freq
, dev
->i2s_speed
);
842 /* Note: this is msp3400 specific */
843 v4l2_device_call_all(&dev
->v4l2_dev
, 0, audio
, s_routing
,
844 dev
->ctl_ainput
, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1
), 0);
847 if (dev
->board
.adecoder
!= EM28XX_NOADECODER
) {
848 v4l2_device_call_all(&dev
->v4l2_dev
, 0, audio
, s_routing
,
849 dev
->ctl_ainput
, dev
->ctl_aoutput
, 0);
852 em28xx_audio_analog_set(dev
);
855 /* Usage lock check functions */
856 static int res_get(struct em28xx_fh
*fh
, unsigned int bit
)
858 struct em28xx
*dev
= fh
->dev
;
860 if (fh
->resources
& bit
)
861 /* have it already allocated */
865 mutex_lock(&dev
->lock
);
866 if (dev
->resources
& bit
) {
867 /* no, someone else uses it */
868 mutex_unlock(&dev
->lock
);
871 /* it's free, grab it */
872 fh
->resources
|= bit
;
873 dev
->resources
|= bit
;
874 em28xx_videodbg("res: get %d\n", bit
);
875 mutex_unlock(&dev
->lock
);
879 static int res_check(struct em28xx_fh
*fh
, unsigned int bit
)
881 return fh
->resources
& bit
;
884 static int res_locked(struct em28xx
*dev
, unsigned int bit
)
886 return dev
->resources
& bit
;
889 static void res_free(struct em28xx_fh
*fh
, unsigned int bits
)
891 struct em28xx
*dev
= fh
->dev
;
893 BUG_ON((fh
->resources
& bits
) != bits
);
895 mutex_lock(&dev
->lock
);
896 fh
->resources
&= ~bits
;
897 dev
->resources
&= ~bits
;
898 em28xx_videodbg("res: put %d\n", bits
);
899 mutex_unlock(&dev
->lock
);
902 static int get_ressource(struct em28xx_fh
*fh
)
905 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
906 return EM28XX_RESOURCE_VIDEO
;
907 case V4L2_BUF_TYPE_VBI_CAPTURE
:
908 return EM28XX_RESOURCE_VBI
;
917 * return the ac97 supported controls
919 static int ac97_queryctrl(struct v4l2_queryctrl
*qc
)
923 for (i
= 0; i
< ARRAY_SIZE(ac97_qctrl
); i
++) {
924 if (qc
->id
&& qc
->id
== ac97_qctrl
[i
].id
) {
925 memcpy(qc
, &(ac97_qctrl
[i
]), sizeof(*qc
));
930 /* Control is not ac97 related */
936 * return the current values for ac97 mute and volume
938 static int ac97_get_ctrl(struct em28xx
*dev
, struct v4l2_control
*ctrl
)
941 case V4L2_CID_AUDIO_MUTE
:
942 ctrl
->value
= dev
->mute
;
944 case V4L2_CID_AUDIO_VOLUME
:
945 ctrl
->value
= dev
->volume
;
948 /* Control is not ac97 related */
955 * set values for ac97 mute and volume
957 static int ac97_set_ctrl(struct em28xx
*dev
, const struct v4l2_control
*ctrl
)
961 for (i
= 0; i
< ARRAY_SIZE(ac97_qctrl
); i
++)
962 if (ctrl
->id
== ac97_qctrl
[i
].id
)
965 /* Announce that hasn't handle it */
969 if (ctrl
->value
< ac97_qctrl
[i
].minimum
||
970 ctrl
->value
> ac97_qctrl
[i
].maximum
)
974 case V4L2_CID_AUDIO_MUTE
:
975 dev
->mute
= ctrl
->value
;
977 case V4L2_CID_AUDIO_VOLUME
:
978 dev
->volume
= ctrl
->value
;
982 return em28xx_audio_analog_set(dev
);
985 static int check_dev(struct em28xx
*dev
)
987 if (dev
->state
& DEV_DISCONNECTED
) {
988 em28xx_errdev("v4l2 ioctl: device not present\n");
992 if (dev
->state
& DEV_MISCONFIGURED
) {
993 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
994 "close and open it again\n");
1000 static void get_scale(struct em28xx
*dev
,
1001 unsigned int width
, unsigned int height
,
1002 unsigned int *hscale
, unsigned int *vscale
)
1004 unsigned int maxw
= norm_maxw(dev
);
1005 unsigned int maxh
= norm_maxh(dev
);
1007 *hscale
= (((unsigned long)maxw
) << 12) / width
- 4096L;
1008 if (*hscale
>= 0x4000)
1011 *vscale
= (((unsigned long)maxh
) << 12) / height
- 4096L;
1012 if (*vscale
>= 0x4000)
1016 /* ------------------------------------------------------------------
1017 IOCTL vidioc handling
1018 ------------------------------------------------------------------*/
1020 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1021 struct v4l2_format
*f
)
1023 struct em28xx_fh
*fh
= priv
;
1024 struct em28xx
*dev
= fh
->dev
;
1026 mutex_lock(&dev
->lock
);
1028 f
->fmt
.pix
.width
= dev
->width
;
1029 f
->fmt
.pix
.height
= dev
->height
;
1030 f
->fmt
.pix
.pixelformat
= dev
->format
->fourcc
;
1031 f
->fmt
.pix
.bytesperline
= (dev
->width
* dev
->format
->depth
+ 7) >> 3;
1032 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
* dev
->height
;
1033 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1035 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1036 if (dev
->progressive
)
1037 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
1039 f
->fmt
.pix
.field
= dev
->interlaced
?
1040 V4L2_FIELD_INTERLACED
: V4L2_FIELD_TOP
;
1042 mutex_unlock(&dev
->lock
);
1046 static struct em28xx_fmt
*format_by_fourcc(unsigned int fourcc
)
1050 for (i
= 0; i
< ARRAY_SIZE(format
); i
++)
1051 if (format
[i
].fourcc
== fourcc
)
1057 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
1058 struct v4l2_format
*f
)
1060 struct em28xx_fh
*fh
= priv
;
1061 struct em28xx
*dev
= fh
->dev
;
1062 unsigned int width
= f
->fmt
.pix
.width
;
1063 unsigned int height
= f
->fmt
.pix
.height
;
1064 unsigned int maxw
= norm_maxw(dev
);
1065 unsigned int maxh
= norm_maxh(dev
);
1066 unsigned int hscale
, vscale
;
1067 struct em28xx_fmt
*fmt
;
1069 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1071 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1072 f
->fmt
.pix
.pixelformat
);
1076 if (dev
->board
.is_em2800
) {
1077 /* the em2800 can only scale down to 50% */
1078 height
= height
> (3 * maxh
/ 4) ? maxh
: maxh
/ 2;
1079 width
= width
> (3 * maxw
/ 4) ? maxw
: maxw
/ 2;
1081 /* width must even because of the YUYV format
1082 height must be even because of interlacing */
1083 v4l_bound_align_image(&width
, 48, maxw
, 1, &height
, 32, maxh
,
1087 get_scale(dev
, width
, height
, &hscale
, &vscale
);
1089 width
= (((unsigned long)maxw
) << 12) / (hscale
+ 4096L);
1090 height
= (((unsigned long)maxh
) << 12) / (vscale
+ 4096L);
1092 f
->fmt
.pix
.width
= width
;
1093 f
->fmt
.pix
.height
= height
;
1094 f
->fmt
.pix
.pixelformat
= fmt
->fourcc
;
1095 f
->fmt
.pix
.bytesperline
= (dev
->width
* fmt
->depth
+ 7) >> 3;
1096 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
* height
;
1097 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1098 if (dev
->progressive
)
1099 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
1101 f
->fmt
.pix
.field
= dev
->interlaced
?
1102 V4L2_FIELD_INTERLACED
: V4L2_FIELD_TOP
;
1107 static int em28xx_set_video_format(struct em28xx
*dev
, unsigned int fourcc
,
1108 unsigned width
, unsigned height
)
1110 struct em28xx_fmt
*fmt
;
1112 fmt
= format_by_fourcc(fourcc
);
1118 dev
->height
= height
;
1120 /* set new image size */
1121 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
1123 em28xx_set_alternate(dev
);
1124 em28xx_resolution_set(dev
);
1129 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1130 struct v4l2_format
*f
)
1132 struct em28xx_fh
*fh
= priv
;
1133 struct em28xx
*dev
= fh
->dev
;
1136 rc
= check_dev(dev
);
1140 mutex_lock(&dev
->lock
);
1142 vidioc_try_fmt_vid_cap(file
, priv
, f
);
1144 if (videobuf_queue_is_busy(&fh
->vb_vidq
)) {
1145 em28xx_errdev("%s queue busy\n", __func__
);
1150 rc
= em28xx_set_video_format(dev
, f
->fmt
.pix
.pixelformat
,
1151 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
);
1154 mutex_unlock(&dev
->lock
);
1158 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
1160 struct em28xx_fh
*fh
= priv
;
1161 struct em28xx
*dev
= fh
->dev
;
1164 rc
= check_dev(dev
);
1173 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
1175 struct em28xx_fh
*fh
= priv
;
1176 struct em28xx
*dev
= fh
->dev
;
1177 struct v4l2_format f
;
1180 rc
= check_dev(dev
);
1184 mutex_lock(&dev
->lock
);
1187 /* Adjusts width/height, if needed */
1188 f
.fmt
.pix
.width
= dev
->width
;
1189 f
.fmt
.pix
.height
= dev
->height
;
1190 vidioc_try_fmt_vid_cap(file
, priv
, &f
);
1192 /* set new image size */
1193 dev
->width
= f
.fmt
.pix
.width
;
1194 dev
->height
= f
.fmt
.pix
.height
;
1195 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
1197 em28xx_resolution_set(dev
);
1198 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_std
, dev
->norm
);
1200 mutex_unlock(&dev
->lock
);
1204 static int vidioc_g_parm(struct file
*file
, void *priv
,
1205 struct v4l2_streamparm
*p
)
1207 struct em28xx_fh
*fh
= priv
;
1208 struct em28xx
*dev
= fh
->dev
;
1211 if (p
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1214 if (dev
->board
.is_webcam
)
1215 rc
= v4l2_device_call_until_err(&dev
->v4l2_dev
, 0,
1218 v4l2_video_std_frame_period(dev
->norm
,
1219 &p
->parm
.capture
.timeperframe
);
1224 static int vidioc_s_parm(struct file
*file
, void *priv
,
1225 struct v4l2_streamparm
*p
)
1227 struct em28xx_fh
*fh
= priv
;
1228 struct em28xx
*dev
= fh
->dev
;
1230 if (!dev
->board
.is_webcam
)
1233 if (p
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1236 return v4l2_device_call_until_err(&dev
->v4l2_dev
, 0, video
, s_parm
, p
);
1239 static const char *iname
[] = {
1240 [EM28XX_VMUX_COMPOSITE1
] = "Composite1",
1241 [EM28XX_VMUX_COMPOSITE2
] = "Composite2",
1242 [EM28XX_VMUX_COMPOSITE3
] = "Composite3",
1243 [EM28XX_VMUX_COMPOSITE4
] = "Composite4",
1244 [EM28XX_VMUX_SVIDEO
] = "S-Video",
1245 [EM28XX_VMUX_TELEVISION
] = "Television",
1246 [EM28XX_VMUX_CABLE
] = "Cable TV",
1247 [EM28XX_VMUX_DVB
] = "DVB",
1248 [EM28XX_VMUX_DEBUG
] = "for debug only",
1251 static int vidioc_enum_input(struct file
*file
, void *priv
,
1252 struct v4l2_input
*i
)
1254 struct em28xx_fh
*fh
= priv
;
1255 struct em28xx
*dev
= fh
->dev
;
1259 if (n
>= MAX_EM28XX_INPUT
)
1261 if (0 == INPUT(n
)->type
)
1265 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1267 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
1269 if ((EM28XX_VMUX_TELEVISION
== INPUT(n
)->type
) ||
1270 (EM28XX_VMUX_CABLE
== INPUT(n
)->type
))
1271 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1273 i
->std
= dev
->vdev
->tvnorms
;
1278 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1280 struct em28xx_fh
*fh
= priv
;
1281 struct em28xx
*dev
= fh
->dev
;
1283 *i
= dev
->ctl_input
;
1288 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1290 struct em28xx_fh
*fh
= priv
;
1291 struct em28xx
*dev
= fh
->dev
;
1294 rc
= check_dev(dev
);
1298 if (i
>= MAX_EM28XX_INPUT
)
1300 if (0 == INPUT(i
)->type
)
1305 mutex_lock(&dev
->lock
);
1306 video_mux(dev
, dev
->ctl_input
);
1307 mutex_unlock(&dev
->lock
);
1311 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1313 struct em28xx_fh
*fh
= priv
;
1314 struct em28xx
*dev
= fh
->dev
;
1316 if (!dev
->audio_mode
.has_audio
)
1320 case EM28XX_AMUX_VIDEO
:
1321 strcpy(a
->name
, "Television");
1323 case EM28XX_AMUX_LINE_IN
:
1324 strcpy(a
->name
, "Line In");
1326 case EM28XX_AMUX_VIDEO2
:
1327 strcpy(a
->name
, "Television alt");
1329 case EM28XX_AMUX_PHONE
:
1330 strcpy(a
->name
, "Phone");
1332 case EM28XX_AMUX_MIC
:
1333 strcpy(a
->name
, "Mic");
1335 case EM28XX_AMUX_CD
:
1336 strcpy(a
->name
, "CD");
1338 case EM28XX_AMUX_AUX
:
1339 strcpy(a
->name
, "Aux");
1341 case EM28XX_AMUX_PCM_OUT
:
1342 strcpy(a
->name
, "PCM");
1348 a
->index
= dev
->ctl_ainput
;
1349 a
->capability
= V4L2_AUDCAP_STEREO
;
1354 static int vidioc_s_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1356 struct em28xx_fh
*fh
= priv
;
1357 struct em28xx
*dev
= fh
->dev
;
1360 if (!dev
->audio_mode
.has_audio
)
1363 if (a
->index
>= MAX_EM28XX_INPUT
)
1365 if (0 == INPUT(a
->index
)->type
)
1368 mutex_lock(&dev
->lock
);
1370 dev
->ctl_ainput
= INPUT(a
->index
)->amux
;
1371 dev
->ctl_aoutput
= INPUT(a
->index
)->aout
;
1373 if (!dev
->ctl_aoutput
)
1374 dev
->ctl_aoutput
= EM28XX_AOUT_MASTER
;
1376 mutex_unlock(&dev
->lock
);
1380 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1381 struct v4l2_queryctrl
*qc
)
1383 struct em28xx_fh
*fh
= priv
;
1384 struct em28xx
*dev
= fh
->dev
;
1388 rc
= check_dev(dev
);
1392 memset(qc
, 0, sizeof(*qc
));
1396 /* enumberate AC97 controls */
1397 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
) {
1398 rc
= ac97_queryctrl(qc
);
1403 /* enumberate V4L2 device controls */
1404 mutex_lock(&dev
->lock
);
1405 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, queryctrl
, qc
);
1406 mutex_unlock(&dev
->lock
);
1414 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1415 struct v4l2_control
*ctrl
)
1417 struct em28xx_fh
*fh
= priv
;
1418 struct em28xx
*dev
= fh
->dev
;
1421 rc
= check_dev(dev
);
1426 mutex_lock(&dev
->lock
);
1428 /* Set an AC97 control */
1429 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
)
1430 rc
= ac97_get_ctrl(dev
, ctrl
);
1434 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1436 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_ctrl
, ctrl
);
1440 mutex_unlock(&dev
->lock
);
1444 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1445 struct v4l2_control
*ctrl
)
1447 struct em28xx_fh
*fh
= priv
;
1448 struct em28xx
*dev
= fh
->dev
;
1451 rc
= check_dev(dev
);
1455 mutex_lock(&dev
->lock
);
1457 /* Set an AC97 control */
1458 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
)
1459 rc
= ac97_set_ctrl(dev
, ctrl
);
1463 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
1465 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_ctrl
, ctrl
);
1468 * In the case of non-AC97 volume controls, we still need
1469 * to do some setups at em28xx, in order to mute/unmute
1470 * and to adjust audio volume. However, the value ranges
1471 * should be checked by the corresponding V4L subdriver.
1474 case V4L2_CID_AUDIO_MUTE
:
1475 dev
->mute
= ctrl
->value
;
1476 rc
= em28xx_audio_analog_set(dev
);
1478 case V4L2_CID_AUDIO_VOLUME
:
1479 dev
->volume
= ctrl
->value
;
1480 rc
= em28xx_audio_analog_set(dev
);
1484 mutex_unlock(&dev
->lock
);
1488 static int vidioc_g_tuner(struct file
*file
, void *priv
,
1489 struct v4l2_tuner
*t
)
1491 struct em28xx_fh
*fh
= priv
;
1492 struct em28xx
*dev
= fh
->dev
;
1495 rc
= check_dev(dev
);
1502 strcpy(t
->name
, "Tuner");
1503 t
->type
= V4L2_TUNER_ANALOG_TV
;
1505 mutex_lock(&dev
->lock
);
1506 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1507 mutex_unlock(&dev
->lock
);
1512 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1513 struct v4l2_tuner
*t
)
1515 struct em28xx_fh
*fh
= priv
;
1516 struct em28xx
*dev
= fh
->dev
;
1519 rc
= check_dev(dev
);
1526 mutex_lock(&dev
->lock
);
1527 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
1528 mutex_unlock(&dev
->lock
);
1533 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1534 struct v4l2_frequency
*f
)
1536 struct em28xx_fh
*fh
= priv
;
1537 struct em28xx
*dev
= fh
->dev
;
1539 mutex_lock(&dev
->lock
);
1540 f
->type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1541 f
->frequency
= dev
->ctl_freq
;
1542 mutex_unlock(&dev
->lock
);
1547 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1548 struct v4l2_frequency
*f
)
1550 struct em28xx_fh
*fh
= priv
;
1551 struct em28xx
*dev
= fh
->dev
;
1554 rc
= check_dev(dev
);
1561 if (unlikely(0 == fh
->radio
&& f
->type
!= V4L2_TUNER_ANALOG_TV
))
1563 if (unlikely(1 == fh
->radio
&& f
->type
!= V4L2_TUNER_RADIO
))
1566 mutex_lock(&dev
->lock
);
1568 dev
->ctl_freq
= f
->frequency
;
1569 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, f
);
1571 mutex_unlock(&dev
->lock
);
1576 #ifdef CONFIG_VIDEO_ADV_DEBUG
1577 static int em28xx_reg_len(int reg
)
1580 case EM28XX_R40_AC97LSB
:
1581 case EM28XX_R30_HSCALELOW
:
1582 case EM28XX_R32_VSCALELOW
:
1589 static int vidioc_g_chip_ident(struct file
*file
, void *priv
,
1590 struct v4l2_dbg_chip_ident
*chip
)
1592 struct em28xx_fh
*fh
= priv
;
1593 struct em28xx
*dev
= fh
->dev
;
1595 chip
->ident
= V4L2_IDENT_NONE
;
1598 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_chip_ident
, chip
);
1604 static int vidioc_g_register(struct file
*file
, void *priv
,
1605 struct v4l2_dbg_register
*reg
)
1607 struct em28xx_fh
*fh
= priv
;
1608 struct em28xx
*dev
= fh
->dev
;
1611 switch (reg
->match
.type
) {
1612 case V4L2_CHIP_MATCH_AC97
:
1613 mutex_lock(&dev
->lock
);
1614 ret
= em28xx_read_ac97(dev
, reg
->reg
);
1615 mutex_unlock(&dev
->lock
);
1622 case V4L2_CHIP_MATCH_I2C_DRIVER
:
1623 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_register
, reg
);
1625 case V4L2_CHIP_MATCH_I2C_ADDR
:
1626 /* TODO: is this correct? */
1627 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_register
, reg
);
1630 if (!v4l2_chip_match_host(®
->match
))
1635 reg
->size
= em28xx_reg_len(reg
->reg
);
1636 if (reg
->size
== 1) {
1637 mutex_lock(&dev
->lock
);
1638 ret
= em28xx_read_reg(dev
, reg
->reg
);
1639 mutex_unlock(&dev
->lock
);
1647 mutex_lock(&dev
->lock
);
1648 ret
= em28xx_read_reg_req_len(dev
, USB_REQ_GET_STATUS
,
1649 reg
->reg
, (char *)&val
, 2);
1650 mutex_unlock(&dev
->lock
);
1654 reg
->val
= le16_to_cpu(val
);
1660 static int vidioc_s_register(struct file
*file
, void *priv
,
1661 struct v4l2_dbg_register
*reg
)
1663 struct em28xx_fh
*fh
= priv
;
1664 struct em28xx
*dev
= fh
->dev
;
1668 switch (reg
->match
.type
) {
1669 case V4L2_CHIP_MATCH_AC97
:
1670 mutex_lock(&dev
->lock
);
1671 rc
= em28xx_write_ac97(dev
, reg
->reg
, reg
->val
);
1672 mutex_unlock(&dev
->lock
);
1675 case V4L2_CHIP_MATCH_I2C_DRIVER
:
1676 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_register
, reg
);
1678 case V4L2_CHIP_MATCH_I2C_ADDR
:
1679 /* TODO: is this correct? */
1680 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_register
, reg
);
1683 if (!v4l2_chip_match_host(®
->match
))
1688 buf
= cpu_to_le16(reg
->val
);
1690 mutex_lock(&dev
->lock
);
1691 rc
= em28xx_write_regs(dev
, reg
->reg
, (char *)&buf
,
1692 em28xx_reg_len(reg
->reg
));
1693 mutex_unlock(&dev
->lock
);
1700 static int vidioc_cropcap(struct file
*file
, void *priv
,
1701 struct v4l2_cropcap
*cc
)
1703 struct em28xx_fh
*fh
= priv
;
1704 struct em28xx
*dev
= fh
->dev
;
1706 if (cc
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1709 cc
->bounds
.left
= 0;
1711 cc
->bounds
.width
= dev
->width
;
1712 cc
->bounds
.height
= dev
->height
;
1713 cc
->defrect
= cc
->bounds
;
1714 cc
->pixelaspect
.numerator
= 54; /* 4:3 FIXME: remove magic numbers */
1715 cc
->pixelaspect
.denominator
= 59;
1720 static int vidioc_streamon(struct file
*file
, void *priv
,
1721 enum v4l2_buf_type type
)
1723 struct em28xx_fh
*fh
= priv
;
1724 struct em28xx
*dev
= fh
->dev
;
1727 rc
= check_dev(dev
);
1731 if (unlikely(type
!= fh
->type
))
1734 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1735 fh
, type
, fh
->resources
, dev
->resources
);
1737 if (unlikely(!res_get(fh
, get_ressource(fh
))))
1740 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1741 rc
= videobuf_streamon(&fh
->vb_vidq
);
1742 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1743 rc
= videobuf_streamon(&fh
->vb_vbiq
);
1748 static int vidioc_streamoff(struct file
*file
, void *priv
,
1749 enum v4l2_buf_type type
)
1751 struct em28xx_fh
*fh
= priv
;
1752 struct em28xx
*dev
= fh
->dev
;
1755 rc
= check_dev(dev
);
1759 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1760 fh
->type
!= V4L2_BUF_TYPE_VBI_CAPTURE
)
1762 if (type
!= fh
->type
)
1765 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1766 fh
, type
, fh
->resources
, dev
->resources
);
1768 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1769 videobuf_streamoff(&fh
->vb_vidq
);
1770 res_free(fh
, EM28XX_RESOURCE_VIDEO
);
1771 } else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1772 videobuf_streamoff(&fh
->vb_vbiq
);
1773 res_free(fh
, EM28XX_RESOURCE_VBI
);
1779 static int vidioc_querycap(struct file
*file
, void *priv
,
1780 struct v4l2_capability
*cap
)
1782 struct em28xx_fh
*fh
= priv
;
1783 struct em28xx
*dev
= fh
->dev
;
1785 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
1786 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
, sizeof(cap
->card
));
1787 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
1789 cap
->version
= EM28XX_VERSION_CODE
;
1792 V4L2_CAP_SLICED_VBI_CAPTURE
|
1793 V4L2_CAP_VIDEO_CAPTURE
|
1794 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1797 cap
->capabilities
|= V4L2_CAP_VBI_CAPTURE
;
1799 if (dev
->audio_mode
.has_audio
)
1800 cap
->capabilities
|= V4L2_CAP_AUDIO
;
1802 if (dev
->tuner_type
!= TUNER_ABSENT
)
1803 cap
->capabilities
|= V4L2_CAP_TUNER
;
1808 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1809 struct v4l2_fmtdesc
*f
)
1811 if (unlikely(f
->index
>= ARRAY_SIZE(format
)))
1814 strlcpy(f
->description
, format
[f
->index
].name
, sizeof(f
->description
));
1815 f
->pixelformat
= format
[f
->index
].fourcc
;
1820 /* Sliced VBI ioctls */
1821 static int vidioc_g_fmt_sliced_vbi_cap(struct file
*file
, void *priv
,
1822 struct v4l2_format
*f
)
1824 struct em28xx_fh
*fh
= priv
;
1825 struct em28xx
*dev
= fh
->dev
;
1828 rc
= check_dev(dev
);
1832 mutex_lock(&dev
->lock
);
1834 f
->fmt
.sliced
.service_set
= 0;
1835 v4l2_device_call_all(&dev
->v4l2_dev
, 0, vbi
, g_sliced_fmt
, &f
->fmt
.sliced
);
1837 if (f
->fmt
.sliced
.service_set
== 0)
1840 mutex_unlock(&dev
->lock
);
1845 static int vidioc_try_set_sliced_vbi_cap(struct file
*file
, void *priv
,
1846 struct v4l2_format
*f
)
1848 struct em28xx_fh
*fh
= priv
;
1849 struct em28xx
*dev
= fh
->dev
;
1852 rc
= check_dev(dev
);
1856 mutex_lock(&dev
->lock
);
1857 v4l2_device_call_all(&dev
->v4l2_dev
, 0, vbi
, g_sliced_fmt
, &f
->fmt
.sliced
);
1858 mutex_unlock(&dev
->lock
);
1860 if (f
->fmt
.sliced
.service_set
== 0)
1866 /* RAW VBI ioctls */
1868 static int vidioc_g_fmt_vbi_cap(struct file
*file
, void *priv
,
1869 struct v4l2_format
*format
)
1871 struct em28xx_fh
*fh
= priv
;
1872 struct em28xx
*dev
= fh
->dev
;
1874 format
->fmt
.vbi
.samples_per_line
= dev
->vbi_width
;
1875 format
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1876 format
->fmt
.vbi
.offset
= 0;
1877 format
->fmt
.vbi
.flags
= 0;
1878 format
->fmt
.vbi
.sampling_rate
= 6750000 * 4 / 2;
1879 format
->fmt
.vbi
.count
[0] = dev
->vbi_height
;
1880 format
->fmt
.vbi
.count
[1] = dev
->vbi_height
;
1882 /* Varies by video standard (NTSC, PAL, etc.) */
1883 if (dev
->norm
& V4L2_STD_525_60
) {
1885 format
->fmt
.vbi
.start
[0] = 10;
1886 format
->fmt
.vbi
.start
[1] = 273;
1887 } else if (dev
->norm
& V4L2_STD_625_50
) {
1889 format
->fmt
.vbi
.start
[0] = 6;
1890 format
->fmt
.vbi
.start
[1] = 318;
1896 static int vidioc_s_fmt_vbi_cap(struct file
*file
, void *priv
,
1897 struct v4l2_format
*format
)
1899 struct em28xx_fh
*fh
= priv
;
1900 struct em28xx
*dev
= fh
->dev
;
1902 format
->fmt
.vbi
.samples_per_line
= dev
->vbi_width
;
1903 format
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1904 format
->fmt
.vbi
.offset
= 0;
1905 format
->fmt
.vbi
.flags
= 0;
1906 format
->fmt
.vbi
.sampling_rate
= 6750000 * 4 / 2;
1907 format
->fmt
.vbi
.count
[0] = dev
->vbi_height
;
1908 format
->fmt
.vbi
.count
[1] = dev
->vbi_height
;
1910 /* Varies by video standard (NTSC, PAL, etc.) */
1911 if (dev
->norm
& V4L2_STD_525_60
) {
1913 format
->fmt
.vbi
.start
[0] = 10;
1914 format
->fmt
.vbi
.start
[1] = 273;
1915 } else if (dev
->norm
& V4L2_STD_625_50
) {
1917 format
->fmt
.vbi
.start
[0] = 6;
1918 format
->fmt
.vbi
.start
[1] = 318;
1924 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1925 struct v4l2_requestbuffers
*rb
)
1927 struct em28xx_fh
*fh
= priv
;
1928 struct em28xx
*dev
= fh
->dev
;
1931 rc
= check_dev(dev
);
1935 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1936 return videobuf_reqbufs(&fh
->vb_vidq
, rb
);
1938 return videobuf_reqbufs(&fh
->vb_vbiq
, rb
);
1941 static int vidioc_querybuf(struct file
*file
, void *priv
,
1942 struct v4l2_buffer
*b
)
1944 struct em28xx_fh
*fh
= priv
;
1945 struct em28xx
*dev
= fh
->dev
;
1948 rc
= check_dev(dev
);
1952 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1953 return videobuf_querybuf(&fh
->vb_vidq
, b
);
1955 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1956 the videobuf framework, but we probably shouldn't be
1957 returning a buffer larger than that which was asked for.
1958 At a minimum, it causes a crash in zvbi since it does
1959 a memcpy based on the source buffer length */
1960 int result
= videobuf_querybuf(&fh
->vb_vbiq
, b
);
1961 b
->length
= dev
->vbi_width
* dev
->vbi_height
* 2;
1967 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1969 struct em28xx_fh
*fh
= priv
;
1970 struct em28xx
*dev
= fh
->dev
;
1973 rc
= check_dev(dev
);
1977 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1978 return videobuf_qbuf(&fh
->vb_vidq
, b
);
1980 return videobuf_qbuf(&fh
->vb_vbiq
, b
);
1983 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1985 struct em28xx_fh
*fh
= priv
;
1986 struct em28xx
*dev
= fh
->dev
;
1989 rc
= check_dev(dev
);
1993 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1994 return videobuf_dqbuf(&fh
->vb_vidq
, b
, file
->f_flags
&
1997 return videobuf_dqbuf(&fh
->vb_vbiq
, b
, file
->f_flags
&
2001 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2002 static int vidiocgmbuf(struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
2004 struct em28xx_fh
*fh
= priv
;
2006 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2007 return videobuf_cgmbuf(&fh
->vb_vidq
, mbuf
, 8);
2009 return videobuf_cgmbuf(&fh
->vb_vbiq
, mbuf
, 8);
2014 /* ----------------------------------------------------------- */
2015 /* RADIO ESPECIFIC IOCTLS */
2016 /* ----------------------------------------------------------- */
2018 static int radio_querycap(struct file
*file
, void *priv
,
2019 struct v4l2_capability
*cap
)
2021 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
2023 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
2024 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
, sizeof(cap
->card
));
2025 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
2027 cap
->version
= EM28XX_VERSION_CODE
;
2028 cap
->capabilities
= V4L2_CAP_TUNER
;
2032 static int radio_g_tuner(struct file
*file
, void *priv
,
2033 struct v4l2_tuner
*t
)
2035 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
2037 if (unlikely(t
->index
> 0))
2040 strcpy(t
->name
, "Radio");
2041 t
->type
= V4L2_TUNER_RADIO
;
2043 mutex_lock(&dev
->lock
);
2044 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
2045 mutex_unlock(&dev
->lock
);
2050 static int radio_enum_input(struct file
*file
, void *priv
,
2051 struct v4l2_input
*i
)
2055 strcpy(i
->name
, "Radio");
2056 i
->type
= V4L2_INPUT_TYPE_TUNER
;
2061 static int radio_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
2063 if (unlikely(a
->index
))
2066 strcpy(a
->name
, "Radio");
2070 static int radio_s_tuner(struct file
*file
, void *priv
,
2071 struct v4l2_tuner
*t
)
2073 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
2078 mutex_lock(&dev
->lock
);
2079 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
2080 mutex_unlock(&dev
->lock
);
2085 static int radio_s_audio(struct file
*file
, void *fh
,
2086 struct v4l2_audio
*a
)
2091 static int radio_s_input(struct file
*file
, void *fh
, unsigned int i
)
2096 static int radio_queryctrl(struct file
*file
, void *priv
,
2097 struct v4l2_queryctrl
*qc
)
2101 if (qc
->id
< V4L2_CID_BASE
||
2102 qc
->id
>= V4L2_CID_LASTP1
)
2105 for (i
= 0; i
< ARRAY_SIZE(ac97_qctrl
); i
++) {
2106 if (qc
->id
&& qc
->id
== ac97_qctrl
[i
].id
) {
2107 memcpy(qc
, &(ac97_qctrl
[i
]), sizeof(*qc
));
2116 * em28xx_v4l2_open()
2117 * inits the device and starts isoc transfer
2119 static int em28xx_v4l2_open(struct file
*filp
)
2121 int errCode
= 0, radio
= 0;
2122 struct video_device
*vdev
= video_devdata(filp
);
2123 struct em28xx
*dev
= video_drvdata(filp
);
2124 enum v4l2_buf_type fh_type
= 0;
2125 struct em28xx_fh
*fh
;
2126 enum v4l2_field field
;
2128 switch (vdev
->vfl_type
) {
2129 case VFL_TYPE_GRABBER
:
2130 fh_type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2133 fh_type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
2135 case VFL_TYPE_RADIO
:
2140 mutex_lock(&dev
->lock
);
2142 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2143 video_device_node_name(vdev
), v4l2_type_names
[fh_type
],
2147 fh
= kzalloc(sizeof(struct em28xx_fh
), GFP_KERNEL
);
2149 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
2150 mutex_unlock(&dev
->lock
);
2156 filp
->private_data
= fh
;
2158 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&& dev
->users
== 0) {
2159 em28xx_set_mode(dev
, EM28XX_ANALOG_MODE
);
2160 em28xx_set_alternate(dev
);
2161 em28xx_resolution_set(dev
);
2163 /* Needed, since GPIO might have disabled power of
2166 em28xx_wake_i2c(dev
);
2170 em28xx_videodbg("video_open: setting radio device\n");
2171 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_radio
);
2176 if (dev
->progressive
)
2177 field
= V4L2_FIELD_NONE
;
2179 field
= V4L2_FIELD_INTERLACED
;
2181 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &em28xx_video_qops
,
2183 V4L2_BUF_TYPE_VIDEO_CAPTURE
, field
,
2184 sizeof(struct em28xx_buffer
), fh
);
2186 videobuf_queue_vmalloc_init(&fh
->vb_vbiq
, &em28xx_vbi_qops
,
2188 V4L2_BUF_TYPE_VBI_CAPTURE
,
2190 sizeof(struct em28xx_buffer
), fh
);
2192 mutex_unlock(&dev
->lock
);
2198 * em28xx_realease_resources()
2199 * unregisters the v4l2,i2c and usb devices
2200 * called when the device gets disconected or at module unload
2202 void em28xx_release_analog_resources(struct em28xx
*dev
)
2205 /*FIXME: I2C IR should be disconnected */
2207 if (dev
->radio_dev
) {
2208 if (video_is_registered(dev
->radio_dev
))
2209 video_unregister_device(dev
->radio_dev
);
2211 video_device_release(dev
->radio_dev
);
2212 dev
->radio_dev
= NULL
;
2215 em28xx_info("V4L2 device %s deregistered\n",
2216 video_device_node_name(dev
->vbi_dev
));
2217 if (video_is_registered(dev
->vbi_dev
))
2218 video_unregister_device(dev
->vbi_dev
);
2220 video_device_release(dev
->vbi_dev
);
2221 dev
->vbi_dev
= NULL
;
2224 em28xx_info("V4L2 device %s deregistered\n",
2225 video_device_node_name(dev
->vdev
));
2226 if (video_is_registered(dev
->vdev
))
2227 video_unregister_device(dev
->vdev
);
2229 video_device_release(dev
->vdev
);
2235 * em28xx_v4l2_close()
2236 * stops streaming and deallocates all resources allocated by the v4l2
2239 static int em28xx_v4l2_close(struct file
*filp
)
2241 struct em28xx_fh
*fh
= filp
->private_data
;
2242 struct em28xx
*dev
= fh
->dev
;
2245 em28xx_videodbg("users=%d\n", dev
->users
);
2247 if (res_check(fh
, EM28XX_RESOURCE_VIDEO
)) {
2248 videobuf_stop(&fh
->vb_vidq
);
2249 res_free(fh
, EM28XX_RESOURCE_VIDEO
);
2252 if (res_check(fh
, EM28XX_RESOURCE_VBI
)) {
2253 videobuf_stop(&fh
->vb_vbiq
);
2254 res_free(fh
, EM28XX_RESOURCE_VBI
);
2257 if (dev
->users
== 1) {
2258 /* the device is already disconnect,
2259 free the remaining resources */
2260 if (dev
->state
& DEV_DISCONNECTED
) {
2261 em28xx_release_resources(dev
);
2266 /* Save some power by putting tuner to sleep */
2267 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_power
, 0);
2269 /* do this before setting alternate! */
2270 em28xx_uninit_isoc(dev
);
2271 em28xx_set_mode(dev
, EM28XX_SUSPEND
);
2273 /* set alternate 0 */
2275 em28xx_videodbg("setting alternate 0\n");
2276 errCode
= usb_set_interface(dev
->udev
, 0, 0);
2278 em28xx_errdev("cannot change alternate number to "
2279 "0 (error=%i)\n", errCode
);
2283 videobuf_mmap_free(&fh
->vb_vidq
);
2284 videobuf_mmap_free(&fh
->vb_vbiq
);
2287 wake_up_interruptible_nr(&dev
->open
, 1);
2292 * em28xx_v4l2_read()
2293 * will allocate buffers when called for the first time
2296 em28xx_v4l2_read(struct file
*filp
, char __user
*buf
, size_t count
,
2299 struct em28xx_fh
*fh
= filp
->private_data
;
2300 struct em28xx
*dev
= fh
->dev
;
2303 rc
= check_dev(dev
);
2307 /* FIXME: read() is not prepared to allow changing the video
2308 resolution while streaming. Seems a bug at em28xx_set_fmt
2311 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
2312 if (res_locked(dev
, EM28XX_RESOURCE_VIDEO
))
2315 return videobuf_read_stream(&fh
->vb_vidq
, buf
, count
, pos
, 0,
2316 filp
->f_flags
& O_NONBLOCK
);
2320 if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
2321 if (!res_get(fh
, EM28XX_RESOURCE_VBI
))
2324 return videobuf_read_stream(&fh
->vb_vbiq
, buf
, count
, pos
, 0,
2325 filp
->f_flags
& O_NONBLOCK
);
2332 * em28xx_v4l2_poll()
2333 * will allocate buffers when called for the first time
2335 static unsigned int em28xx_v4l2_poll(struct file
*filp
, poll_table
*wait
)
2337 struct em28xx_fh
*fh
= filp
->private_data
;
2338 struct em28xx
*dev
= fh
->dev
;
2341 rc
= check_dev(dev
);
2345 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
2346 if (!res_get(fh
, EM28XX_RESOURCE_VIDEO
))
2348 return videobuf_poll_stream(filp
, &fh
->vb_vidq
, wait
);
2349 } else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
2350 if (!res_get(fh
, EM28XX_RESOURCE_VBI
))
2352 return videobuf_poll_stream(filp
, &fh
->vb_vbiq
, wait
);
2359 * em28xx_v4l2_mmap()
2361 static int em28xx_v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
2363 struct em28xx_fh
*fh
= filp
->private_data
;
2364 struct em28xx
*dev
= fh
->dev
;
2367 rc
= check_dev(dev
);
2371 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2372 rc
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
2373 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
2374 rc
= videobuf_mmap_mapper(&fh
->vb_vbiq
, vma
);
2376 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2377 (unsigned long)vma
->vm_start
,
2378 (unsigned long)vma
->vm_end
-(unsigned long)vma
->vm_start
,
2384 static const struct v4l2_file_operations em28xx_v4l_fops
= {
2385 .owner
= THIS_MODULE
,
2386 .open
= em28xx_v4l2_open
,
2387 .release
= em28xx_v4l2_close
,
2388 .read
= em28xx_v4l2_read
,
2389 .poll
= em28xx_v4l2_poll
,
2390 .mmap
= em28xx_v4l2_mmap
,
2391 .ioctl
= video_ioctl2
,
2394 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
2395 .vidioc_querycap
= vidioc_querycap
,
2396 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
2397 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
2398 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
2399 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
2400 .vidioc_g_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
2401 .vidioc_s_fmt_vbi_cap
= vidioc_s_fmt_vbi_cap
,
2402 .vidioc_g_audio
= vidioc_g_audio
,
2403 .vidioc_s_audio
= vidioc_s_audio
,
2404 .vidioc_cropcap
= vidioc_cropcap
,
2406 .vidioc_g_fmt_sliced_vbi_cap
= vidioc_g_fmt_sliced_vbi_cap
,
2407 .vidioc_try_fmt_sliced_vbi_cap
= vidioc_try_set_sliced_vbi_cap
,
2408 .vidioc_s_fmt_sliced_vbi_cap
= vidioc_try_set_sliced_vbi_cap
,
2410 .vidioc_reqbufs
= vidioc_reqbufs
,
2411 .vidioc_querybuf
= vidioc_querybuf
,
2412 .vidioc_qbuf
= vidioc_qbuf
,
2413 .vidioc_dqbuf
= vidioc_dqbuf
,
2414 .vidioc_g_std
= vidioc_g_std
,
2415 .vidioc_s_std
= vidioc_s_std
,
2416 .vidioc_g_parm
= vidioc_g_parm
,
2417 .vidioc_s_parm
= vidioc_s_parm
,
2418 .vidioc_enum_input
= vidioc_enum_input
,
2419 .vidioc_g_input
= vidioc_g_input
,
2420 .vidioc_s_input
= vidioc_s_input
,
2421 .vidioc_queryctrl
= vidioc_queryctrl
,
2422 .vidioc_g_ctrl
= vidioc_g_ctrl
,
2423 .vidioc_s_ctrl
= vidioc_s_ctrl
,
2424 .vidioc_streamon
= vidioc_streamon
,
2425 .vidioc_streamoff
= vidioc_streamoff
,
2426 .vidioc_g_tuner
= vidioc_g_tuner
,
2427 .vidioc_s_tuner
= vidioc_s_tuner
,
2428 .vidioc_g_frequency
= vidioc_g_frequency
,
2429 .vidioc_s_frequency
= vidioc_s_frequency
,
2430 #ifdef CONFIG_VIDEO_ADV_DEBUG
2431 .vidioc_g_register
= vidioc_g_register
,
2432 .vidioc_s_register
= vidioc_s_register
,
2433 .vidioc_g_chip_ident
= vidioc_g_chip_ident
,
2435 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2436 .vidiocgmbuf
= vidiocgmbuf
,
2440 static const struct video_device em28xx_video_template
= {
2441 .fops
= &em28xx_v4l_fops
,
2442 .release
= video_device_release
,
2443 .ioctl_ops
= &video_ioctl_ops
,
2445 .tvnorms
= V4L2_STD_ALL
,
2446 .current_norm
= V4L2_STD_PAL
,
2449 static const struct v4l2_file_operations radio_fops
= {
2450 .owner
= THIS_MODULE
,
2451 .open
= em28xx_v4l2_open
,
2452 .release
= em28xx_v4l2_close
,
2453 .ioctl
= video_ioctl2
,
2456 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
2457 .vidioc_querycap
= radio_querycap
,
2458 .vidioc_g_tuner
= radio_g_tuner
,
2459 .vidioc_enum_input
= radio_enum_input
,
2460 .vidioc_g_audio
= radio_g_audio
,
2461 .vidioc_s_tuner
= radio_s_tuner
,
2462 .vidioc_s_audio
= radio_s_audio
,
2463 .vidioc_s_input
= radio_s_input
,
2464 .vidioc_queryctrl
= radio_queryctrl
,
2465 .vidioc_g_ctrl
= vidioc_g_ctrl
,
2466 .vidioc_s_ctrl
= vidioc_s_ctrl
,
2467 .vidioc_g_frequency
= vidioc_g_frequency
,
2468 .vidioc_s_frequency
= vidioc_s_frequency
,
2469 #ifdef CONFIG_VIDEO_ADV_DEBUG
2470 .vidioc_g_register
= vidioc_g_register
,
2471 .vidioc_s_register
= vidioc_s_register
,
2475 static struct video_device em28xx_radio_template
= {
2476 .name
= "em28xx-radio",
2477 .fops
= &radio_fops
,
2478 .ioctl_ops
= &radio_ioctl_ops
,
2481 /******************************** usb interface ******************************/
2485 static struct video_device
*em28xx_vdev_init(struct em28xx
*dev
,
2486 const struct video_device
*template,
2487 const char *type_name
)
2489 struct video_device
*vfd
;
2491 vfd
= video_device_alloc();
2496 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
2497 vfd
->release
= video_device_release
;
2498 vfd
->debug
= video_debug
;
2500 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s",
2501 dev
->name
, type_name
);
2503 video_set_drvdata(vfd
, dev
);
2507 int em28xx_register_analog_devices(struct em28xx
*dev
)
2512 printk(KERN_INFO
"%s: v4l2 driver version %d.%d.%d\n",
2514 (EM28XX_VERSION_CODE
>> 16) & 0xff,
2515 (EM28XX_VERSION_CODE
>> 8) & 0xff, EM28XX_VERSION_CODE
& 0xff);
2517 /* set default norm */
2518 dev
->norm
= em28xx_video_template
.current_norm
;
2519 dev
->interlaced
= EM28XX_INTERLACED_DEFAULT
;
2522 /* Analog specific initialization */
2523 dev
->format
= &format
[0];
2524 em28xx_set_video_format(dev
, format
[0].fourcc
,
2525 norm_maxw(dev
), norm_maxh(dev
));
2527 video_mux(dev
, dev
->ctl_input
);
2529 /* Audio defaults */
2533 /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
2534 val
= (u8
)em28xx_read_reg(dev
, EM28XX_R0F_XCLK
);
2535 em28xx_write_reg(dev
, EM28XX_R0F_XCLK
,
2536 (EM28XX_XCLK_AUDIO_UNMUTE
| val
));
2538 em28xx_set_outfmt(dev
);
2539 em28xx_colorlevels_set_default(dev
);
2540 em28xx_compression_disable(dev
);
2542 /* allocate and fill video video_device struct */
2543 dev
->vdev
= em28xx_vdev_init(dev
, &em28xx_video_template
, "video");
2545 em28xx_errdev("cannot allocate video_device.\n");
2549 /* register v4l2 video video_device */
2550 ret
= video_register_device(dev
->vdev
, VFL_TYPE_GRABBER
,
2551 video_nr
[dev
->devno
]);
2553 em28xx_errdev("unable to register video device (error=%i).\n",
2558 /* Allocate and fill vbi video_device struct */
2559 if (em28xx_vbi_supported(dev
) == 1) {
2560 dev
->vbi_dev
= em28xx_vdev_init(dev
, &em28xx_video_template
,
2563 /* register v4l2 vbi video_device */
2564 ret
= video_register_device(dev
->vbi_dev
, VFL_TYPE_VBI
,
2565 vbi_nr
[dev
->devno
]);
2567 em28xx_errdev("unable to register vbi device\n");
2572 if (em28xx_boards
[dev
->model
].radio
.type
== EM28XX_RADIO
) {
2573 dev
->radio_dev
= em28xx_vdev_init(dev
, &em28xx_radio_template
,
2575 if (!dev
->radio_dev
) {
2576 em28xx_errdev("cannot allocate video_device.\n");
2579 ret
= video_register_device(dev
->radio_dev
, VFL_TYPE_RADIO
,
2580 radio_nr
[dev
->devno
]);
2582 em28xx_errdev("can't register radio device\n");
2585 em28xx_info("Registered radio device as %s\n",
2586 video_device_node_name(dev
->radio_dev
));
2589 em28xx_info("V4L2 video device registered as %s\n",
2590 video_device_node_name(dev
->vdev
));
2593 em28xx_info("V4L2 VBI device registered as %s\n",
2594 video_device_node_name(dev
->vbi_dev
));