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, 1)
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
= "16bpp YUY2, 4:2:2, packed",
94 .fourcc
= V4L2_PIX_FMT_YUYV
,
100 /* supported controls */
101 /* Common to all boards */
102 static struct v4l2_queryctrl em28xx_qctrl
[] = {
104 .id
= V4L2_CID_AUDIO_VOLUME
,
105 .type
= V4L2_CTRL_TYPE_INTEGER
,
110 .default_value
= 0x1f,
113 .id
= V4L2_CID_AUDIO_MUTE
,
114 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
124 /* ------------------------------------------------------------------
125 DMA and thread functions
126 ------------------------------------------------------------------*/
129 * Announces that a buffer were filled and request the next
131 static inline void buffer_filled(struct em28xx
*dev
,
132 struct em28xx_dmaqueue
*dma_q
,
133 struct em28xx_buffer
*buf
)
135 /* Advice that buffer was filled */
136 em28xx_isocdbg("[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
137 buf
->vb
.state
= VIDEOBUF_DONE
;
138 buf
->vb
.field_count
++;
139 do_gettimeofday(&buf
->vb
.ts
);
141 dev
->isoc_ctl
.buf
= NULL
;
143 list_del(&buf
->vb
.queue
);
144 wake_up(&buf
->vb
.done
);
148 * Identify the buffer header type and properly handles
150 static void em28xx_copy_video(struct em28xx
*dev
,
151 struct em28xx_dmaqueue
*dma_q
,
152 struct em28xx_buffer
*buf
,
154 unsigned char *outp
, unsigned long len
)
156 void *fieldstart
, *startwrite
, *startread
;
157 int linesdone
, currlinedone
, offset
, lencopy
, remain
;
158 int bytesperline
= dev
->width
<< 1;
160 if (dma_q
->pos
+ len
> buf
->vb
.size
)
161 len
= buf
->vb
.size
- dma_q
->pos
;
163 if (p
[0] != 0x88 && p
[0] != 0x22) {
164 em28xx_isocdbg("frame is not complete\n");
172 /* Interlaces frame */
176 fieldstart
= outp
+ bytesperline
;
178 linesdone
= dma_q
->pos
/ bytesperline
;
179 currlinedone
= dma_q
->pos
% bytesperline
;
180 offset
= linesdone
* bytesperline
* 2 + currlinedone
;
181 startwrite
= fieldstart
+ offset
;
182 lencopy
= bytesperline
- currlinedone
;
183 lencopy
= lencopy
> remain
? remain
: lencopy
;
185 if ((char *)startwrite
+ lencopy
> (char *)outp
+ buf
->vb
.size
) {
186 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
187 ((char *)startwrite
+ lencopy
) -
188 ((char *)outp
+ buf
->vb
.size
));
189 lencopy
= remain
= (char *)outp
+ buf
->vb
.size
- (char *)startwrite
;
193 memcpy(startwrite
, startread
, lencopy
);
198 startwrite
+= lencopy
+ bytesperline
;
199 startread
+= lencopy
;
200 if (bytesperline
> remain
)
203 lencopy
= bytesperline
;
205 if ((char *)startwrite
+ lencopy
> (char *)outp
+ buf
->vb
.size
) {
206 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
207 ((char *)startwrite
+ lencopy
) -
208 ((char *)outp
+ buf
->vb
.size
));
209 lencopy
= remain
= (char *)outp
+ buf
->vb
.size
-
215 memcpy(startwrite
, startread
, lencopy
);
223 static inline void print_err_status(struct em28xx
*dev
,
224 int packet
, int status
)
226 char *errmsg
= "Unknown";
230 errmsg
= "unlinked synchronuously";
233 errmsg
= "unlinked asynchronuously";
236 errmsg
= "Buffer error (overrun)";
239 errmsg
= "Stalled (device not responding)";
242 errmsg
= "Babble (bad cable?)";
245 errmsg
= "Bit-stuff error (bad cable?)";
248 errmsg
= "CRC/Timeout (could be anything)";
251 errmsg
= "Device does not respond";
255 em28xx_isocdbg("URB status %d [%s].\n", status
, errmsg
);
257 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
258 packet
, status
, errmsg
);
263 * video-buf generic routine to get the next available buffer
265 static inline void get_next_buf(struct em28xx_dmaqueue
*dma_q
,
266 struct em28xx_buffer
**buf
)
268 struct em28xx
*dev
= container_of(dma_q
, struct em28xx
, vidq
);
271 if (list_empty(&dma_q
->active
)) {
272 em28xx_isocdbg("No active queue to serve\n");
273 dev
->isoc_ctl
.buf
= NULL
;
278 /* Get the next buffer */
279 *buf
= list_entry(dma_q
->active
.next
, struct em28xx_buffer
, vb
.queue
);
281 /* Cleans up buffer - Usefull for testing for frame/URB loss */
282 outp
= videobuf_to_vmalloc(&(*buf
)->vb
);
283 memset(outp
, 0, (*buf
)->vb
.size
);
285 dev
->isoc_ctl
.buf
= *buf
;
291 * Controls the isoc copy of each urb packet
293 static inline int em28xx_isoc_copy(struct em28xx
*dev
, struct urb
*urb
)
295 struct em28xx_buffer
*buf
;
296 struct em28xx_dmaqueue
*dma_q
= urb
->context
;
297 unsigned char *outp
= NULL
;
298 int i
, len
= 0, rc
= 1;
304 if ((dev
->state
& DEV_DISCONNECTED
) || (dev
->state
& DEV_MISCONFIGURED
))
307 if (urb
->status
< 0) {
308 print_err_status(dev
, -1, urb
->status
);
309 if (urb
->status
== -ENOENT
)
313 buf
= dev
->isoc_ctl
.buf
;
315 outp
= videobuf_to_vmalloc(&buf
->vb
);
317 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
318 int status
= urb
->iso_frame_desc
[i
].status
;
321 print_err_status(dev
, i
, status
);
322 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
326 len
= urb
->iso_frame_desc
[i
].actual_length
- 4;
328 if (urb
->iso_frame_desc
[i
].actual_length
<= 0) {
329 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
332 if (urb
->iso_frame_desc
[i
].actual_length
>
334 em28xx_isocdbg("packet bigger than packet size");
338 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
340 /* FIXME: incomplete buffer checks where removed to make
341 logic simpler. Impacts of those changes should be evaluated
343 if (p
[0] == 0x33 && p
[1] == 0x95 && p
[2] == 0x00) {
344 em28xx_isocdbg("VBI HEADER!!!\n");
345 /* FIXME: Should add vbi copy */
348 if (p
[0] == 0x22 && p
[1] == 0x5a) {
349 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p
[2],
350 len
, (p
[2] & 1)? "odd" : "even");
354 buffer_filled(dev
, dma_q
, buf
);
355 get_next_buf(dma_q
, &buf
);
359 outp
= videobuf_to_vmalloc(&buf
->vb
);
372 em28xx_copy_video(dev
, dma_q
, buf
, p
, outp
, len
);
377 /* ------------------------------------------------------------------
379 ------------------------------------------------------------------*/
382 buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
, unsigned int *size
)
384 struct em28xx_fh
*fh
= vq
->priv_data
;
385 struct em28xx
*dev
= fh
->dev
;
386 struct v4l2_frequency f
;
388 *size
= (fh
->dev
->width
* fh
->dev
->height
* dev
->format
->depth
+ 7) >> 3;
391 *count
= EM28XX_DEF_BUF
;
393 if (*count
< EM28XX_MIN_BUF
)
394 *count
= EM28XX_MIN_BUF
;
396 /* Ask tuner to go to analog or radio mode */
397 memset(&f
, 0, sizeof(f
));
398 f
.frequency
= dev
->ctl_freq
;
399 f
.type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
401 em28xx_i2c_call_clients(dev
, VIDIOC_S_FREQUENCY
, &f
);
406 /* This is called *without* dev->slock held; please keep it that way */
407 static void free_buffer(struct videobuf_queue
*vq
, struct em28xx_buffer
*buf
)
409 struct em28xx_fh
*fh
= vq
->priv_data
;
410 struct em28xx
*dev
= fh
->dev
;
411 unsigned long flags
= 0;
415 /* We used to wait for the buffer to finish here, but this didn't work
416 because, as we were keeping the state as VIDEOBUF_QUEUED,
417 videobuf_queue_cancel marked it as finished for us.
418 (Also, it could wedge forever if the hardware was misconfigured.)
420 This should be safe; by the time we get here, the buffer isn't
421 queued anymore. If we ever start marking the buffers as
422 VIDEOBUF_ACTIVE, it won't be, though.
424 spin_lock_irqsave(&dev
->slock
, flags
);
425 if (dev
->isoc_ctl
.buf
== buf
)
426 dev
->isoc_ctl
.buf
= NULL
;
427 spin_unlock_irqrestore(&dev
->slock
, flags
);
429 videobuf_vmalloc_free(&buf
->vb
);
430 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
434 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
435 enum v4l2_field field
)
437 struct em28xx_fh
*fh
= vq
->priv_data
;
438 struct em28xx_buffer
*buf
= container_of(vb
, struct em28xx_buffer
, vb
);
439 struct em28xx
*dev
= fh
->dev
;
440 int rc
= 0, urb_init
= 0;
442 buf
->vb
.size
= (fh
->dev
->width
* fh
->dev
->height
* dev
->format
->depth
+ 7) >> 3;
444 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
447 buf
->vb
.width
= dev
->width
;
448 buf
->vb
.height
= dev
->height
;
449 buf
->vb
.field
= field
;
451 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
452 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
457 if (!dev
->isoc_ctl
.num_bufs
)
461 rc
= em28xx_init_isoc(dev
, EM28XX_NUM_PACKETS
,
462 EM28XX_NUM_BUFS
, dev
->max_pkt_size
,
468 buf
->vb
.state
= VIDEOBUF_PREPARED
;
472 free_buffer(vq
, buf
);
477 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
479 struct em28xx_buffer
*buf
= container_of(vb
, struct em28xx_buffer
, vb
);
480 struct em28xx_fh
*fh
= vq
->priv_data
;
481 struct em28xx
*dev
= fh
->dev
;
482 struct em28xx_dmaqueue
*vidq
= &dev
->vidq
;
484 buf
->vb
.state
= VIDEOBUF_QUEUED
;
485 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
489 static void buffer_release(struct videobuf_queue
*vq
,
490 struct videobuf_buffer
*vb
)
492 struct em28xx_buffer
*buf
= container_of(vb
, struct em28xx_buffer
, vb
);
493 struct em28xx_fh
*fh
= vq
->priv_data
;
494 struct em28xx
*dev
= (struct em28xx
*)fh
->dev
;
496 em28xx_isocdbg("em28xx: called buffer_release\n");
498 free_buffer(vq
, buf
);
501 static struct videobuf_queue_ops em28xx_video_qops
= {
502 .buf_setup
= buffer_setup
,
503 .buf_prepare
= buffer_prepare
,
504 .buf_queue
= buffer_queue
,
505 .buf_release
= buffer_release
,
508 /********************* v4l2 interface **************************************/
510 static void video_mux(struct em28xx
*dev
, int index
)
512 struct v4l2_routing route
;
514 route
.input
= INPUT(index
)->vmux
;
516 dev
->ctl_input
= index
;
517 dev
->ctl_ainput
= INPUT(index
)->amux
;
518 dev
->ctl_aoutput
= INPUT(index
)->aout
;
520 if (!dev
->ctl_aoutput
)
521 dev
->ctl_aoutput
= EM28XX_AOUT_MASTER
;
523 em28xx_i2c_call_clients(dev
, VIDIOC_INT_S_VIDEO_ROUTING
, &route
);
525 if (dev
->board
.has_msp34xx
) {
526 if (dev
->i2s_speed
) {
527 em28xx_i2c_call_clients(dev
, VIDIOC_INT_I2S_CLOCK_FREQ
,
530 route
.input
= dev
->ctl_ainput
;
531 route
.output
= MSP_OUTPUT(MSP_SC_IN_DSP_SCART1
);
532 /* Note: this is msp3400 specific */
533 em28xx_i2c_call_clients(dev
, VIDIOC_INT_S_AUDIO_ROUTING
,
537 em28xx_audio_analog_set(dev
);
540 /* Usage lock check functions */
541 static int res_get(struct em28xx_fh
*fh
)
543 struct em28xx
*dev
= fh
->dev
;
546 /* This instance already has stream_on */
558 static int res_check(struct em28xx_fh
*fh
)
560 return (fh
->stream_on
);
563 static void res_free(struct em28xx_fh
*fh
)
565 struct em28xx
*dev
= fh
->dev
;
573 * return the current saturation, brightness or contrast, mute state
575 static int em28xx_get_ctrl(struct em28xx
*dev
, struct v4l2_control
*ctrl
)
578 case V4L2_CID_AUDIO_MUTE
:
579 ctrl
->value
= dev
->mute
;
581 case V4L2_CID_AUDIO_VOLUME
:
582 ctrl
->value
= dev
->volume
;
591 * mute or set new saturation, brightness or contrast
593 static int em28xx_set_ctrl(struct em28xx
*dev
, const struct v4l2_control
*ctrl
)
596 case V4L2_CID_AUDIO_MUTE
:
597 if (ctrl
->value
!= dev
->mute
) {
598 dev
->mute
= ctrl
->value
;
599 return em28xx_audio_analog_set(dev
);
602 case V4L2_CID_AUDIO_VOLUME
:
603 dev
->volume
= ctrl
->value
;
604 return em28xx_audio_analog_set(dev
);
610 static int check_dev(struct em28xx
*dev
)
612 if (dev
->state
& DEV_DISCONNECTED
) {
613 em28xx_errdev("v4l2 ioctl: device not present\n");
617 if (dev
->state
& DEV_MISCONFIGURED
) {
618 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
619 "close and open it again\n");
625 static void get_scale(struct em28xx
*dev
,
626 unsigned int width
, unsigned int height
,
627 unsigned int *hscale
, unsigned int *vscale
)
629 unsigned int maxw
= norm_maxw(dev
);
630 unsigned int maxh
= norm_maxh(dev
);
632 *hscale
= (((unsigned long)maxw
) << 12) / width
- 4096L;
633 if (*hscale
>= 0x4000)
636 *vscale
= (((unsigned long)maxh
) << 12) / height
- 4096L;
637 if (*vscale
>= 0x4000)
641 /* ------------------------------------------------------------------
642 IOCTL vidioc handling
643 ------------------------------------------------------------------*/
645 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
646 struct v4l2_format
*f
)
648 struct em28xx_fh
*fh
= priv
;
649 struct em28xx
*dev
= fh
->dev
;
651 mutex_lock(&dev
->lock
);
653 f
->fmt
.pix
.width
= dev
->width
;
654 f
->fmt
.pix
.height
= dev
->height
;
655 f
->fmt
.pix
.pixelformat
= dev
->format
->fourcc
;
656 f
->fmt
.pix
.bytesperline
= (dev
->width
* dev
->format
->depth
+ 7) >> 3;
657 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
* dev
->height
;
658 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
660 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
661 f
->fmt
.pix
.field
= dev
->interlaced
?
662 V4L2_FIELD_INTERLACED
: V4L2_FIELD_TOP
;
664 mutex_unlock(&dev
->lock
);
668 static struct em28xx_fmt
*format_by_fourcc(unsigned int fourcc
)
672 for (i
= 0; i
< ARRAY_SIZE(format
); i
++)
673 if (format
[i
].fourcc
== fourcc
)
679 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
680 struct v4l2_format
*f
)
682 struct em28xx_fh
*fh
= priv
;
683 struct em28xx
*dev
= fh
->dev
;
684 int width
= f
->fmt
.pix
.width
;
685 int height
= f
->fmt
.pix
.height
;
686 unsigned int maxw
= norm_maxw(dev
);
687 unsigned int maxh
= norm_maxh(dev
);
688 unsigned int hscale
, vscale
;
689 struct em28xx_fmt
*fmt
;
691 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
693 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
694 f
->fmt
.pix
.pixelformat
);
698 /* width must even because of the YUYV format
699 height must be even because of interlacing */
703 if (unlikely(height
< 32))
705 if (unlikely(height
> maxh
))
707 if (unlikely(width
< 48))
709 if (unlikely(width
> maxw
))
712 if (dev
->board
.is_em2800
) {
713 /* the em2800 can only scale down to 50% */
714 if (height
% (maxh
/ 2))
716 if (width
% (maxw
/ 2))
718 /* according to empiatech support */
719 /* the MaxPacketSize is to small to support */
720 /* framesizes larger than 640x480 @ 30 fps */
721 /* or 640x576 @ 25 fps. As this would cut */
722 /* of a part of the image we prefer */
723 /* 360x576 or 360x480 for now */
724 if (width
== maxw
&& height
== maxh
)
728 get_scale(dev
, width
, height
, &hscale
, &vscale
);
730 width
= (((unsigned long)maxw
) << 12) / (hscale
+ 4096L);
731 height
= (((unsigned long)maxh
) << 12) / (vscale
+ 4096L);
733 f
->fmt
.pix
.width
= width
;
734 f
->fmt
.pix
.height
= height
;
735 f
->fmt
.pix
.pixelformat
= fmt
->fourcc
;
736 f
->fmt
.pix
.bytesperline
= (dev
->width
* fmt
->depth
+ 7) >> 3;
737 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
* height
;
738 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
739 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
744 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
745 struct v4l2_format
*f
)
747 struct em28xx_fh
*fh
= priv
;
748 struct em28xx
*dev
= fh
->dev
;
750 struct em28xx_fmt
*fmt
;
756 mutex_lock(&dev
->lock
);
758 vidioc_try_fmt_vid_cap(file
, priv
, f
);
760 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
766 if (videobuf_queue_is_busy(&fh
->vb_vidq
)) {
767 em28xx_errdev("%s queue busy\n", __func__
);
772 if (dev
->stream_on
&& !fh
->stream_on
) {
773 em28xx_errdev("%s device in use by another fh\n", __func__
);
778 /* set new image size */
779 dev
->width
= f
->fmt
.pix
.width
;
780 dev
->height
= f
->fmt
.pix
.height
;
782 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
784 em28xx_set_alternate(dev
);
785 em28xx_resolution_set(dev
);
790 mutex_unlock(&dev
->lock
);
794 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
* norm
)
796 struct em28xx_fh
*fh
= priv
;
797 struct em28xx
*dev
= fh
->dev
;
798 struct v4l2_format f
;
805 mutex_lock(&dev
->lock
);
808 /* Adjusts width/height, if needed */
809 f
.fmt
.pix
.width
= dev
->width
;
810 f
.fmt
.pix
.height
= dev
->height
;
811 vidioc_try_fmt_vid_cap(file
, priv
, &f
);
813 /* set new image size */
814 dev
->width
= f
.fmt
.pix
.width
;
815 dev
->height
= f
.fmt
.pix
.height
;
816 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
818 em28xx_resolution_set(dev
);
819 em28xx_i2c_call_clients(dev
, VIDIOC_S_STD
, &dev
->norm
);
821 mutex_unlock(&dev
->lock
);
825 static const char *iname
[] = {
826 [EM28XX_VMUX_COMPOSITE1
] = "Composite1",
827 [EM28XX_VMUX_COMPOSITE2
] = "Composite2",
828 [EM28XX_VMUX_COMPOSITE3
] = "Composite3",
829 [EM28XX_VMUX_COMPOSITE4
] = "Composite4",
830 [EM28XX_VMUX_SVIDEO
] = "S-Video",
831 [EM28XX_VMUX_TELEVISION
] = "Television",
832 [EM28XX_VMUX_CABLE
] = "Cable TV",
833 [EM28XX_VMUX_DVB
] = "DVB",
834 [EM28XX_VMUX_DEBUG
] = "for debug only",
837 static int vidioc_enum_input(struct file
*file
, void *priv
,
838 struct v4l2_input
*i
)
840 struct em28xx_fh
*fh
= priv
;
841 struct em28xx
*dev
= fh
->dev
;
845 if (n
>= MAX_EM28XX_INPUT
)
847 if (0 == INPUT(n
)->type
)
851 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
853 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
855 if ((EM28XX_VMUX_TELEVISION
== INPUT(n
)->type
) ||
856 (EM28XX_VMUX_CABLE
== INPUT(n
)->type
))
857 i
->type
= V4L2_INPUT_TYPE_TUNER
;
859 i
->std
= dev
->vdev
->tvnorms
;
864 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
866 struct em28xx_fh
*fh
= priv
;
867 struct em28xx
*dev
= fh
->dev
;
874 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
876 struct em28xx_fh
*fh
= priv
;
877 struct em28xx
*dev
= fh
->dev
;
884 if (i
>= MAX_EM28XX_INPUT
)
886 if (0 == INPUT(i
)->type
)
889 mutex_lock(&dev
->lock
);
893 mutex_unlock(&dev
->lock
);
897 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
899 struct em28xx_fh
*fh
= priv
;
900 struct em28xx
*dev
= fh
->dev
;
903 case EM28XX_AMUX_VIDEO
:
904 strcpy(a
->name
, "Television");
906 case EM28XX_AMUX_LINE_IN
:
907 strcpy(a
->name
, "Line In");
909 case EM28XX_AMUX_VIDEO2
:
910 strcpy(a
->name
, "Television alt");
912 case EM28XX_AMUX_PHONE
:
913 strcpy(a
->name
, "Phone");
915 case EM28XX_AMUX_MIC
:
916 strcpy(a
->name
, "Mic");
919 strcpy(a
->name
, "CD");
921 case EM28XX_AMUX_AUX
:
922 strcpy(a
->name
, "Aux");
924 case EM28XX_AMUX_PCM_OUT
:
925 strcpy(a
->name
, "PCM");
931 a
->index
= dev
->ctl_ainput
;
932 a
->capability
= V4L2_AUDCAP_STEREO
;
937 static int vidioc_s_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
939 struct em28xx_fh
*fh
= priv
;
940 struct em28xx
*dev
= fh
->dev
;
942 mutex_lock(&dev
->lock
);
944 dev
->ctl_ainput
= INPUT(a
->index
)->amux
;
945 dev
->ctl_aoutput
= INPUT(a
->index
)->aout
;
947 if (!dev
->ctl_aoutput
)
948 dev
->ctl_aoutput
= EM28XX_AOUT_MASTER
;
950 mutex_unlock(&dev
->lock
);
954 static int vidioc_queryctrl(struct file
*file
, void *priv
,
955 struct v4l2_queryctrl
*qc
)
957 struct em28xx_fh
*fh
= priv
;
958 struct em28xx
*dev
= fh
->dev
;
967 memset(qc
, 0, sizeof(*qc
));
971 if (!dev
->board
.has_msp34xx
) {
972 for (i
= 0; i
< ARRAY_SIZE(em28xx_qctrl
); i
++) {
973 if (qc
->id
&& qc
->id
== em28xx_qctrl
[i
].id
) {
974 memcpy(qc
, &(em28xx_qctrl
[i
]), sizeof(*qc
));
979 mutex_lock(&dev
->lock
);
980 em28xx_i2c_call_clients(dev
, VIDIOC_QUERYCTRL
, qc
);
981 mutex_unlock(&dev
->lock
);
989 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
990 struct v4l2_control
*ctrl
)
992 struct em28xx_fh
*fh
= priv
;
993 struct em28xx
*dev
= fh
->dev
;
1001 mutex_lock(&dev
->lock
);
1003 if (dev
->board
.has_msp34xx
)
1004 em28xx_i2c_call_clients(dev
, VIDIOC_G_CTRL
, ctrl
);
1006 rc
= em28xx_get_ctrl(dev
, ctrl
);
1008 mutex_unlock(&dev
->lock
);
1012 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1013 struct v4l2_control
*ctrl
)
1015 struct em28xx_fh
*fh
= priv
;
1016 struct em28xx
*dev
= fh
->dev
;
1020 rc
= check_dev(dev
);
1024 mutex_lock(&dev
->lock
);
1026 if (dev
->board
.has_msp34xx
)
1027 em28xx_i2c_call_clients(dev
, VIDIOC_S_CTRL
, ctrl
);
1030 for (i
= 0; i
< ARRAY_SIZE(em28xx_qctrl
); i
++) {
1031 if (ctrl
->id
== em28xx_qctrl
[i
].id
) {
1032 if (ctrl
->value
< em28xx_qctrl
[i
].minimum
||
1033 ctrl
->value
> em28xx_qctrl
[i
].maximum
) {
1038 rc
= em28xx_set_ctrl(dev
, ctrl
);
1044 /* Control not found - try to send it to the attached devices */
1046 em28xx_i2c_call_clients(dev
, VIDIOC_S_CTRL
, ctrl
);
1050 mutex_unlock(&dev
->lock
);
1054 static int vidioc_g_tuner(struct file
*file
, void *priv
,
1055 struct v4l2_tuner
*t
)
1057 struct em28xx_fh
*fh
= priv
;
1058 struct em28xx
*dev
= fh
->dev
;
1061 rc
= check_dev(dev
);
1068 strcpy(t
->name
, "Tuner");
1070 mutex_lock(&dev
->lock
);
1072 em28xx_i2c_call_clients(dev
, VIDIOC_G_TUNER
, t
);
1074 mutex_unlock(&dev
->lock
);
1078 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1079 struct v4l2_tuner
*t
)
1081 struct em28xx_fh
*fh
= priv
;
1082 struct em28xx
*dev
= fh
->dev
;
1085 rc
= check_dev(dev
);
1092 mutex_lock(&dev
->lock
);
1094 em28xx_i2c_call_clients(dev
, VIDIOC_S_TUNER
, t
);
1096 mutex_unlock(&dev
->lock
);
1100 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1101 struct v4l2_frequency
*f
)
1103 struct em28xx_fh
*fh
= priv
;
1104 struct em28xx
*dev
= fh
->dev
;
1106 mutex_lock(&dev
->lock
);
1107 f
->type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1108 f
->frequency
= dev
->ctl_freq
;
1109 mutex_unlock(&dev
->lock
);
1114 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1115 struct v4l2_frequency
*f
)
1117 struct em28xx_fh
*fh
= priv
;
1118 struct em28xx
*dev
= fh
->dev
;
1121 rc
= check_dev(dev
);
1128 if (unlikely(0 == fh
->radio
&& f
->type
!= V4L2_TUNER_ANALOG_TV
))
1130 if (unlikely(1 == fh
->radio
&& f
->type
!= V4L2_TUNER_RADIO
))
1133 mutex_lock(&dev
->lock
);
1135 dev
->ctl_freq
= f
->frequency
;
1136 em28xx_i2c_call_clients(dev
, VIDIOC_S_FREQUENCY
, f
);
1138 mutex_unlock(&dev
->lock
);
1143 #ifdef CONFIG_VIDEO_ADV_DEBUG
1144 static int em28xx_reg_len(int reg
)
1147 case EM28XX_R40_AC97LSB
:
1148 case EM28XX_R30_HSCALELOW
:
1149 case EM28XX_R32_VSCALELOW
:
1156 static int vidioc_g_chip_ident(struct file
*file
, void *priv
,
1157 struct v4l2_chip_ident
*chip
)
1159 struct em28xx_fh
*fh
= priv
;
1160 struct em28xx
*dev
= fh
->dev
;
1162 chip
->ident
= V4L2_IDENT_NONE
;
1165 em28xx_i2c_call_clients(dev
, VIDIOC_G_CHIP_IDENT
, chip
);
1171 static int vidioc_g_register(struct file
*file
, void *priv
,
1172 struct v4l2_register
*reg
)
1174 struct em28xx_fh
*fh
= priv
;
1175 struct em28xx
*dev
= fh
->dev
;
1178 switch (reg
->match_type
) {
1179 case V4L2_CHIP_MATCH_AC97
:
1180 mutex_lock(&dev
->lock
);
1181 ret
= em28xx_read_ac97(dev
, reg
->reg
);
1182 mutex_unlock(&dev
->lock
);
1188 case V4L2_CHIP_MATCH_I2C_DRIVER
:
1189 em28xx_i2c_call_clients(dev
, VIDIOC_DBG_G_REGISTER
, reg
);
1191 case V4L2_CHIP_MATCH_I2C_ADDR
:
1192 /* Not supported yet */
1195 if (!v4l2_chip_match_host(reg
->match_type
, reg
->match_chip
))
1200 if (em28xx_reg_len(reg
->reg
) == 1) {
1201 mutex_lock(&dev
->lock
);
1202 ret
= em28xx_read_reg(dev
, reg
->reg
);
1203 mutex_unlock(&dev
->lock
);
1211 mutex_lock(&dev
->lock
);
1212 ret
= em28xx_read_reg_req_len(dev
, USB_REQ_GET_STATUS
,
1213 reg
->reg
, (char *)&val
, 2);
1214 mutex_unlock(&dev
->lock
);
1218 reg
->val
= le64_to_cpu(val
);
1224 static int vidioc_s_register(struct file
*file
, void *priv
,
1225 struct v4l2_register
*reg
)
1227 struct em28xx_fh
*fh
= priv
;
1228 struct em28xx
*dev
= fh
->dev
;
1232 switch (reg
->match_type
) {
1233 case V4L2_CHIP_MATCH_AC97
:
1234 mutex_lock(&dev
->lock
);
1235 rc
= em28xx_write_ac97(dev
, reg
->reg
, reg
->val
);
1236 mutex_unlock(&dev
->lock
);
1239 case V4L2_CHIP_MATCH_I2C_DRIVER
:
1240 em28xx_i2c_call_clients(dev
, VIDIOC_DBG_S_REGISTER
, reg
);
1242 case V4L2_CHIP_MATCH_I2C_ADDR
:
1243 /* Not supported yet */
1246 if (!v4l2_chip_match_host(reg
->match_type
, reg
->match_chip
))
1251 buf
= cpu_to_le64(reg
->val
);
1253 mutex_lock(&dev
->lock
);
1254 rc
= em28xx_write_regs(dev
, reg
->reg
, (char *)&buf
,
1255 em28xx_reg_len(reg
->reg
));
1256 mutex_unlock(&dev
->lock
);
1263 static int vidioc_cropcap(struct file
*file
, void *priv
,
1264 struct v4l2_cropcap
*cc
)
1266 struct em28xx_fh
*fh
= priv
;
1267 struct em28xx
*dev
= fh
->dev
;
1269 if (cc
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1272 cc
->bounds
.left
= 0;
1274 cc
->bounds
.width
= dev
->width
;
1275 cc
->bounds
.height
= dev
->height
;
1276 cc
->defrect
= cc
->bounds
;
1277 cc
->pixelaspect
.numerator
= 54; /* 4:3 FIXME: remove magic numbers */
1278 cc
->pixelaspect
.denominator
= 59;
1283 static int vidioc_streamon(struct file
*file
, void *priv
,
1284 enum v4l2_buf_type type
)
1286 struct em28xx_fh
*fh
= priv
;
1287 struct em28xx
*dev
= fh
->dev
;
1290 rc
= check_dev(dev
);
1295 mutex_lock(&dev
->lock
);
1298 if (likely(rc
>= 0))
1299 rc
= videobuf_streamon(&fh
->vb_vidq
);
1301 mutex_unlock(&dev
->lock
);
1306 static int vidioc_streamoff(struct file
*file
, void *priv
,
1307 enum v4l2_buf_type type
)
1309 struct em28xx_fh
*fh
= priv
;
1310 struct em28xx
*dev
= fh
->dev
;
1313 rc
= check_dev(dev
);
1317 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1319 if (type
!= fh
->type
)
1322 mutex_lock(&dev
->lock
);
1324 videobuf_streamoff(&fh
->vb_vidq
);
1327 mutex_unlock(&dev
->lock
);
1332 static int vidioc_querycap(struct file
*file
, void *priv
,
1333 struct v4l2_capability
*cap
)
1335 struct em28xx_fh
*fh
= priv
;
1336 struct em28xx
*dev
= fh
->dev
;
1338 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
1339 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
, sizeof(cap
->card
));
1340 strlcpy(cap
->bus_info
, dev_name(&dev
->udev
->dev
), sizeof(cap
->bus_info
));
1342 cap
->version
= EM28XX_VERSION_CODE
;
1345 V4L2_CAP_SLICED_VBI_CAPTURE
|
1346 V4L2_CAP_VIDEO_CAPTURE
|
1348 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1350 if (dev
->tuner_type
!= TUNER_ABSENT
)
1351 cap
->capabilities
|= V4L2_CAP_TUNER
;
1356 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1357 struct v4l2_fmtdesc
*f
)
1359 if (unlikely(f
->index
>= ARRAY_SIZE(format
)))
1362 strlcpy(f
->description
, format
[f
->index
].name
, sizeof(f
->description
));
1363 f
->pixelformat
= format
[f
->index
].fourcc
;
1368 /* Sliced VBI ioctls */
1369 static int vidioc_g_fmt_sliced_vbi_cap(struct file
*file
, void *priv
,
1370 struct v4l2_format
*f
)
1372 struct em28xx_fh
*fh
= priv
;
1373 struct em28xx
*dev
= fh
->dev
;
1376 rc
= check_dev(dev
);
1380 mutex_lock(&dev
->lock
);
1382 f
->fmt
.sliced
.service_set
= 0;
1384 em28xx_i2c_call_clients(dev
, VIDIOC_G_FMT
, f
);
1386 if (f
->fmt
.sliced
.service_set
== 0)
1389 mutex_unlock(&dev
->lock
);
1393 static int vidioc_try_set_sliced_vbi_cap(struct file
*file
, void *priv
,
1394 struct v4l2_format
*f
)
1396 struct em28xx_fh
*fh
= priv
;
1397 struct em28xx
*dev
= fh
->dev
;
1400 rc
= check_dev(dev
);
1404 mutex_lock(&dev
->lock
);
1405 em28xx_i2c_call_clients(dev
, VIDIOC_G_FMT
, f
);
1406 mutex_unlock(&dev
->lock
);
1408 if (f
->fmt
.sliced
.service_set
== 0)
1415 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1416 struct v4l2_requestbuffers
*rb
)
1418 struct em28xx_fh
*fh
= priv
;
1419 struct em28xx
*dev
= fh
->dev
;
1422 rc
= check_dev(dev
);
1426 return (videobuf_reqbufs(&fh
->vb_vidq
, rb
));
1429 static int vidioc_querybuf(struct file
*file
, void *priv
,
1430 struct v4l2_buffer
*b
)
1432 struct em28xx_fh
*fh
= priv
;
1433 struct em28xx
*dev
= fh
->dev
;
1436 rc
= check_dev(dev
);
1440 return (videobuf_querybuf(&fh
->vb_vidq
, b
));
1443 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1445 struct em28xx_fh
*fh
= priv
;
1446 struct em28xx
*dev
= fh
->dev
;
1449 rc
= check_dev(dev
);
1453 return (videobuf_qbuf(&fh
->vb_vidq
, b
));
1456 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1458 struct em28xx_fh
*fh
= priv
;
1459 struct em28xx
*dev
= fh
->dev
;
1462 rc
= check_dev(dev
);
1466 return (videobuf_dqbuf(&fh
->vb_vidq
, b
,
1467 file
->f_flags
& O_NONBLOCK
));
1470 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1471 static int vidiocgmbuf(struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
1473 struct em28xx_fh
*fh
= priv
;
1475 return videobuf_cgmbuf(&fh
->vb_vidq
, mbuf
, 8);
1480 /* ----------------------------------------------------------- */
1481 /* RADIO ESPECIFIC IOCTLS */
1482 /* ----------------------------------------------------------- */
1484 static int radio_querycap(struct file
*file
, void *priv
,
1485 struct v4l2_capability
*cap
)
1487 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
1489 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
1490 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
, sizeof(cap
->card
));
1491 strlcpy(cap
->bus_info
, dev_name(&dev
->udev
->dev
), sizeof(cap
->bus_info
));
1493 cap
->version
= EM28XX_VERSION_CODE
;
1494 cap
->capabilities
= V4L2_CAP_TUNER
;
1498 static int radio_g_tuner(struct file
*file
, void *priv
,
1499 struct v4l2_tuner
*t
)
1501 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
1503 if (unlikely(t
->index
> 0))
1506 strcpy(t
->name
, "Radio");
1507 t
->type
= V4L2_TUNER_RADIO
;
1509 mutex_lock(&dev
->lock
);
1510 em28xx_i2c_call_clients(dev
, VIDIOC_G_TUNER
, t
);
1511 mutex_unlock(&dev
->lock
);
1516 static int radio_enum_input(struct file
*file
, void *priv
,
1517 struct v4l2_input
*i
)
1521 strcpy(i
->name
, "Radio");
1522 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1527 static int radio_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1529 if (unlikely(a
->index
))
1532 strcpy(a
->name
, "Radio");
1536 static int radio_s_tuner(struct file
*file
, void *priv
,
1537 struct v4l2_tuner
*t
)
1539 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
1544 mutex_lock(&dev
->lock
);
1545 em28xx_i2c_call_clients(dev
, VIDIOC_S_TUNER
, t
);
1546 mutex_unlock(&dev
->lock
);
1551 static int radio_s_audio(struct file
*file
, void *fh
,
1552 struct v4l2_audio
*a
)
1557 static int radio_s_input(struct file
*file
, void *fh
, unsigned int i
)
1562 static int radio_queryctrl(struct file
*file
, void *priv
,
1563 struct v4l2_queryctrl
*qc
)
1567 if (qc
->id
< V4L2_CID_BASE
||
1568 qc
->id
>= V4L2_CID_LASTP1
)
1571 for (i
= 0; i
< ARRAY_SIZE(em28xx_qctrl
); i
++) {
1572 if (qc
->id
&& qc
->id
== em28xx_qctrl
[i
].id
) {
1573 memcpy(qc
, &(em28xx_qctrl
[i
]), sizeof(*qc
));
1582 * em28xx_v4l2_open()
1583 * inits the device and starts isoc transfer
1585 static int em28xx_v4l2_open(struct inode
*inode
, struct file
*filp
)
1587 int minor
= iminor(inode
);
1588 int errCode
= 0, radio
;
1590 enum v4l2_buf_type fh_type
;
1591 struct em28xx_fh
*fh
;
1593 dev
= em28xx_get_device(inode
, &fh_type
, &radio
);
1598 mutex_lock(&dev
->lock
);
1600 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1601 minor
, v4l2_type_names
[fh_type
], dev
->users
);
1604 fh
= kzalloc(sizeof(struct em28xx_fh
), GFP_KERNEL
);
1606 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1607 mutex_unlock(&dev
->lock
);
1613 filp
->private_data
= fh
;
1615 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&& dev
->users
== 0) {
1616 dev
->width
= norm_maxw(dev
);
1617 dev
->height
= norm_maxh(dev
);
1621 em28xx_set_mode(dev
, EM28XX_ANALOG_MODE
);
1622 em28xx_set_alternate(dev
);
1623 em28xx_resolution_set(dev
);
1625 /* Needed, since GPIO might have disabled power of
1628 em28xx_wake_i2c(dev
);
1632 em28xx_videodbg("video_open: setting radio device\n");
1633 em28xx_i2c_call_clients(dev
, AUDC_SET_RADIO
, NULL
);
1638 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &em28xx_video_qops
,
1639 NULL
, &dev
->slock
, fh
->type
, V4L2_FIELD_INTERLACED
,
1640 sizeof(struct em28xx_buffer
), fh
);
1642 mutex_unlock(&dev
->lock
);
1648 * em28xx_realease_resources()
1649 * unregisters the v4l2,i2c and usb devices
1650 * called when the device gets disconected or at module unload
1652 void em28xx_release_analog_resources(struct em28xx
*dev
)
1655 /*FIXME: I2C IR should be disconnected */
1657 if (dev
->radio_dev
) {
1658 if (-1 != dev
->radio_dev
->minor
)
1659 video_unregister_device(dev
->radio_dev
);
1661 video_device_release(dev
->radio_dev
);
1662 dev
->radio_dev
= NULL
;
1665 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1667 if (-1 != dev
->vbi_dev
->minor
)
1668 video_unregister_device(dev
->vbi_dev
);
1670 video_device_release(dev
->vbi_dev
);
1671 dev
->vbi_dev
= NULL
;
1674 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1676 if (-1 != dev
->vdev
->minor
)
1677 video_unregister_device(dev
->vdev
);
1679 video_device_release(dev
->vdev
);
1685 * em28xx_v4l2_close()
1686 * stops streaming and deallocates all resources allocated by the v4l2
1689 static int em28xx_v4l2_close(struct inode
*inode
, struct file
*filp
)
1691 struct em28xx_fh
*fh
= filp
->private_data
;
1692 struct em28xx
*dev
= fh
->dev
;
1695 em28xx_videodbg("users=%d\n", dev
->users
);
1698 mutex_lock(&dev
->lock
);
1702 if (dev
->users
== 1) {
1703 videobuf_stop(&fh
->vb_vidq
);
1704 videobuf_mmap_free(&fh
->vb_vidq
);
1706 /* the device is already disconnect,
1707 free the remaining resources */
1708 if (dev
->state
& DEV_DISCONNECTED
) {
1709 em28xx_release_resources(dev
);
1710 mutex_unlock(&dev
->lock
);
1715 /* Save some power by putting tuner to sleep */
1716 em28xx_i2c_call_clients(dev
, TUNER_SET_STANDBY
, NULL
);
1718 /* do this before setting alternate! */
1719 em28xx_uninit_isoc(dev
);
1720 em28xx_set_mode(dev
, EM28XX_SUSPEND
);
1722 /* set alternate 0 */
1724 em28xx_videodbg("setting alternate 0\n");
1725 errCode
= usb_set_interface(dev
->udev
, 0, 0);
1727 em28xx_errdev("cannot change alternate number to "
1728 "0 (error=%i)\n", errCode
);
1733 wake_up_interruptible_nr(&dev
->open
, 1);
1734 mutex_unlock(&dev
->lock
);
1739 * em28xx_v4l2_read()
1740 * will allocate buffers when called for the first time
1743 em28xx_v4l2_read(struct file
*filp
, char __user
*buf
, size_t count
,
1746 struct em28xx_fh
*fh
= filp
->private_data
;
1747 struct em28xx
*dev
= fh
->dev
;
1750 rc
= check_dev(dev
);
1754 /* FIXME: read() is not prepared to allow changing the video
1755 resolution while streaming. Seems a bug at em28xx_set_fmt
1758 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1759 mutex_lock(&dev
->lock
);
1761 mutex_unlock(&dev
->lock
);
1763 if (unlikely(rc
< 0))
1766 return videobuf_read_stream(&fh
->vb_vidq
, buf
, count
, pos
, 0,
1767 filp
->f_flags
& O_NONBLOCK
);
1773 * em28xx_v4l2_poll()
1774 * will allocate buffers when called for the first time
1776 static unsigned int em28xx_v4l2_poll(struct file
*filp
, poll_table
* wait
)
1778 struct em28xx_fh
*fh
= filp
->private_data
;
1779 struct em28xx
*dev
= fh
->dev
;
1782 rc
= check_dev(dev
);
1786 mutex_lock(&dev
->lock
);
1788 mutex_unlock(&dev
->lock
);
1790 if (unlikely(rc
< 0))
1793 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= fh
->type
)
1796 return videobuf_poll_stream(filp
, &fh
->vb_vidq
, wait
);
1800 * em28xx_v4l2_mmap()
1802 static int em28xx_v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1804 struct em28xx_fh
*fh
= filp
->private_data
;
1805 struct em28xx
*dev
= fh
->dev
;
1808 rc
= check_dev(dev
);
1812 mutex_lock(&dev
->lock
);
1814 mutex_unlock(&dev
->lock
);
1816 if (unlikely(rc
< 0))
1819 rc
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1821 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1822 (unsigned long)vma
->vm_start
,
1823 (unsigned long)vma
->vm_end
-(unsigned long)vma
->vm_start
,
1829 static const struct file_operations em28xx_v4l_fops
= {
1830 .owner
= THIS_MODULE
,
1831 .open
= em28xx_v4l2_open
,
1832 .release
= em28xx_v4l2_close
,
1833 .read
= em28xx_v4l2_read
,
1834 .poll
= em28xx_v4l2_poll
,
1835 .mmap
= em28xx_v4l2_mmap
,
1836 .ioctl
= video_ioctl2
,
1837 .llseek
= no_llseek
,
1838 .compat_ioctl
= v4l_compat_ioctl32
,
1841 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1842 .vidioc_querycap
= vidioc_querycap
,
1843 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1844 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1845 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1846 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1847 .vidioc_g_audio
= vidioc_g_audio
,
1848 .vidioc_s_audio
= vidioc_s_audio
,
1849 .vidioc_cropcap
= vidioc_cropcap
,
1851 .vidioc_g_fmt_sliced_vbi_cap
= vidioc_g_fmt_sliced_vbi_cap
,
1852 .vidioc_try_fmt_sliced_vbi_cap
= vidioc_try_set_sliced_vbi_cap
,
1853 .vidioc_s_fmt_sliced_vbi_cap
= vidioc_try_set_sliced_vbi_cap
,
1855 .vidioc_reqbufs
= vidioc_reqbufs
,
1856 .vidioc_querybuf
= vidioc_querybuf
,
1857 .vidioc_qbuf
= vidioc_qbuf
,
1858 .vidioc_dqbuf
= vidioc_dqbuf
,
1859 .vidioc_s_std
= vidioc_s_std
,
1860 .vidioc_enum_input
= vidioc_enum_input
,
1861 .vidioc_g_input
= vidioc_g_input
,
1862 .vidioc_s_input
= vidioc_s_input
,
1863 .vidioc_queryctrl
= vidioc_queryctrl
,
1864 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1865 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1866 .vidioc_streamon
= vidioc_streamon
,
1867 .vidioc_streamoff
= vidioc_streamoff
,
1868 .vidioc_g_tuner
= vidioc_g_tuner
,
1869 .vidioc_s_tuner
= vidioc_s_tuner
,
1870 .vidioc_g_frequency
= vidioc_g_frequency
,
1871 .vidioc_s_frequency
= vidioc_s_frequency
,
1872 #ifdef CONFIG_VIDEO_ADV_DEBUG
1873 .vidioc_g_register
= vidioc_g_register
,
1874 .vidioc_s_register
= vidioc_s_register
,
1875 .vidioc_g_chip_ident
= vidioc_g_chip_ident
,
1877 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1878 .vidiocgmbuf
= vidiocgmbuf
,
1882 static const struct video_device em28xx_video_template
= {
1883 .fops
= &em28xx_v4l_fops
,
1884 .release
= video_device_release
,
1885 .ioctl_ops
= &video_ioctl_ops
,
1889 .tvnorms
= V4L2_STD_ALL
,
1890 .current_norm
= V4L2_STD_PAL
,
1893 static const struct file_operations radio_fops
= {
1894 .owner
= THIS_MODULE
,
1895 .open
= em28xx_v4l2_open
,
1896 .release
= em28xx_v4l2_close
,
1897 .ioctl
= video_ioctl2
,
1898 .compat_ioctl
= v4l_compat_ioctl32
,
1899 .llseek
= no_llseek
,
1902 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
1903 .vidioc_querycap
= radio_querycap
,
1904 .vidioc_g_tuner
= radio_g_tuner
,
1905 .vidioc_enum_input
= radio_enum_input
,
1906 .vidioc_g_audio
= radio_g_audio
,
1907 .vidioc_s_tuner
= radio_s_tuner
,
1908 .vidioc_s_audio
= radio_s_audio
,
1909 .vidioc_s_input
= radio_s_input
,
1910 .vidioc_queryctrl
= radio_queryctrl
,
1911 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1912 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1913 .vidioc_g_frequency
= vidioc_g_frequency
,
1914 .vidioc_s_frequency
= vidioc_s_frequency
,
1915 #ifdef CONFIG_VIDEO_ADV_DEBUG
1916 .vidioc_g_register
= vidioc_g_register
,
1917 .vidioc_s_register
= vidioc_s_register
,
1921 static struct video_device em28xx_radio_template
= {
1922 .name
= "em28xx-radio",
1923 .fops
= &radio_fops
,
1924 .ioctl_ops
= &radio_ioctl_ops
,
1928 /******************************** usb interface ******************************/
1932 static struct video_device
*em28xx_vdev_init(struct em28xx
*dev
,
1933 const struct video_device
*template,
1934 const char *type_name
)
1936 struct video_device
*vfd
;
1938 vfd
= video_device_alloc();
1943 vfd
->parent
= &dev
->udev
->dev
;
1944 vfd
->release
= video_device_release
;
1945 vfd
->debug
= video_debug
;
1947 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s",
1948 dev
->name
, type_name
);
1953 int em28xx_register_analog_devices(struct em28xx
*dev
)
1957 printk(KERN_INFO
"%s: v4l2 driver version %d.%d.%d\n",
1959 (EM28XX_VERSION_CODE
>> 16) & 0xff,
1960 (EM28XX_VERSION_CODE
>> 8) & 0xff, EM28XX_VERSION_CODE
& 0xff);
1962 /* Analog specific initialization */
1963 dev
->format
= &format
[0];
1966 /* enable vbi capturing */
1968 /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
1969 /* em28xx_write_reg(dev, EM28XX_R0F_XCLK, 0x80); clk register */
1970 em28xx_write_reg(dev
, EM28XX_R11_VINCTRL
, 0x51);
1972 dev
->mute
= 1; /* maybe not the right place... */
1975 em28xx_set_outfmt(dev
);
1976 em28xx_colorlevels_set_default(dev
);
1977 em28xx_compression_disable(dev
);
1979 /* set default norm */
1980 dev
->norm
= em28xx_video_template
.current_norm
;
1981 dev
->width
= norm_maxw(dev
);
1982 dev
->height
= norm_maxh(dev
);
1983 dev
->interlaced
= EM28XX_INTERLACED_DEFAULT
;
1987 /* FIXME: This is a very bad hack! Not all devices have TV on input 2 */
1990 /* allocate and fill video video_device struct */
1991 dev
->vdev
= em28xx_vdev_init(dev
, &em28xx_video_template
, "video");
1993 em28xx_errdev("cannot allocate video_device.\n");
1997 /* register v4l2 video video_device */
1998 ret
= video_register_device(dev
->vdev
, VFL_TYPE_GRABBER
,
1999 video_nr
[dev
->devno
]);
2001 em28xx_errdev("unable to register video device (error=%i).\n",
2006 /* Allocate and fill vbi video_device struct */
2007 dev
->vbi_dev
= em28xx_vdev_init(dev
, &em28xx_video_template
, "vbi");
2009 /* register v4l2 vbi video_device */
2010 ret
= video_register_device(dev
->vbi_dev
, VFL_TYPE_VBI
,
2011 vbi_nr
[dev
->devno
]);
2013 em28xx_errdev("unable to register vbi device\n");
2017 if (em28xx_boards
[dev
->model
].radio
.type
== EM28XX_RADIO
) {
2018 dev
->radio_dev
= em28xx_vdev_init(dev
, &em28xx_radio_template
, "radio");
2019 if (!dev
->radio_dev
) {
2020 em28xx_errdev("cannot allocate video_device.\n");
2023 ret
= video_register_device(dev
->radio_dev
, VFL_TYPE_RADIO
,
2024 radio_nr
[dev
->devno
]);
2026 em28xx_errdev("can't register radio device\n");
2029 em28xx_info("Registered radio device as /dev/radio%d\n",
2030 dev
->radio_dev
->num
);
2033 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2034 dev
->vdev
->num
, dev
->vbi_dev
->num
);