2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
5 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
7 Mauro Carvalho Chehab <mchehab@infradead.org>
8 Sascha Sommer <saschasommer@freenet.de>
10 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/bitmap.h>
33 #include <linux/usb.h>
34 #include <linux/i2c.h>
35 #include <linux/version.h>
37 #include <linux/mutex.h>
40 #include <media/v4l2-common.h>
41 #include <media/v4l2-ioctl.h>
42 #include <media/v4l2-chip-ident.h>
43 #include <media/msp3400.h>
44 #include <media/tuner.h>
46 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
47 "Markus Rechberger <mrechberger@gmail.com>, " \
48 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
49 "Sascha Sommer <saschasommer@freenet.de>"
51 #define DRIVER_DESC "Empia em28xx based USB video device driver"
52 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
54 #define em28xx_videodbg(fmt, arg...) do {\
56 printk(KERN_INFO "%s %s :"fmt, \
57 dev->name, __func__ , ##arg); } while (0)
59 static unsigned int isoc_debug
;
60 module_param(isoc_debug
, int, 0644);
61 MODULE_PARM_DESC(isoc_debug
, "enable debug messages [isoc transfers]");
63 #define em28xx_isocdbg(fmt, arg...) \
66 printk(KERN_INFO "%s %s :"fmt, \
67 dev->name, __func__ , ##arg); \
71 MODULE_AUTHOR(DRIVER_AUTHOR
);
72 MODULE_DESCRIPTION(DRIVER_DESC
);
73 MODULE_LICENSE("GPL");
75 static unsigned int video_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
76 static unsigned int vbi_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
77 static unsigned int radio_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
79 module_param_array(video_nr
, int, NULL
, 0444);
80 module_param_array(vbi_nr
, int, NULL
, 0444);
81 module_param_array(radio_nr
, int, NULL
, 0444);
82 MODULE_PARM_DESC(video_nr
, "video device numbers");
83 MODULE_PARM_DESC(vbi_nr
, "vbi device numbers");
84 MODULE_PARM_DESC(radio_nr
, "radio device numbers");
86 static unsigned int video_debug
;
87 module_param(video_debug
, int, 0644);
88 MODULE_PARM_DESC(video_debug
, "enable debug messages [video]");
90 /* supported video standards */
91 static struct em28xx_fmt format
[] = {
93 .name
= "16 bpp YUY2, 4:2:2, packed",
94 .fourcc
= V4L2_PIX_FMT_YUYV
,
96 .reg
= EM28XX_OUTFMT_YUV422_Y0UY1V
,
98 .name
= "16 bpp RGB 565, LE",
99 .fourcc
= V4L2_PIX_FMT_RGB565
,
101 .reg
= EM28XX_OUTFMT_RGB_16_656
,
103 .name
= "8 bpp Bayer BGBG..GRGR",
104 .fourcc
= V4L2_PIX_FMT_SBGGR8
,
106 .reg
= EM28XX_OUTFMT_RGB_8_BGBG
,
108 .name
= "8 bpp Bayer GRGR..BGBG",
109 .fourcc
= V4L2_PIX_FMT_SGRBG8
,
111 .reg
= EM28XX_OUTFMT_RGB_8_GRGR
,
113 .name
= "8 bpp Bayer GBGB..RGRG",
114 .fourcc
= V4L2_PIX_FMT_SGBRG8
,
116 .reg
= EM28XX_OUTFMT_RGB_8_GBGB
,
118 .name
= "12 bpp YUV411",
119 .fourcc
= V4L2_PIX_FMT_YUV411P
,
121 .reg
= EM28XX_OUTFMT_YUV411
,
125 /* supported controls */
126 /* Common to all boards */
127 static struct v4l2_queryctrl em28xx_qctrl
[] = {
129 .id
= V4L2_CID_AUDIO_VOLUME
,
130 .type
= V4L2_CTRL_TYPE_INTEGER
,
135 .default_value
= 0x1f,
138 .id
= V4L2_CID_AUDIO_MUTE
,
139 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
149 /* ------------------------------------------------------------------
150 DMA and thread functions
151 ------------------------------------------------------------------*/
154 * Announces that a buffer were filled and request the next
156 static inline void buffer_filled(struct em28xx
*dev
,
157 struct em28xx_dmaqueue
*dma_q
,
158 struct em28xx_buffer
*buf
)
160 /* Advice that buffer was filled */
161 em28xx_isocdbg("[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
162 buf
->vb
.state
= VIDEOBUF_DONE
;
163 buf
->vb
.field_count
++;
164 do_gettimeofday(&buf
->vb
.ts
);
166 dev
->isoc_ctl
.buf
= NULL
;
168 list_del(&buf
->vb
.queue
);
169 wake_up(&buf
->vb
.done
);
173 * Identify the buffer header type and properly handles
175 static void em28xx_copy_video(struct em28xx
*dev
,
176 struct em28xx_dmaqueue
*dma_q
,
177 struct em28xx_buffer
*buf
,
179 unsigned char *outp
, unsigned long len
)
181 void *fieldstart
, *startwrite
, *startread
;
182 int linesdone
, currlinedone
, offset
, lencopy
, remain
;
183 int bytesperline
= dev
->width
<< 1;
185 if (dma_q
->pos
+ len
> buf
->vb
.size
)
186 len
= buf
->vb
.size
- dma_q
->pos
;
188 if (p
[0] != 0x88 && p
[0] != 0x22) {
189 em28xx_isocdbg("frame is not complete\n");
197 /* Interlaces frame */
201 fieldstart
= outp
+ bytesperline
;
203 linesdone
= dma_q
->pos
/ bytesperline
;
204 currlinedone
= dma_q
->pos
% bytesperline
;
205 offset
= linesdone
* bytesperline
* 2 + currlinedone
;
206 startwrite
= fieldstart
+ offset
;
207 lencopy
= bytesperline
- currlinedone
;
208 lencopy
= lencopy
> remain
? remain
: lencopy
;
210 if ((char *)startwrite
+ lencopy
> (char *)outp
+ buf
->vb
.size
) {
211 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
212 ((char *)startwrite
+ lencopy
) -
213 ((char *)outp
+ buf
->vb
.size
));
214 remain
= (char *)outp
+ buf
->vb
.size
- (char *)startwrite
;
219 memcpy(startwrite
, startread
, lencopy
);
224 startwrite
+= lencopy
+ bytesperline
;
225 startread
+= lencopy
;
226 if (bytesperline
> remain
)
229 lencopy
= bytesperline
;
231 if ((char *)startwrite
+ lencopy
> (char *)outp
+
233 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
234 ((char *)startwrite
+ lencopy
) -
235 ((char *)outp
+ buf
->vb
.size
));
236 lencopy
= remain
= (char *)outp
+ buf
->vb
.size
-
242 memcpy(startwrite
, startread
, lencopy
);
250 static inline void print_err_status(struct em28xx
*dev
,
251 int packet
, int status
)
253 char *errmsg
= "Unknown";
257 errmsg
= "unlinked synchronuously";
260 errmsg
= "unlinked asynchronuously";
263 errmsg
= "Buffer error (overrun)";
266 errmsg
= "Stalled (device not responding)";
269 errmsg
= "Babble (bad cable?)";
272 errmsg
= "Bit-stuff error (bad cable?)";
275 errmsg
= "CRC/Timeout (could be anything)";
278 errmsg
= "Device does not respond";
282 em28xx_isocdbg("URB status %d [%s].\n", status
, errmsg
);
284 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
285 packet
, status
, errmsg
);
290 * video-buf generic routine to get the next available buffer
292 static inline void get_next_buf(struct em28xx_dmaqueue
*dma_q
,
293 struct em28xx_buffer
**buf
)
295 struct em28xx
*dev
= container_of(dma_q
, struct em28xx
, vidq
);
298 if (list_empty(&dma_q
->active
)) {
299 em28xx_isocdbg("No active queue to serve\n");
300 dev
->isoc_ctl
.buf
= NULL
;
305 /* Get the next buffer */
306 *buf
= list_entry(dma_q
->active
.next
, struct em28xx_buffer
, vb
.queue
);
308 /* Cleans up buffer - Usefull for testing for frame/URB loss */
309 outp
= videobuf_to_vmalloc(&(*buf
)->vb
);
310 memset(outp
, 0, (*buf
)->vb
.size
);
312 dev
->isoc_ctl
.buf
= *buf
;
318 * Controls the isoc copy of each urb packet
320 static inline int em28xx_isoc_copy(struct em28xx
*dev
, struct urb
*urb
)
322 struct em28xx_buffer
*buf
;
323 struct em28xx_dmaqueue
*dma_q
= urb
->context
;
324 unsigned char *outp
= NULL
;
325 int i
, len
= 0, rc
= 1;
331 if ((dev
->state
& DEV_DISCONNECTED
) || (dev
->state
& DEV_MISCONFIGURED
))
334 if (urb
->status
< 0) {
335 print_err_status(dev
, -1, urb
->status
);
336 if (urb
->status
== -ENOENT
)
340 buf
= dev
->isoc_ctl
.buf
;
342 outp
= videobuf_to_vmalloc(&buf
->vb
);
344 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
345 int status
= urb
->iso_frame_desc
[i
].status
;
348 print_err_status(dev
, i
, status
);
349 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
353 len
= urb
->iso_frame_desc
[i
].actual_length
- 4;
355 if (urb
->iso_frame_desc
[i
].actual_length
<= 0) {
356 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
359 if (urb
->iso_frame_desc
[i
].actual_length
>
361 em28xx_isocdbg("packet bigger than packet size");
365 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
367 /* FIXME: incomplete buffer checks where removed to make
368 logic simpler. Impacts of those changes should be evaluated
370 if (p
[0] == 0x33 && p
[1] == 0x95 && p
[2] == 0x00) {
371 em28xx_isocdbg("VBI HEADER!!!\n");
372 /* FIXME: Should add vbi copy */
375 if (p
[0] == 0x22 && p
[1] == 0x5a) {
376 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p
[2],
377 len
, (p
[2] & 1) ? "odd" : "even");
381 buffer_filled(dev
, dma_q
, buf
);
382 get_next_buf(dma_q
, &buf
);
386 outp
= videobuf_to_vmalloc(&buf
->vb
);
399 em28xx_copy_video(dev
, dma_q
, buf
, p
, outp
, len
);
404 /* ------------------------------------------------------------------
406 ------------------------------------------------------------------*/
409 buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
, unsigned int *size
)
411 struct em28xx_fh
*fh
= vq
->priv_data
;
412 struct em28xx
*dev
= fh
->dev
;
413 struct v4l2_frequency f
;
415 *size
= (fh
->dev
->width
* fh
->dev
->height
* dev
->format
->depth
+ 7) >> 3;
418 *count
= EM28XX_DEF_BUF
;
420 if (*count
< EM28XX_MIN_BUF
)
421 *count
= EM28XX_MIN_BUF
;
423 /* Ask tuner to go to analog or radio mode */
424 memset(&f
, 0, sizeof(f
));
425 f
.frequency
= dev
->ctl_freq
;
426 f
.type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
428 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, &f
);
433 /* This is called *without* dev->slock held; please keep it that way */
434 static void free_buffer(struct videobuf_queue
*vq
, struct em28xx_buffer
*buf
)
436 struct em28xx_fh
*fh
= vq
->priv_data
;
437 struct em28xx
*dev
= fh
->dev
;
438 unsigned long flags
= 0;
442 /* We used to wait for the buffer to finish here, but this didn't work
443 because, as we were keeping the state as VIDEOBUF_QUEUED,
444 videobuf_queue_cancel marked it as finished for us.
445 (Also, it could wedge forever if the hardware was misconfigured.)
447 This should be safe; by the time we get here, the buffer isn't
448 queued anymore. If we ever start marking the buffers as
449 VIDEOBUF_ACTIVE, it won't be, though.
451 spin_lock_irqsave(&dev
->slock
, flags
);
452 if (dev
->isoc_ctl
.buf
== buf
)
453 dev
->isoc_ctl
.buf
= NULL
;
454 spin_unlock_irqrestore(&dev
->slock
, flags
);
456 videobuf_vmalloc_free(&buf
->vb
);
457 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
461 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
462 enum v4l2_field field
)
464 struct em28xx_fh
*fh
= vq
->priv_data
;
465 struct em28xx_buffer
*buf
= container_of(vb
, struct em28xx_buffer
, vb
);
466 struct em28xx
*dev
= fh
->dev
;
467 int rc
= 0, urb_init
= 0;
469 buf
->vb
.size
= (fh
->dev
->width
* fh
->dev
->height
* dev
->format
->depth
+ 7) >> 3;
471 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
474 buf
->vb
.width
= dev
->width
;
475 buf
->vb
.height
= dev
->height
;
476 buf
->vb
.field
= field
;
478 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
479 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
484 if (!dev
->isoc_ctl
.num_bufs
)
488 rc
= em28xx_init_isoc(dev
, EM28XX_NUM_PACKETS
,
489 EM28XX_NUM_BUFS
, dev
->max_pkt_size
,
495 buf
->vb
.state
= VIDEOBUF_PREPARED
;
499 free_buffer(vq
, buf
);
504 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
506 struct em28xx_buffer
*buf
= container_of(vb
,
507 struct em28xx_buffer
,
509 struct em28xx_fh
*fh
= vq
->priv_data
;
510 struct em28xx
*dev
= fh
->dev
;
511 struct em28xx_dmaqueue
*vidq
= &dev
->vidq
;
513 buf
->vb
.state
= VIDEOBUF_QUEUED
;
514 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
518 static void buffer_release(struct videobuf_queue
*vq
,
519 struct videobuf_buffer
*vb
)
521 struct em28xx_buffer
*buf
= container_of(vb
,
522 struct em28xx_buffer
,
524 struct em28xx_fh
*fh
= vq
->priv_data
;
525 struct em28xx
*dev
= (struct em28xx
*)fh
->dev
;
527 em28xx_isocdbg("em28xx: called buffer_release\n");
529 free_buffer(vq
, buf
);
532 static struct videobuf_queue_ops em28xx_video_qops
= {
533 .buf_setup
= buffer_setup
,
534 .buf_prepare
= buffer_prepare
,
535 .buf_queue
= buffer_queue
,
536 .buf_release
= buffer_release
,
539 /********************* v4l2 interface **************************************/
541 static void video_mux(struct em28xx
*dev
, int index
)
543 dev
->ctl_input
= index
;
544 dev
->ctl_ainput
= INPUT(index
)->amux
;
545 dev
->ctl_aoutput
= INPUT(index
)->aout
;
547 if (!dev
->ctl_aoutput
)
548 dev
->ctl_aoutput
= EM28XX_AOUT_MASTER
;
550 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_routing
,
551 INPUT(index
)->vmux
, 0, 0);
553 if (dev
->board
.has_msp34xx
) {
554 if (dev
->i2s_speed
) {
555 v4l2_device_call_all(&dev
->v4l2_dev
, 0, audio
,
556 s_i2s_clock_freq
, dev
->i2s_speed
);
558 /* Note: this is msp3400 specific */
559 v4l2_device_call_all(&dev
->v4l2_dev
, 0, audio
, s_routing
,
560 dev
->ctl_ainput
, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1
), 0);
563 if (dev
->board
.adecoder
!= EM28XX_NOADECODER
) {
564 v4l2_device_call_all(&dev
->v4l2_dev
, 0, audio
, s_routing
,
565 dev
->ctl_ainput
, dev
->ctl_aoutput
, 0);
568 em28xx_audio_analog_set(dev
);
571 /* Usage lock check functions */
572 static int res_get(struct em28xx_fh
*fh
)
574 struct em28xx
*dev
= fh
->dev
;
577 /* This instance already has stream_on */
589 static int res_check(struct em28xx_fh
*fh
)
591 return fh
->stream_on
;
594 static void res_free(struct em28xx_fh
*fh
)
596 struct em28xx
*dev
= fh
->dev
;
604 * return the current saturation, brightness or contrast, mute state
606 static int em28xx_get_ctrl(struct em28xx
*dev
, struct v4l2_control
*ctrl
)
609 case V4L2_CID_AUDIO_MUTE
:
610 ctrl
->value
= dev
->mute
;
612 case V4L2_CID_AUDIO_VOLUME
:
613 ctrl
->value
= dev
->volume
;
622 * mute or set new saturation, brightness or contrast
624 static int em28xx_set_ctrl(struct em28xx
*dev
, const struct v4l2_control
*ctrl
)
627 case V4L2_CID_AUDIO_MUTE
:
628 if (ctrl
->value
!= dev
->mute
) {
629 dev
->mute
= ctrl
->value
;
630 return em28xx_audio_analog_set(dev
);
633 case V4L2_CID_AUDIO_VOLUME
:
634 dev
->volume
= ctrl
->value
;
635 return em28xx_audio_analog_set(dev
);
641 static int check_dev(struct em28xx
*dev
)
643 if (dev
->state
& DEV_DISCONNECTED
) {
644 em28xx_errdev("v4l2 ioctl: device not present\n");
648 if (dev
->state
& DEV_MISCONFIGURED
) {
649 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
650 "close and open it again\n");
656 static void get_scale(struct em28xx
*dev
,
657 unsigned int width
, unsigned int height
,
658 unsigned int *hscale
, unsigned int *vscale
)
660 unsigned int maxw
= norm_maxw(dev
);
661 unsigned int maxh
= norm_maxh(dev
);
663 *hscale
= (((unsigned long)maxw
) << 12) / width
- 4096L;
664 if (*hscale
>= 0x4000)
667 *vscale
= (((unsigned long)maxh
) << 12) / height
- 4096L;
668 if (*vscale
>= 0x4000)
672 /* ------------------------------------------------------------------
673 IOCTL vidioc handling
674 ------------------------------------------------------------------*/
676 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
677 struct v4l2_format
*f
)
679 struct em28xx_fh
*fh
= priv
;
680 struct em28xx
*dev
= fh
->dev
;
682 mutex_lock(&dev
->lock
);
684 f
->fmt
.pix
.width
= dev
->width
;
685 f
->fmt
.pix
.height
= dev
->height
;
686 f
->fmt
.pix
.pixelformat
= dev
->format
->fourcc
;
687 f
->fmt
.pix
.bytesperline
= (dev
->width
* dev
->format
->depth
+ 7) >> 3;
688 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
* dev
->height
;
689 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
691 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
692 f
->fmt
.pix
.field
= dev
->interlaced
?
693 V4L2_FIELD_INTERLACED
: V4L2_FIELD_TOP
;
695 mutex_unlock(&dev
->lock
);
699 static struct em28xx_fmt
*format_by_fourcc(unsigned int fourcc
)
703 for (i
= 0; i
< ARRAY_SIZE(format
); i
++)
704 if (format
[i
].fourcc
== fourcc
)
710 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
711 struct v4l2_format
*f
)
713 struct em28xx_fh
*fh
= priv
;
714 struct em28xx
*dev
= fh
->dev
;
715 unsigned int width
= f
->fmt
.pix
.width
;
716 unsigned int height
= f
->fmt
.pix
.height
;
717 unsigned int maxw
= norm_maxw(dev
);
718 unsigned int maxh
= norm_maxh(dev
);
719 unsigned int hscale
, vscale
;
720 struct em28xx_fmt
*fmt
;
722 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
724 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
725 f
->fmt
.pix
.pixelformat
);
729 if (dev
->board
.is_em2800
) {
730 /* the em2800 can only scale down to 50% */
731 height
= height
> (3 * maxh
/ 4) ? maxh
: maxh
/ 2;
732 width
= width
> (3 * maxw
/ 4) ? maxw
: maxw
/ 2;
733 /* According to empiatech support the MaxPacketSize is too small
734 * to support framesizes larger than 640x480 @ 30 fps or 640x576
735 * @ 25 fps. As this would cut of a part of the image we prefer
736 * 360x576 or 360x480 for now */
737 if (width
== maxw
&& height
== maxh
)
740 /* width must even because of the YUYV format
741 height must be even because of interlacing */
742 v4l_bound_align_image(&width
, 48, maxw
, 1, &height
, 32, maxh
, 1, 0);
745 get_scale(dev
, width
, height
, &hscale
, &vscale
);
747 width
= (((unsigned long)maxw
) << 12) / (hscale
+ 4096L);
748 height
= (((unsigned long)maxh
) << 12) / (vscale
+ 4096L);
750 f
->fmt
.pix
.width
= width
;
751 f
->fmt
.pix
.height
= height
;
752 f
->fmt
.pix
.pixelformat
= fmt
->fourcc
;
753 f
->fmt
.pix
.bytesperline
= (dev
->width
* fmt
->depth
+ 7) >> 3;
754 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
* height
;
755 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
756 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
761 static int em28xx_set_video_format(struct em28xx
*dev
, unsigned int fourcc
,
762 unsigned width
, unsigned height
)
764 struct em28xx_fmt
*fmt
;
766 fmt
= format_by_fourcc(fourcc
);
772 dev
->height
= height
;
774 /* set new image size */
775 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
777 em28xx_set_alternate(dev
);
778 em28xx_resolution_set(dev
);
783 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
784 struct v4l2_format
*f
)
786 struct em28xx_fh
*fh
= priv
;
787 struct em28xx
*dev
= fh
->dev
;
794 mutex_lock(&dev
->lock
);
796 vidioc_try_fmt_vid_cap(file
, priv
, f
);
798 if (videobuf_queue_is_busy(&fh
->vb_vidq
)) {
799 em28xx_errdev("%s queue busy\n", __func__
);
804 if (dev
->stream_on
&& !fh
->stream_on
) {
805 em28xx_errdev("%s device in use by another fh\n", __func__
);
810 rc
= em28xx_set_video_format(dev
, f
->fmt
.pix
.pixelformat
,
811 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
);
814 mutex_unlock(&dev
->lock
);
818 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
820 struct em28xx_fh
*fh
= priv
;
821 struct em28xx
*dev
= fh
->dev
;
822 struct v4l2_format f
;
829 mutex_lock(&dev
->lock
);
832 /* Adjusts width/height, if needed */
833 f
.fmt
.pix
.width
= dev
->width
;
834 f
.fmt
.pix
.height
= dev
->height
;
835 vidioc_try_fmt_vid_cap(file
, priv
, &f
);
837 /* set new image size */
838 dev
->width
= f
.fmt
.pix
.width
;
839 dev
->height
= f
.fmt
.pix
.height
;
840 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
842 em28xx_resolution_set(dev
);
843 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_std
, dev
->norm
);
845 mutex_unlock(&dev
->lock
);
849 static const char *iname
[] = {
850 [EM28XX_VMUX_COMPOSITE1
] = "Composite1",
851 [EM28XX_VMUX_COMPOSITE2
] = "Composite2",
852 [EM28XX_VMUX_COMPOSITE3
] = "Composite3",
853 [EM28XX_VMUX_COMPOSITE4
] = "Composite4",
854 [EM28XX_VMUX_SVIDEO
] = "S-Video",
855 [EM28XX_VMUX_TELEVISION
] = "Television",
856 [EM28XX_VMUX_CABLE
] = "Cable TV",
857 [EM28XX_VMUX_DVB
] = "DVB",
858 [EM28XX_VMUX_DEBUG
] = "for debug only",
861 static int vidioc_enum_input(struct file
*file
, void *priv
,
862 struct v4l2_input
*i
)
864 struct em28xx_fh
*fh
= priv
;
865 struct em28xx
*dev
= fh
->dev
;
869 if (n
>= MAX_EM28XX_INPUT
)
871 if (0 == INPUT(n
)->type
)
875 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
877 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
879 if ((EM28XX_VMUX_TELEVISION
== INPUT(n
)->type
) ||
880 (EM28XX_VMUX_CABLE
== INPUT(n
)->type
))
881 i
->type
= V4L2_INPUT_TYPE_TUNER
;
883 i
->std
= dev
->vdev
->tvnorms
;
888 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
890 struct em28xx_fh
*fh
= priv
;
891 struct em28xx
*dev
= fh
->dev
;
898 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
900 struct em28xx_fh
*fh
= priv
;
901 struct em28xx
*dev
= fh
->dev
;
908 if (i
>= MAX_EM28XX_INPUT
)
910 if (0 == INPUT(i
)->type
)
915 mutex_lock(&dev
->lock
);
916 video_mux(dev
, dev
->ctl_input
);
917 mutex_unlock(&dev
->lock
);
921 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
923 struct em28xx_fh
*fh
= priv
;
924 struct em28xx
*dev
= fh
->dev
;
927 case EM28XX_AMUX_VIDEO
:
928 strcpy(a
->name
, "Television");
930 case EM28XX_AMUX_LINE_IN
:
931 strcpy(a
->name
, "Line In");
933 case EM28XX_AMUX_VIDEO2
:
934 strcpy(a
->name
, "Television alt");
936 case EM28XX_AMUX_PHONE
:
937 strcpy(a
->name
, "Phone");
939 case EM28XX_AMUX_MIC
:
940 strcpy(a
->name
, "Mic");
943 strcpy(a
->name
, "CD");
945 case EM28XX_AMUX_AUX
:
946 strcpy(a
->name
, "Aux");
948 case EM28XX_AMUX_PCM_OUT
:
949 strcpy(a
->name
, "PCM");
955 a
->index
= dev
->ctl_ainput
;
956 a
->capability
= V4L2_AUDCAP_STEREO
;
961 static int vidioc_s_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
963 struct em28xx_fh
*fh
= priv
;
964 struct em28xx
*dev
= fh
->dev
;
967 if (a
->index
>= MAX_EM28XX_INPUT
)
969 if (0 == INPUT(a
->index
)->type
)
972 mutex_lock(&dev
->lock
);
974 dev
->ctl_ainput
= INPUT(a
->index
)->amux
;
975 dev
->ctl_aoutput
= INPUT(a
->index
)->aout
;
977 if (!dev
->ctl_aoutput
)
978 dev
->ctl_aoutput
= EM28XX_AOUT_MASTER
;
980 mutex_unlock(&dev
->lock
);
984 static int vidioc_queryctrl(struct file
*file
, void *priv
,
985 struct v4l2_queryctrl
*qc
)
987 struct em28xx_fh
*fh
= priv
;
988 struct em28xx
*dev
= fh
->dev
;
997 memset(qc
, 0, sizeof(*qc
));
1001 if (!dev
->board
.has_msp34xx
) {
1002 for (i
= 0; i
< ARRAY_SIZE(em28xx_qctrl
); i
++) {
1003 if (qc
->id
&& qc
->id
== em28xx_qctrl
[i
].id
) {
1004 memcpy(qc
, &(em28xx_qctrl
[i
]), sizeof(*qc
));
1010 mutex_lock(&dev
->lock
);
1011 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, queryctrl
, qc
);
1012 mutex_unlock(&dev
->lock
);
1020 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1021 struct v4l2_control
*ctrl
)
1023 struct em28xx_fh
*fh
= priv
;
1024 struct em28xx
*dev
= fh
->dev
;
1027 rc
= check_dev(dev
);
1032 mutex_lock(&dev
->lock
);
1034 if (dev
->board
.has_msp34xx
)
1035 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_ctrl
, ctrl
);
1037 rc
= em28xx_get_ctrl(dev
, ctrl
);
1039 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_ctrl
, ctrl
);
1044 mutex_unlock(&dev
->lock
);
1048 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1049 struct v4l2_control
*ctrl
)
1051 struct em28xx_fh
*fh
= priv
;
1052 struct em28xx
*dev
= fh
->dev
;
1056 rc
= check_dev(dev
);
1060 mutex_lock(&dev
->lock
);
1062 if (dev
->board
.has_msp34xx
)
1063 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_ctrl
, ctrl
);
1066 for (i
= 0; i
< ARRAY_SIZE(em28xx_qctrl
); i
++) {
1067 if (ctrl
->id
== em28xx_qctrl
[i
].id
) {
1068 if (ctrl
->value
< em28xx_qctrl
[i
].minimum
||
1069 ctrl
->value
> em28xx_qctrl
[i
].maximum
) {
1074 rc
= em28xx_set_ctrl(dev
, ctrl
);
1080 /* Control not found - try to send it to the attached devices */
1082 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_ctrl
, ctrl
);
1086 mutex_unlock(&dev
->lock
);
1090 static int vidioc_g_tuner(struct file
*file
, void *priv
,
1091 struct v4l2_tuner
*t
)
1093 struct em28xx_fh
*fh
= priv
;
1094 struct em28xx
*dev
= fh
->dev
;
1097 rc
= check_dev(dev
);
1104 strcpy(t
->name
, "Tuner");
1106 mutex_lock(&dev
->lock
);
1107 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1108 mutex_unlock(&dev
->lock
);
1113 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1114 struct v4l2_tuner
*t
)
1116 struct em28xx_fh
*fh
= priv
;
1117 struct em28xx
*dev
= fh
->dev
;
1120 rc
= check_dev(dev
);
1127 mutex_lock(&dev
->lock
);
1128 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
1129 mutex_unlock(&dev
->lock
);
1134 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1135 struct v4l2_frequency
*f
)
1137 struct em28xx_fh
*fh
= priv
;
1138 struct em28xx
*dev
= fh
->dev
;
1140 mutex_lock(&dev
->lock
);
1141 f
->type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1142 f
->frequency
= dev
->ctl_freq
;
1143 mutex_unlock(&dev
->lock
);
1148 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1149 struct v4l2_frequency
*f
)
1151 struct em28xx_fh
*fh
= priv
;
1152 struct em28xx
*dev
= fh
->dev
;
1155 rc
= check_dev(dev
);
1162 if (unlikely(0 == fh
->radio
&& f
->type
!= V4L2_TUNER_ANALOG_TV
))
1164 if (unlikely(1 == fh
->radio
&& f
->type
!= V4L2_TUNER_RADIO
))
1167 mutex_lock(&dev
->lock
);
1169 dev
->ctl_freq
= f
->frequency
;
1170 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, f
);
1172 mutex_unlock(&dev
->lock
);
1177 #ifdef CONFIG_VIDEO_ADV_DEBUG
1178 static int em28xx_reg_len(int reg
)
1181 case EM28XX_R40_AC97LSB
:
1182 case EM28XX_R30_HSCALELOW
:
1183 case EM28XX_R32_VSCALELOW
:
1190 static int vidioc_g_chip_ident(struct file
*file
, void *priv
,
1191 struct v4l2_dbg_chip_ident
*chip
)
1193 struct em28xx_fh
*fh
= priv
;
1194 struct em28xx
*dev
= fh
->dev
;
1196 chip
->ident
= V4L2_IDENT_NONE
;
1199 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_chip_ident
, chip
);
1205 static int vidioc_g_register(struct file
*file
, void *priv
,
1206 struct v4l2_dbg_register
*reg
)
1208 struct em28xx_fh
*fh
= priv
;
1209 struct em28xx
*dev
= fh
->dev
;
1212 switch (reg
->match
.type
) {
1213 case V4L2_CHIP_MATCH_AC97
:
1214 mutex_lock(&dev
->lock
);
1215 ret
= em28xx_read_ac97(dev
, reg
->reg
);
1216 mutex_unlock(&dev
->lock
);
1223 case V4L2_CHIP_MATCH_I2C_DRIVER
:
1224 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_register
, reg
);
1226 case V4L2_CHIP_MATCH_I2C_ADDR
:
1227 /* Not supported yet */
1230 if (!v4l2_chip_match_host(®
->match
))
1235 reg
->size
= em28xx_reg_len(reg
->reg
);
1236 if (reg
->size
== 1) {
1237 mutex_lock(&dev
->lock
);
1238 ret
= em28xx_read_reg(dev
, reg
->reg
);
1239 mutex_unlock(&dev
->lock
);
1247 mutex_lock(&dev
->lock
);
1248 ret
= em28xx_read_reg_req_len(dev
, USB_REQ_GET_STATUS
,
1249 reg
->reg
, (char *)&val
, 2);
1250 mutex_unlock(&dev
->lock
);
1254 reg
->val
= le16_to_cpu(val
);
1260 static int vidioc_s_register(struct file
*file
, void *priv
,
1261 struct v4l2_dbg_register
*reg
)
1263 struct em28xx_fh
*fh
= priv
;
1264 struct em28xx
*dev
= fh
->dev
;
1268 switch (reg
->match
.type
) {
1269 case V4L2_CHIP_MATCH_AC97
:
1270 mutex_lock(&dev
->lock
);
1271 rc
= em28xx_write_ac97(dev
, reg
->reg
, reg
->val
);
1272 mutex_unlock(&dev
->lock
);
1275 case V4L2_CHIP_MATCH_I2C_DRIVER
:
1276 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_register
, reg
);
1278 case V4L2_CHIP_MATCH_I2C_ADDR
:
1279 /* Not supported yet */
1282 if (!v4l2_chip_match_host(®
->match
))
1287 buf
= cpu_to_le16(reg
->val
);
1289 mutex_lock(&dev
->lock
);
1290 rc
= em28xx_write_regs(dev
, reg
->reg
, (char *)&buf
,
1291 em28xx_reg_len(reg
->reg
));
1292 mutex_unlock(&dev
->lock
);
1299 static int vidioc_cropcap(struct file
*file
, void *priv
,
1300 struct v4l2_cropcap
*cc
)
1302 struct em28xx_fh
*fh
= priv
;
1303 struct em28xx
*dev
= fh
->dev
;
1305 if (cc
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1308 cc
->bounds
.left
= 0;
1310 cc
->bounds
.width
= dev
->width
;
1311 cc
->bounds
.height
= dev
->height
;
1312 cc
->defrect
= cc
->bounds
;
1313 cc
->pixelaspect
.numerator
= 54; /* 4:3 FIXME: remove magic numbers */
1314 cc
->pixelaspect
.denominator
= 59;
1319 static int vidioc_streamon(struct file
*file
, void *priv
,
1320 enum v4l2_buf_type type
)
1322 struct em28xx_fh
*fh
= priv
;
1323 struct em28xx
*dev
= fh
->dev
;
1326 rc
= check_dev(dev
);
1331 mutex_lock(&dev
->lock
);
1334 if (likely(rc
>= 0))
1335 rc
= videobuf_streamon(&fh
->vb_vidq
);
1337 mutex_unlock(&dev
->lock
);
1342 static int vidioc_streamoff(struct file
*file
, void *priv
,
1343 enum v4l2_buf_type type
)
1345 struct em28xx_fh
*fh
= priv
;
1346 struct em28xx
*dev
= fh
->dev
;
1349 rc
= check_dev(dev
);
1353 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1355 if (type
!= fh
->type
)
1358 mutex_lock(&dev
->lock
);
1360 videobuf_streamoff(&fh
->vb_vidq
);
1363 mutex_unlock(&dev
->lock
);
1368 static int vidioc_querycap(struct file
*file
, void *priv
,
1369 struct v4l2_capability
*cap
)
1371 struct em28xx_fh
*fh
= priv
;
1372 struct em28xx
*dev
= fh
->dev
;
1374 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
1375 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
, sizeof(cap
->card
));
1376 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
1378 cap
->version
= EM28XX_VERSION_CODE
;
1381 V4L2_CAP_SLICED_VBI_CAPTURE
|
1382 V4L2_CAP_VIDEO_CAPTURE
|
1384 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1386 if (dev
->tuner_type
!= TUNER_ABSENT
)
1387 cap
->capabilities
|= V4L2_CAP_TUNER
;
1392 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1393 struct v4l2_fmtdesc
*f
)
1395 if (unlikely(f
->index
>= ARRAY_SIZE(format
)))
1398 strlcpy(f
->description
, format
[f
->index
].name
, sizeof(f
->description
));
1399 f
->pixelformat
= format
[f
->index
].fourcc
;
1404 /* Sliced VBI ioctls */
1405 static int vidioc_g_fmt_sliced_vbi_cap(struct file
*file
, void *priv
,
1406 struct v4l2_format
*f
)
1408 struct em28xx_fh
*fh
= priv
;
1409 struct em28xx
*dev
= fh
->dev
;
1412 rc
= check_dev(dev
);
1416 mutex_lock(&dev
->lock
);
1418 f
->fmt
.sliced
.service_set
= 0;
1419 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, g_fmt
, f
);
1421 if (f
->fmt
.sliced
.service_set
== 0)
1424 mutex_unlock(&dev
->lock
);
1429 static int vidioc_try_set_sliced_vbi_cap(struct file
*file
, void *priv
,
1430 struct v4l2_format
*f
)
1432 struct em28xx_fh
*fh
= priv
;
1433 struct em28xx
*dev
= fh
->dev
;
1436 rc
= check_dev(dev
);
1440 mutex_lock(&dev
->lock
);
1441 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, g_fmt
, f
);
1442 mutex_unlock(&dev
->lock
);
1444 if (f
->fmt
.sliced
.service_set
== 0)
1451 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1452 struct v4l2_requestbuffers
*rb
)
1454 struct em28xx_fh
*fh
= priv
;
1455 struct em28xx
*dev
= fh
->dev
;
1458 rc
= check_dev(dev
);
1462 return videobuf_reqbufs(&fh
->vb_vidq
, rb
);
1465 static int vidioc_querybuf(struct file
*file
, void *priv
,
1466 struct v4l2_buffer
*b
)
1468 struct em28xx_fh
*fh
= priv
;
1469 struct em28xx
*dev
= fh
->dev
;
1472 rc
= check_dev(dev
);
1476 return videobuf_querybuf(&fh
->vb_vidq
, b
);
1479 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1481 struct em28xx_fh
*fh
= priv
;
1482 struct em28xx
*dev
= fh
->dev
;
1485 rc
= check_dev(dev
);
1489 return videobuf_qbuf(&fh
->vb_vidq
, b
);
1492 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1494 struct em28xx_fh
*fh
= priv
;
1495 struct em28xx
*dev
= fh
->dev
;
1498 rc
= check_dev(dev
);
1502 return videobuf_dqbuf(&fh
->vb_vidq
, b
, file
->f_flags
& O_NONBLOCK
);
1505 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1506 static int vidiocgmbuf(struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
1508 struct em28xx_fh
*fh
= priv
;
1510 return videobuf_cgmbuf(&fh
->vb_vidq
, mbuf
, 8);
1515 /* ----------------------------------------------------------- */
1516 /* RADIO ESPECIFIC IOCTLS */
1517 /* ----------------------------------------------------------- */
1519 static int radio_querycap(struct file
*file
, void *priv
,
1520 struct v4l2_capability
*cap
)
1522 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
1524 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
1525 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
, sizeof(cap
->card
));
1526 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
1528 cap
->version
= EM28XX_VERSION_CODE
;
1529 cap
->capabilities
= V4L2_CAP_TUNER
;
1533 static int radio_g_tuner(struct file
*file
, void *priv
,
1534 struct v4l2_tuner
*t
)
1536 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
1538 if (unlikely(t
->index
> 0))
1541 strcpy(t
->name
, "Radio");
1542 t
->type
= V4L2_TUNER_RADIO
;
1544 mutex_lock(&dev
->lock
);
1545 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1546 mutex_unlock(&dev
->lock
);
1551 static int radio_enum_input(struct file
*file
, void *priv
,
1552 struct v4l2_input
*i
)
1556 strcpy(i
->name
, "Radio");
1557 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1562 static int radio_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1564 if (unlikely(a
->index
))
1567 strcpy(a
->name
, "Radio");
1571 static int radio_s_tuner(struct file
*file
, void *priv
,
1572 struct v4l2_tuner
*t
)
1574 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
1579 mutex_lock(&dev
->lock
);
1580 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
1581 mutex_unlock(&dev
->lock
);
1586 static int radio_s_audio(struct file
*file
, void *fh
,
1587 struct v4l2_audio
*a
)
1592 static int radio_s_input(struct file
*file
, void *fh
, unsigned int i
)
1597 static int radio_queryctrl(struct file
*file
, void *priv
,
1598 struct v4l2_queryctrl
*qc
)
1602 if (qc
->id
< V4L2_CID_BASE
||
1603 qc
->id
>= V4L2_CID_LASTP1
)
1606 for (i
= 0; i
< ARRAY_SIZE(em28xx_qctrl
); i
++) {
1607 if (qc
->id
&& qc
->id
== em28xx_qctrl
[i
].id
) {
1608 memcpy(qc
, &(em28xx_qctrl
[i
]), sizeof(*qc
));
1617 * em28xx_v4l2_open()
1618 * inits the device and starts isoc transfer
1620 static int em28xx_v4l2_open(struct file
*filp
)
1622 int minor
= video_devdata(filp
)->minor
;
1623 int errCode
= 0, radio
;
1625 enum v4l2_buf_type fh_type
;
1626 struct em28xx_fh
*fh
;
1628 dev
= em28xx_get_device(minor
, &fh_type
, &radio
);
1633 mutex_lock(&dev
->lock
);
1635 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1636 minor
, v4l2_type_names
[fh_type
], dev
->users
);
1639 fh
= kzalloc(sizeof(struct em28xx_fh
), GFP_KERNEL
);
1641 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1642 mutex_unlock(&dev
->lock
);
1648 filp
->private_data
= fh
;
1650 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&& dev
->users
== 0) {
1651 em28xx_set_mode(dev
, EM28XX_ANALOG_MODE
);
1652 em28xx_set_alternate(dev
);
1653 em28xx_resolution_set(dev
);
1655 /* Needed, since GPIO might have disabled power of
1658 em28xx_wake_i2c(dev
);
1662 em28xx_videodbg("video_open: setting radio device\n");
1663 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_radio
);
1668 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &em28xx_video_qops
,
1669 NULL
, &dev
->slock
, fh
->type
, V4L2_FIELD_INTERLACED
,
1670 sizeof(struct em28xx_buffer
), fh
);
1672 mutex_unlock(&dev
->lock
);
1678 * em28xx_realease_resources()
1679 * unregisters the v4l2,i2c and usb devices
1680 * called when the device gets disconected or at module unload
1682 void em28xx_release_analog_resources(struct em28xx
*dev
)
1685 /*FIXME: I2C IR should be disconnected */
1687 if (dev
->radio_dev
) {
1688 if (-1 != dev
->radio_dev
->minor
)
1689 video_unregister_device(dev
->radio_dev
);
1691 video_device_release(dev
->radio_dev
);
1692 dev
->radio_dev
= NULL
;
1695 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1697 if (-1 != dev
->vbi_dev
->minor
)
1698 video_unregister_device(dev
->vbi_dev
);
1700 video_device_release(dev
->vbi_dev
);
1701 dev
->vbi_dev
= NULL
;
1704 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1706 if (-1 != dev
->vdev
->minor
)
1707 video_unregister_device(dev
->vdev
);
1709 video_device_release(dev
->vdev
);
1715 * em28xx_v4l2_close()
1716 * stops streaming and deallocates all resources allocated by the v4l2
1719 static int em28xx_v4l2_close(struct file
*filp
)
1721 struct em28xx_fh
*fh
= filp
->private_data
;
1722 struct em28xx
*dev
= fh
->dev
;
1725 em28xx_videodbg("users=%d\n", dev
->users
);
1728 mutex_lock(&dev
->lock
);
1732 if (dev
->users
== 1) {
1733 videobuf_stop(&fh
->vb_vidq
);
1734 videobuf_mmap_free(&fh
->vb_vidq
);
1736 /* the device is already disconnect,
1737 free the remaining resources */
1738 if (dev
->state
& DEV_DISCONNECTED
) {
1739 em28xx_release_resources(dev
);
1740 mutex_unlock(&dev
->lock
);
1745 /* Save some power by putting tuner to sleep */
1746 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_standby
);
1748 /* do this before setting alternate! */
1749 em28xx_uninit_isoc(dev
);
1750 em28xx_set_mode(dev
, EM28XX_SUSPEND
);
1752 /* set alternate 0 */
1754 em28xx_videodbg("setting alternate 0\n");
1755 errCode
= usb_set_interface(dev
->udev
, 0, 0);
1757 em28xx_errdev("cannot change alternate number to "
1758 "0 (error=%i)\n", errCode
);
1763 wake_up_interruptible_nr(&dev
->open
, 1);
1764 mutex_unlock(&dev
->lock
);
1769 * em28xx_v4l2_read()
1770 * will allocate buffers when called for the first time
1773 em28xx_v4l2_read(struct file
*filp
, char __user
*buf
, size_t count
,
1776 struct em28xx_fh
*fh
= filp
->private_data
;
1777 struct em28xx
*dev
= fh
->dev
;
1780 rc
= check_dev(dev
);
1784 /* FIXME: read() is not prepared to allow changing the video
1785 resolution while streaming. Seems a bug at em28xx_set_fmt
1788 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1789 mutex_lock(&dev
->lock
);
1791 mutex_unlock(&dev
->lock
);
1793 if (unlikely(rc
< 0))
1796 return videobuf_read_stream(&fh
->vb_vidq
, buf
, count
, pos
, 0,
1797 filp
->f_flags
& O_NONBLOCK
);
1803 * em28xx_v4l2_poll()
1804 * will allocate buffers when called for the first time
1806 static unsigned int em28xx_v4l2_poll(struct file
*filp
, poll_table
*wait
)
1808 struct em28xx_fh
*fh
= filp
->private_data
;
1809 struct em28xx
*dev
= fh
->dev
;
1812 rc
= check_dev(dev
);
1816 mutex_lock(&dev
->lock
);
1818 mutex_unlock(&dev
->lock
);
1820 if (unlikely(rc
< 0))
1823 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= fh
->type
)
1826 return videobuf_poll_stream(filp
, &fh
->vb_vidq
, wait
);
1830 * em28xx_v4l2_mmap()
1832 static int em28xx_v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1834 struct em28xx_fh
*fh
= filp
->private_data
;
1835 struct em28xx
*dev
= fh
->dev
;
1838 rc
= check_dev(dev
);
1842 mutex_lock(&dev
->lock
);
1844 mutex_unlock(&dev
->lock
);
1846 if (unlikely(rc
< 0))
1849 rc
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1851 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1852 (unsigned long)vma
->vm_start
,
1853 (unsigned long)vma
->vm_end
-(unsigned long)vma
->vm_start
,
1859 static const struct v4l2_file_operations em28xx_v4l_fops
= {
1860 .owner
= THIS_MODULE
,
1861 .open
= em28xx_v4l2_open
,
1862 .release
= em28xx_v4l2_close
,
1863 .read
= em28xx_v4l2_read
,
1864 .poll
= em28xx_v4l2_poll
,
1865 .mmap
= em28xx_v4l2_mmap
,
1866 .ioctl
= video_ioctl2
,
1869 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1870 .vidioc_querycap
= vidioc_querycap
,
1871 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1872 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1873 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1874 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1875 .vidioc_g_audio
= vidioc_g_audio
,
1876 .vidioc_s_audio
= vidioc_s_audio
,
1877 .vidioc_cropcap
= vidioc_cropcap
,
1879 .vidioc_g_fmt_sliced_vbi_cap
= vidioc_g_fmt_sliced_vbi_cap
,
1880 .vidioc_try_fmt_sliced_vbi_cap
= vidioc_try_set_sliced_vbi_cap
,
1881 .vidioc_s_fmt_sliced_vbi_cap
= vidioc_try_set_sliced_vbi_cap
,
1883 .vidioc_reqbufs
= vidioc_reqbufs
,
1884 .vidioc_querybuf
= vidioc_querybuf
,
1885 .vidioc_qbuf
= vidioc_qbuf
,
1886 .vidioc_dqbuf
= vidioc_dqbuf
,
1887 .vidioc_s_std
= vidioc_s_std
,
1888 .vidioc_enum_input
= vidioc_enum_input
,
1889 .vidioc_g_input
= vidioc_g_input
,
1890 .vidioc_s_input
= vidioc_s_input
,
1891 .vidioc_queryctrl
= vidioc_queryctrl
,
1892 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1893 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1894 .vidioc_streamon
= vidioc_streamon
,
1895 .vidioc_streamoff
= vidioc_streamoff
,
1896 .vidioc_g_tuner
= vidioc_g_tuner
,
1897 .vidioc_s_tuner
= vidioc_s_tuner
,
1898 .vidioc_g_frequency
= vidioc_g_frequency
,
1899 .vidioc_s_frequency
= vidioc_s_frequency
,
1900 #ifdef CONFIG_VIDEO_ADV_DEBUG
1901 .vidioc_g_register
= vidioc_g_register
,
1902 .vidioc_s_register
= vidioc_s_register
,
1903 .vidioc_g_chip_ident
= vidioc_g_chip_ident
,
1905 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1906 .vidiocgmbuf
= vidiocgmbuf
,
1910 static const struct video_device em28xx_video_template
= {
1911 .fops
= &em28xx_v4l_fops
,
1912 .release
= video_device_release
,
1913 .ioctl_ops
= &video_ioctl_ops
,
1917 .tvnorms
= V4L2_STD_ALL
,
1918 .current_norm
= V4L2_STD_PAL
,
1921 static const struct v4l2_file_operations radio_fops
= {
1922 .owner
= THIS_MODULE
,
1923 .open
= em28xx_v4l2_open
,
1924 .release
= em28xx_v4l2_close
,
1925 .ioctl
= video_ioctl2
,
1928 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
1929 .vidioc_querycap
= radio_querycap
,
1930 .vidioc_g_tuner
= radio_g_tuner
,
1931 .vidioc_enum_input
= radio_enum_input
,
1932 .vidioc_g_audio
= radio_g_audio
,
1933 .vidioc_s_tuner
= radio_s_tuner
,
1934 .vidioc_s_audio
= radio_s_audio
,
1935 .vidioc_s_input
= radio_s_input
,
1936 .vidioc_queryctrl
= radio_queryctrl
,
1937 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1938 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1939 .vidioc_g_frequency
= vidioc_g_frequency
,
1940 .vidioc_s_frequency
= vidioc_s_frequency
,
1941 #ifdef CONFIG_VIDEO_ADV_DEBUG
1942 .vidioc_g_register
= vidioc_g_register
,
1943 .vidioc_s_register
= vidioc_s_register
,
1947 static struct video_device em28xx_radio_template
= {
1948 .name
= "em28xx-radio",
1949 .fops
= &radio_fops
,
1950 .ioctl_ops
= &radio_ioctl_ops
,
1954 /******************************** usb interface ******************************/
1958 static struct video_device
*em28xx_vdev_init(struct em28xx
*dev
,
1959 const struct video_device
*template,
1960 const char *type_name
)
1962 struct video_device
*vfd
;
1964 vfd
= video_device_alloc();
1970 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1971 vfd
->release
= video_device_release
;
1972 vfd
->debug
= video_debug
;
1974 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s",
1975 dev
->name
, type_name
);
1980 int em28xx_register_analog_devices(struct em28xx
*dev
)
1985 printk(KERN_INFO
"%s: v4l2 driver version %d.%d.%d\n",
1987 (EM28XX_VERSION_CODE
>> 16) & 0xff,
1988 (EM28XX_VERSION_CODE
>> 8) & 0xff, EM28XX_VERSION_CODE
& 0xff);
1990 /* set default norm */
1991 dev
->norm
= em28xx_video_template
.current_norm
;
1992 dev
->interlaced
= EM28XX_INTERLACED_DEFAULT
;
1995 /* Analog specific initialization */
1996 dev
->format
= &format
[0];
1997 em28xx_set_video_format(dev
, format
[0].fourcc
,
1998 norm_maxw(dev
), norm_maxh(dev
));
2000 video_mux(dev
, dev
->ctl_input
);
2002 /* Audio defaults */
2006 /* enable vbi capturing */
2008 /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
2009 val
= (u8
)em28xx_read_reg(dev
, EM28XX_R0F_XCLK
);
2010 em28xx_write_reg(dev
, EM28XX_R0F_XCLK
,
2011 (EM28XX_XCLK_AUDIO_UNMUTE
| val
));
2012 em28xx_write_reg(dev
, EM28XX_R11_VINCTRL
, 0x51);
2014 em28xx_set_outfmt(dev
);
2015 em28xx_colorlevels_set_default(dev
);
2016 em28xx_compression_disable(dev
);
2018 /* allocate and fill video video_device struct */
2019 dev
->vdev
= em28xx_vdev_init(dev
, &em28xx_video_template
, "video");
2021 em28xx_errdev("cannot allocate video_device.\n");
2025 /* register v4l2 video video_device */
2026 ret
= video_register_device(dev
->vdev
, VFL_TYPE_GRABBER
,
2027 video_nr
[dev
->devno
]);
2029 em28xx_errdev("unable to register video device (error=%i).\n",
2034 /* Allocate and fill vbi video_device struct */
2035 dev
->vbi_dev
= em28xx_vdev_init(dev
, &em28xx_video_template
, "vbi");
2037 /* register v4l2 vbi video_device */
2038 ret
= video_register_device(dev
->vbi_dev
, VFL_TYPE_VBI
,
2039 vbi_nr
[dev
->devno
]);
2041 em28xx_errdev("unable to register vbi device\n");
2045 if (em28xx_boards
[dev
->model
].radio
.type
== EM28XX_RADIO
) {
2046 dev
->radio_dev
= em28xx_vdev_init(dev
, &em28xx_radio_template
,
2048 if (!dev
->radio_dev
) {
2049 em28xx_errdev("cannot allocate video_device.\n");
2052 ret
= video_register_device(dev
->radio_dev
, VFL_TYPE_RADIO
,
2053 radio_nr
[dev
->devno
]);
2055 em28xx_errdev("can't register radio device\n");
2058 em28xx_info("Registered radio device as /dev/radio%d\n",
2059 dev
->radio_dev
->num
);
2062 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2063 dev
->vdev
->num
, dev
->vbi_dev
->num
);