2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
5 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
7 Mauro Carvalho Chehab <mchehab@infradead.org>
8 Sascha Sommer <saschasommer@freenet.de>
10 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/bitmap.h>
33 #include <linux/usb.h>
34 #include <linux/i2c.h>
35 #include <linux/version.h>
37 #include <linux/mutex.h>
40 #include <media/v4l2-common.h>
41 #include <media/v4l2-ioctl.h>
42 #include <media/v4l2-chip-ident.h>
43 #include <media/msp3400.h>
44 #include <media/tuner.h>
46 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
47 "Markus Rechberger <mrechberger@gmail.com>, " \
48 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
49 "Sascha Sommer <saschasommer@freenet.de>"
51 #define DRIVER_DESC "Empia em28xx based USB video device driver"
52 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
54 #define em28xx_videodbg(fmt, arg...) do {\
56 printk(KERN_INFO "%s %s :"fmt, \
57 dev->name, __func__ , ##arg); } while (0)
59 static unsigned int isoc_debug
;
60 module_param(isoc_debug
, int, 0644);
61 MODULE_PARM_DESC(isoc_debug
, "enable debug messages [isoc transfers]");
63 #define em28xx_isocdbg(fmt, arg...) \
66 printk(KERN_INFO "%s %s :"fmt, \
67 dev->name, __func__ , ##arg); \
71 MODULE_AUTHOR(DRIVER_AUTHOR
);
72 MODULE_DESCRIPTION(DRIVER_DESC
);
73 MODULE_LICENSE("GPL");
75 static unsigned int video_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
76 static unsigned int vbi_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
77 static unsigned int radio_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
79 module_param_array(video_nr
, int, NULL
, 0444);
80 module_param_array(vbi_nr
, int, NULL
, 0444);
81 module_param_array(radio_nr
, int, NULL
, 0444);
82 MODULE_PARM_DESC(video_nr
, "video device numbers");
83 MODULE_PARM_DESC(vbi_nr
, "vbi device numbers");
84 MODULE_PARM_DESC(radio_nr
, "radio device numbers");
86 static unsigned int video_debug
;
87 module_param(video_debug
, int, 0644);
88 MODULE_PARM_DESC(video_debug
, "enable debug messages [video]");
90 /* supported video standards */
91 static struct em28xx_fmt format
[] = {
93 .name
= "16 bpp YUY2, 4:2:2, packed",
94 .fourcc
= V4L2_PIX_FMT_YUYV
,
96 .reg
= EM28XX_OUTFMT_YUV422_Y0UY1V
,
98 .name
= "16 bpp RGB 565, LE",
99 .fourcc
= V4L2_PIX_FMT_RGB565
,
101 .reg
= EM28XX_OUTFMT_RGB_16_656
,
103 .name
= "8 bpp Bayer BGBG..GRGR",
104 .fourcc
= V4L2_PIX_FMT_SBGGR8
,
106 .reg
= EM28XX_OUTFMT_RGB_8_BGBG
,
108 .name
= "8 bpp Bayer GRGR..BGBG",
109 .fourcc
= V4L2_PIX_FMT_SGRBG8
,
111 .reg
= EM28XX_OUTFMT_RGB_8_GRGR
,
113 .name
= "8 bpp Bayer GBGB..RGRG",
114 .fourcc
= V4L2_PIX_FMT_SGBRG8
,
116 .reg
= EM28XX_OUTFMT_RGB_8_GBGB
,
118 .name
= "12 bpp YUV411",
119 .fourcc
= V4L2_PIX_FMT_YUV411P
,
121 .reg
= EM28XX_OUTFMT_YUV411
,
125 /* supported controls */
126 /* Common to all boards */
127 static struct v4l2_queryctrl ac97_qctrl
[] = {
129 .id
= V4L2_CID_AUDIO_VOLUME
,
130 .type
= V4L2_CTRL_TYPE_INTEGER
,
135 .default_value
= 0x1f,
136 .flags
= V4L2_CTRL_FLAG_SLIDER
,
138 .id
= V4L2_CID_AUDIO_MUTE
,
139 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
149 /* ------------------------------------------------------------------
150 DMA and thread functions
151 ------------------------------------------------------------------*/
154 * Announces that a buffer were filled and request the next
156 static inline void buffer_filled(struct em28xx
*dev
,
157 struct em28xx_dmaqueue
*dma_q
,
158 struct em28xx_buffer
*buf
)
160 /* Advice that buffer was filled */
161 em28xx_isocdbg("[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
162 buf
->vb
.state
= VIDEOBUF_DONE
;
163 buf
->vb
.field_count
++;
164 do_gettimeofday(&buf
->vb
.ts
);
166 dev
->isoc_ctl
.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 if (dev
->progressive
)
200 /* Interlaces two half frames */
204 fieldstart
= outp
+ bytesperline
;
207 linesdone
= dma_q
->pos
/ bytesperline
;
208 currlinedone
= dma_q
->pos
% bytesperline
;
210 if (dev
->progressive
)
211 offset
= linesdone
* bytesperline
+ currlinedone
;
213 offset
= linesdone
* bytesperline
* 2 + currlinedone
;
215 startwrite
= fieldstart
+ offset
;
216 lencopy
= bytesperline
- currlinedone
;
217 lencopy
= lencopy
> remain
? remain
: lencopy
;
219 if ((char *)startwrite
+ lencopy
> (char *)outp
+ buf
->vb
.size
) {
220 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
221 ((char *)startwrite
+ lencopy
) -
222 ((char *)outp
+ buf
->vb
.size
));
223 remain
= (char *)outp
+ buf
->vb
.size
- (char *)startwrite
;
228 memcpy(startwrite
, startread
, lencopy
);
233 startwrite
+= lencopy
+ bytesperline
;
234 startread
+= lencopy
;
235 if (bytesperline
> remain
)
238 lencopy
= bytesperline
;
240 if ((char *)startwrite
+ lencopy
> (char *)outp
+
242 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
243 ((char *)startwrite
+ lencopy
) -
244 ((char *)outp
+ buf
->vb
.size
));
245 lencopy
= remain
= (char *)outp
+ buf
->vb
.size
-
251 memcpy(startwrite
, startread
, lencopy
);
259 static inline void print_err_status(struct em28xx
*dev
,
260 int packet
, int status
)
262 char *errmsg
= "Unknown";
266 errmsg
= "unlinked synchronuously";
269 errmsg
= "unlinked asynchronuously";
272 errmsg
= "Buffer error (overrun)";
275 errmsg
= "Stalled (device not responding)";
278 errmsg
= "Babble (bad cable?)";
281 errmsg
= "Bit-stuff error (bad cable?)";
284 errmsg
= "CRC/Timeout (could be anything)";
287 errmsg
= "Device does not respond";
291 em28xx_isocdbg("URB status %d [%s].\n", status
, errmsg
);
293 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
294 packet
, status
, errmsg
);
299 * video-buf generic routine to get the next available buffer
301 static inline void get_next_buf(struct em28xx_dmaqueue
*dma_q
,
302 struct em28xx_buffer
**buf
)
304 struct em28xx
*dev
= container_of(dma_q
, struct em28xx
, vidq
);
307 if (list_empty(&dma_q
->active
)) {
308 em28xx_isocdbg("No active queue to serve\n");
309 dev
->isoc_ctl
.buf
= NULL
;
314 /* Get the next buffer */
315 *buf
= list_entry(dma_q
->active
.next
, struct em28xx_buffer
, vb
.queue
);
317 /* Cleans up buffer - Usefull for testing for frame/URB loss */
318 outp
= videobuf_to_vmalloc(&(*buf
)->vb
);
319 memset(outp
, 0, (*buf
)->vb
.size
);
321 dev
->isoc_ctl
.buf
= *buf
;
327 * Controls the isoc copy of each urb packet
329 static inline int em28xx_isoc_copy(struct em28xx
*dev
, struct urb
*urb
)
331 struct em28xx_buffer
*buf
;
332 struct em28xx_dmaqueue
*dma_q
= urb
->context
;
333 unsigned char *outp
= NULL
;
334 int i
, len
= 0, rc
= 1;
340 if ((dev
->state
& DEV_DISCONNECTED
) || (dev
->state
& DEV_MISCONFIGURED
))
343 if (urb
->status
< 0) {
344 print_err_status(dev
, -1, urb
->status
);
345 if (urb
->status
== -ENOENT
)
349 buf
= dev
->isoc_ctl
.buf
;
351 outp
= videobuf_to_vmalloc(&buf
->vb
);
353 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
354 int status
= urb
->iso_frame_desc
[i
].status
;
357 print_err_status(dev
, i
, status
);
358 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
362 len
= urb
->iso_frame_desc
[i
].actual_length
- 4;
364 if (urb
->iso_frame_desc
[i
].actual_length
<= 0) {
365 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
368 if (urb
->iso_frame_desc
[i
].actual_length
>
370 em28xx_isocdbg("packet bigger than packet size");
374 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
376 /* FIXME: incomplete buffer checks where removed to make
377 logic simpler. Impacts of those changes should be evaluated
379 if (p
[0] == 0x33 && p
[1] == 0x95 && p
[2] == 0x00) {
380 em28xx_isocdbg("VBI HEADER!!!\n");
381 /* FIXME: Should add vbi copy */
384 if (p
[0] == 0x22 && p
[1] == 0x5a) {
385 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p
[2],
386 len
, (p
[2] & 1) ? "odd" : "even");
388 if (dev
->progressive
|| !(p
[2] & 1)) {
390 buffer_filled(dev
, dma_q
, buf
);
391 get_next_buf(dma_q
, &buf
);
395 outp
= videobuf_to_vmalloc(&buf
->vb
);
408 em28xx_copy_video(dev
, dma_q
, buf
, p
, outp
, len
);
413 /* ------------------------------------------------------------------
415 ------------------------------------------------------------------*/
418 buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
, unsigned int *size
)
420 struct em28xx_fh
*fh
= vq
->priv_data
;
421 struct em28xx
*dev
= fh
->dev
;
422 struct v4l2_frequency f
;
424 *size
= (fh
->dev
->width
* fh
->dev
->height
* dev
->format
->depth
+ 7) >> 3;
427 *count
= EM28XX_DEF_BUF
;
429 if (*count
< EM28XX_MIN_BUF
)
430 *count
= EM28XX_MIN_BUF
;
432 /* Ask tuner to go to analog or radio mode */
433 memset(&f
, 0, sizeof(f
));
434 f
.frequency
= dev
->ctl_freq
;
435 f
.type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
437 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, &f
);
442 /* This is called *without* dev->slock held; please keep it that way */
443 static void free_buffer(struct videobuf_queue
*vq
, struct em28xx_buffer
*buf
)
445 struct em28xx_fh
*fh
= vq
->priv_data
;
446 struct em28xx
*dev
= fh
->dev
;
447 unsigned long flags
= 0;
451 /* We used to wait for the buffer to finish here, but this didn't work
452 because, as we were keeping the state as VIDEOBUF_QUEUED,
453 videobuf_queue_cancel marked it as finished for us.
454 (Also, it could wedge forever if the hardware was misconfigured.)
456 This should be safe; by the time we get here, the buffer isn't
457 queued anymore. If we ever start marking the buffers as
458 VIDEOBUF_ACTIVE, it won't be, though.
460 spin_lock_irqsave(&dev
->slock
, flags
);
461 if (dev
->isoc_ctl
.buf
== buf
)
462 dev
->isoc_ctl
.buf
= NULL
;
463 spin_unlock_irqrestore(&dev
->slock
, flags
);
465 videobuf_vmalloc_free(&buf
->vb
);
466 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
470 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
471 enum v4l2_field field
)
473 struct em28xx_fh
*fh
= vq
->priv_data
;
474 struct em28xx_buffer
*buf
= container_of(vb
, struct em28xx_buffer
, vb
);
475 struct em28xx
*dev
= fh
->dev
;
476 int rc
= 0, urb_init
= 0;
478 buf
->vb
.size
= (fh
->dev
->width
* fh
->dev
->height
* dev
->format
->depth
+ 7) >> 3;
480 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
483 buf
->vb
.width
= dev
->width
;
484 buf
->vb
.height
= dev
->height
;
485 buf
->vb
.field
= field
;
487 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
488 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
493 if (!dev
->isoc_ctl
.num_bufs
)
497 rc
= em28xx_init_isoc(dev
, EM28XX_NUM_PACKETS
,
498 EM28XX_NUM_BUFS
, dev
->max_pkt_size
,
504 buf
->vb
.state
= VIDEOBUF_PREPARED
;
508 free_buffer(vq
, buf
);
513 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
515 struct em28xx_buffer
*buf
= container_of(vb
,
516 struct em28xx_buffer
,
518 struct em28xx_fh
*fh
= vq
->priv_data
;
519 struct em28xx
*dev
= fh
->dev
;
520 struct em28xx_dmaqueue
*vidq
= &dev
->vidq
;
522 buf
->vb
.state
= VIDEOBUF_QUEUED
;
523 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
527 static void buffer_release(struct videobuf_queue
*vq
,
528 struct videobuf_buffer
*vb
)
530 struct em28xx_buffer
*buf
= container_of(vb
,
531 struct em28xx_buffer
,
533 struct em28xx_fh
*fh
= vq
->priv_data
;
534 struct em28xx
*dev
= (struct em28xx
*)fh
->dev
;
536 em28xx_isocdbg("em28xx: called buffer_release\n");
538 free_buffer(vq
, buf
);
541 static struct videobuf_queue_ops em28xx_video_qops
= {
542 .buf_setup
= buffer_setup
,
543 .buf_prepare
= buffer_prepare
,
544 .buf_queue
= buffer_queue
,
545 .buf_release
= buffer_release
,
548 /********************* v4l2 interface **************************************/
550 static void video_mux(struct em28xx
*dev
, int index
)
552 dev
->ctl_input
= index
;
553 dev
->ctl_ainput
= INPUT(index
)->amux
;
554 dev
->ctl_aoutput
= INPUT(index
)->aout
;
556 if (!dev
->ctl_aoutput
)
557 dev
->ctl_aoutput
= EM28XX_AOUT_MASTER
;
559 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_routing
,
560 INPUT(index
)->vmux
, 0, 0);
562 if (dev
->board
.has_msp34xx
) {
563 if (dev
->i2s_speed
) {
564 v4l2_device_call_all(&dev
->v4l2_dev
, 0, audio
,
565 s_i2s_clock_freq
, dev
->i2s_speed
);
567 /* Note: this is msp3400 specific */
568 v4l2_device_call_all(&dev
->v4l2_dev
, 0, audio
, s_routing
,
569 dev
->ctl_ainput
, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1
), 0);
572 if (dev
->board
.adecoder
!= EM28XX_NOADECODER
) {
573 v4l2_device_call_all(&dev
->v4l2_dev
, 0, audio
, s_routing
,
574 dev
->ctl_ainput
, dev
->ctl_aoutput
, 0);
577 em28xx_audio_analog_set(dev
);
580 /* Usage lock check functions */
581 static int res_get(struct em28xx_fh
*fh
)
583 struct em28xx
*dev
= fh
->dev
;
586 /* This instance already has stream_on */
598 static int res_check(struct em28xx_fh
*fh
)
600 return fh
->stream_on
;
603 static void res_free(struct em28xx_fh
*fh
)
605 struct em28xx
*dev
= fh
->dev
;
613 * return the ac97 supported controls
615 static int ac97_queryctrl(struct v4l2_queryctrl
*qc
)
619 for (i
= 0; i
< ARRAY_SIZE(ac97_qctrl
); i
++) {
620 if (qc
->id
&& qc
->id
== ac97_qctrl
[i
].id
) {
621 memcpy(qc
, &(ac97_qctrl
[i
]), sizeof(*qc
));
626 /* Control is not ac97 related */
632 * return the current values for ac97 mute and volume
634 static int ac97_get_ctrl(struct em28xx
*dev
, struct v4l2_control
*ctrl
)
637 case V4L2_CID_AUDIO_MUTE
:
638 ctrl
->value
= dev
->mute
;
640 case V4L2_CID_AUDIO_VOLUME
:
641 ctrl
->value
= dev
->volume
;
644 /* Control is not ac97 related */
651 * set values for ac97 mute and volume
653 static int ac97_set_ctrl(struct em28xx
*dev
, const struct v4l2_control
*ctrl
)
657 for (i
= 0; i
< ARRAY_SIZE(ac97_qctrl
); i
++)
658 if (ctrl
->id
== ac97_qctrl
[i
].id
)
661 /* Announce that hasn't handle it */
665 if (ctrl
->value
< ac97_qctrl
[i
].minimum
||
666 ctrl
->value
> ac97_qctrl
[i
].maximum
)
670 case V4L2_CID_AUDIO_MUTE
:
671 dev
->mute
= ctrl
->value
;
673 case V4L2_CID_AUDIO_VOLUME
:
674 dev
->volume
= ctrl
->value
;
678 return em28xx_audio_analog_set(dev
);
681 static int check_dev(struct em28xx
*dev
)
683 if (dev
->state
& DEV_DISCONNECTED
) {
684 em28xx_errdev("v4l2 ioctl: device not present\n");
688 if (dev
->state
& DEV_MISCONFIGURED
) {
689 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
690 "close and open it again\n");
696 static void get_scale(struct em28xx
*dev
,
697 unsigned int width
, unsigned int height
,
698 unsigned int *hscale
, unsigned int *vscale
)
700 unsigned int maxw
= norm_maxw(dev
);
701 unsigned int maxh
= norm_maxh(dev
);
703 *hscale
= (((unsigned long)maxw
) << 12) / width
- 4096L;
704 if (*hscale
>= 0x4000)
707 *vscale
= (((unsigned long)maxh
) << 12) / height
- 4096L;
708 if (*vscale
>= 0x4000)
712 /* ------------------------------------------------------------------
713 IOCTL vidioc handling
714 ------------------------------------------------------------------*/
716 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
717 struct v4l2_format
*f
)
719 struct em28xx_fh
*fh
= priv
;
720 struct em28xx
*dev
= fh
->dev
;
722 mutex_lock(&dev
->lock
);
724 f
->fmt
.pix
.width
= dev
->width
;
725 f
->fmt
.pix
.height
= dev
->height
;
726 f
->fmt
.pix
.pixelformat
= dev
->format
->fourcc
;
727 f
->fmt
.pix
.bytesperline
= (dev
->width
* dev
->format
->depth
+ 7) >> 3;
728 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
* dev
->height
;
729 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
731 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
732 if (dev
->progressive
)
733 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
735 f
->fmt
.pix
.field
= dev
->interlaced
?
736 V4L2_FIELD_INTERLACED
: V4L2_FIELD_TOP
;
738 mutex_unlock(&dev
->lock
);
742 static struct em28xx_fmt
*format_by_fourcc(unsigned int fourcc
)
746 for (i
= 0; i
< ARRAY_SIZE(format
); i
++)
747 if (format
[i
].fourcc
== fourcc
)
753 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
754 struct v4l2_format
*f
)
756 struct em28xx_fh
*fh
= priv
;
757 struct em28xx
*dev
= fh
->dev
;
758 unsigned int width
= f
->fmt
.pix
.width
;
759 unsigned int height
= f
->fmt
.pix
.height
;
760 unsigned int maxw
= norm_maxw(dev
);
761 unsigned int maxh
= norm_maxh(dev
);
762 unsigned int hscale
, vscale
;
763 struct em28xx_fmt
*fmt
;
765 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
767 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
768 f
->fmt
.pix
.pixelformat
);
772 if (dev
->board
.is_em2800
) {
773 /* the em2800 can only scale down to 50% */
774 height
= height
> (3 * maxh
/ 4) ? maxh
: maxh
/ 2;
775 width
= width
> (3 * maxw
/ 4) ? maxw
: maxw
/ 2;
776 /* According to empiatech support the MaxPacketSize is too small
777 * to support framesizes larger than 640x480 @ 30 fps or 640x576
778 * @ 25 fps. As this would cut of a part of the image we prefer
779 * 360x576 or 360x480 for now */
780 if (width
== maxw
&& height
== maxh
)
783 /* width must even because of the YUYV format
784 height must be even because of interlacing */
785 v4l_bound_align_image(&width
, 48, maxw
, 1, &height
, 32, maxh
, 1, 0);
788 get_scale(dev
, width
, height
, &hscale
, &vscale
);
790 width
= (((unsigned long)maxw
) << 12) / (hscale
+ 4096L);
791 height
= (((unsigned long)maxh
) << 12) / (vscale
+ 4096L);
793 f
->fmt
.pix
.width
= width
;
794 f
->fmt
.pix
.height
= height
;
795 f
->fmt
.pix
.pixelformat
= fmt
->fourcc
;
796 f
->fmt
.pix
.bytesperline
= (dev
->width
* fmt
->depth
+ 7) >> 3;
797 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
* height
;
798 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
799 if (dev
->progressive
)
800 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
802 f
->fmt
.pix
.field
= dev
->interlaced
?
803 V4L2_FIELD_INTERLACED
: V4L2_FIELD_TOP
;
808 static int em28xx_set_video_format(struct em28xx
*dev
, unsigned int fourcc
,
809 unsigned width
, unsigned height
)
811 struct em28xx_fmt
*fmt
;
813 fmt
= format_by_fourcc(fourcc
);
819 dev
->height
= height
;
821 /* set new image size */
822 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
824 em28xx_set_alternate(dev
);
825 em28xx_resolution_set(dev
);
830 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
831 struct v4l2_format
*f
)
833 struct em28xx_fh
*fh
= priv
;
834 struct em28xx
*dev
= fh
->dev
;
841 mutex_lock(&dev
->lock
);
843 vidioc_try_fmt_vid_cap(file
, priv
, f
);
845 if (videobuf_queue_is_busy(&fh
->vb_vidq
)) {
846 em28xx_errdev("%s queue busy\n", __func__
);
851 if (dev
->stream_on
&& !fh
->stream_on
) {
852 em28xx_errdev("%s device in use by another fh\n", __func__
);
857 rc
= em28xx_set_video_format(dev
, f
->fmt
.pix
.pixelformat
,
858 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
);
861 mutex_unlock(&dev
->lock
);
865 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
867 struct em28xx_fh
*fh
= priv
;
868 struct em28xx
*dev
= fh
->dev
;
869 struct v4l2_format f
;
876 mutex_lock(&dev
->lock
);
879 /* Adjusts width/height, if needed */
880 f
.fmt
.pix
.width
= dev
->width
;
881 f
.fmt
.pix
.height
= dev
->height
;
882 vidioc_try_fmt_vid_cap(file
, priv
, &f
);
884 /* set new image size */
885 dev
->width
= f
.fmt
.pix
.width
;
886 dev
->height
= f
.fmt
.pix
.height
;
887 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
889 em28xx_resolution_set(dev
);
890 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_std
, dev
->norm
);
892 mutex_unlock(&dev
->lock
);
896 static int vidioc_g_parm(struct file
*file
, void *priv
,
897 struct v4l2_streamparm
*p
)
899 struct em28xx_fh
*fh
= priv
;
900 struct em28xx
*dev
= fh
->dev
;
903 if (p
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
906 if (dev
->board
.is_webcam
)
907 rc
= v4l2_device_call_until_err(&dev
->v4l2_dev
, 0,
910 v4l2_video_std_frame_period(dev
->norm
,
911 &p
->parm
.capture
.timeperframe
);
916 static int vidioc_s_parm(struct file
*file
, void *priv
,
917 struct v4l2_streamparm
*p
)
919 struct em28xx_fh
*fh
= priv
;
920 struct em28xx
*dev
= fh
->dev
;
922 if (!dev
->board
.is_webcam
)
925 if (p
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
928 return v4l2_device_call_until_err(&dev
->v4l2_dev
, 0, video
, s_parm
, p
);
931 static const char *iname
[] = {
932 [EM28XX_VMUX_COMPOSITE1
] = "Composite1",
933 [EM28XX_VMUX_COMPOSITE2
] = "Composite2",
934 [EM28XX_VMUX_COMPOSITE3
] = "Composite3",
935 [EM28XX_VMUX_COMPOSITE4
] = "Composite4",
936 [EM28XX_VMUX_SVIDEO
] = "S-Video",
937 [EM28XX_VMUX_TELEVISION
] = "Television",
938 [EM28XX_VMUX_CABLE
] = "Cable TV",
939 [EM28XX_VMUX_DVB
] = "DVB",
940 [EM28XX_VMUX_DEBUG
] = "for debug only",
943 static int vidioc_enum_input(struct file
*file
, void *priv
,
944 struct v4l2_input
*i
)
946 struct em28xx_fh
*fh
= priv
;
947 struct em28xx
*dev
= fh
->dev
;
951 if (n
>= MAX_EM28XX_INPUT
)
953 if (0 == INPUT(n
)->type
)
957 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
959 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
961 if ((EM28XX_VMUX_TELEVISION
== INPUT(n
)->type
) ||
962 (EM28XX_VMUX_CABLE
== INPUT(n
)->type
))
963 i
->type
= V4L2_INPUT_TYPE_TUNER
;
965 i
->std
= dev
->vdev
->tvnorms
;
970 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
972 struct em28xx_fh
*fh
= priv
;
973 struct em28xx
*dev
= fh
->dev
;
980 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
982 struct em28xx_fh
*fh
= priv
;
983 struct em28xx
*dev
= fh
->dev
;
990 if (i
>= MAX_EM28XX_INPUT
)
992 if (0 == INPUT(i
)->type
)
997 mutex_lock(&dev
->lock
);
998 video_mux(dev
, dev
->ctl_input
);
999 mutex_unlock(&dev
->lock
);
1003 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1005 struct em28xx_fh
*fh
= priv
;
1006 struct em28xx
*dev
= fh
->dev
;
1008 if (!dev
->audio_mode
.has_audio
)
1012 case EM28XX_AMUX_VIDEO
:
1013 strcpy(a
->name
, "Television");
1015 case EM28XX_AMUX_LINE_IN
:
1016 strcpy(a
->name
, "Line In");
1018 case EM28XX_AMUX_VIDEO2
:
1019 strcpy(a
->name
, "Television alt");
1021 case EM28XX_AMUX_PHONE
:
1022 strcpy(a
->name
, "Phone");
1024 case EM28XX_AMUX_MIC
:
1025 strcpy(a
->name
, "Mic");
1027 case EM28XX_AMUX_CD
:
1028 strcpy(a
->name
, "CD");
1030 case EM28XX_AMUX_AUX
:
1031 strcpy(a
->name
, "Aux");
1033 case EM28XX_AMUX_PCM_OUT
:
1034 strcpy(a
->name
, "PCM");
1040 a
->index
= dev
->ctl_ainput
;
1041 a
->capability
= V4L2_AUDCAP_STEREO
;
1046 static int vidioc_s_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1048 struct em28xx_fh
*fh
= priv
;
1049 struct em28xx
*dev
= fh
->dev
;
1052 if (!dev
->audio_mode
.has_audio
)
1055 if (a
->index
>= MAX_EM28XX_INPUT
)
1057 if (0 == INPUT(a
->index
)->type
)
1060 mutex_lock(&dev
->lock
);
1062 dev
->ctl_ainput
= INPUT(a
->index
)->amux
;
1063 dev
->ctl_aoutput
= INPUT(a
->index
)->aout
;
1065 if (!dev
->ctl_aoutput
)
1066 dev
->ctl_aoutput
= EM28XX_AOUT_MASTER
;
1068 mutex_unlock(&dev
->lock
);
1072 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1073 struct v4l2_queryctrl
*qc
)
1075 struct em28xx_fh
*fh
= priv
;
1076 struct em28xx
*dev
= fh
->dev
;
1080 rc
= check_dev(dev
);
1084 memset(qc
, 0, sizeof(*qc
));
1088 /* enumberate AC97 controls */
1089 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
) {
1090 rc
= ac97_queryctrl(qc
);
1095 /* enumberate V4L2 device controls */
1096 mutex_lock(&dev
->lock
);
1097 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, queryctrl
, qc
);
1098 mutex_unlock(&dev
->lock
);
1106 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1107 struct v4l2_control
*ctrl
)
1109 struct em28xx_fh
*fh
= priv
;
1110 struct em28xx
*dev
= fh
->dev
;
1113 rc
= check_dev(dev
);
1118 mutex_lock(&dev
->lock
);
1120 /* Set an AC97 control */
1121 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
)
1122 rc
= ac97_get_ctrl(dev
, ctrl
);
1126 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1128 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_ctrl
, ctrl
);
1132 mutex_unlock(&dev
->lock
);
1136 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1137 struct v4l2_control
*ctrl
)
1139 struct em28xx_fh
*fh
= priv
;
1140 struct em28xx
*dev
= fh
->dev
;
1143 rc
= check_dev(dev
);
1147 mutex_lock(&dev
->lock
);
1149 /* Set an AC97 control */
1150 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
)
1151 rc
= ac97_set_ctrl(dev
, ctrl
);
1155 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
1157 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_ctrl
, ctrl
);
1160 * In the case of non-AC97 volume controls, we still need
1161 * to do some setups at em28xx, in order to mute/unmute
1162 * and to adjust audio volume. However, the value ranges
1163 * should be checked by the corresponding V4L subdriver.
1166 case V4L2_CID_AUDIO_MUTE
:
1167 dev
->mute
= ctrl
->value
;
1168 rc
= em28xx_audio_analog_set(dev
);
1170 case V4L2_CID_AUDIO_VOLUME
:
1171 dev
->volume
= ctrl
->value
;
1172 rc
= em28xx_audio_analog_set(dev
);
1176 mutex_unlock(&dev
->lock
);
1180 static int vidioc_g_tuner(struct file
*file
, void *priv
,
1181 struct v4l2_tuner
*t
)
1183 struct em28xx_fh
*fh
= priv
;
1184 struct em28xx
*dev
= fh
->dev
;
1187 rc
= check_dev(dev
);
1194 strcpy(t
->name
, "Tuner");
1196 mutex_lock(&dev
->lock
);
1197 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1198 mutex_unlock(&dev
->lock
);
1203 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1204 struct v4l2_tuner
*t
)
1206 struct em28xx_fh
*fh
= priv
;
1207 struct em28xx
*dev
= fh
->dev
;
1210 rc
= check_dev(dev
);
1217 mutex_lock(&dev
->lock
);
1218 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
1219 mutex_unlock(&dev
->lock
);
1224 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1225 struct v4l2_frequency
*f
)
1227 struct em28xx_fh
*fh
= priv
;
1228 struct em28xx
*dev
= fh
->dev
;
1230 mutex_lock(&dev
->lock
);
1231 f
->type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1232 f
->frequency
= dev
->ctl_freq
;
1233 mutex_unlock(&dev
->lock
);
1238 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1239 struct v4l2_frequency
*f
)
1241 struct em28xx_fh
*fh
= priv
;
1242 struct em28xx
*dev
= fh
->dev
;
1245 rc
= check_dev(dev
);
1252 if (unlikely(0 == fh
->radio
&& f
->type
!= V4L2_TUNER_ANALOG_TV
))
1254 if (unlikely(1 == fh
->radio
&& f
->type
!= V4L2_TUNER_RADIO
))
1257 mutex_lock(&dev
->lock
);
1259 dev
->ctl_freq
= f
->frequency
;
1260 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, f
);
1262 mutex_unlock(&dev
->lock
);
1267 #ifdef CONFIG_VIDEO_ADV_DEBUG
1268 static int em28xx_reg_len(int reg
)
1271 case EM28XX_R40_AC97LSB
:
1272 case EM28XX_R30_HSCALELOW
:
1273 case EM28XX_R32_VSCALELOW
:
1280 static int vidioc_g_chip_ident(struct file
*file
, void *priv
,
1281 struct v4l2_dbg_chip_ident
*chip
)
1283 struct em28xx_fh
*fh
= priv
;
1284 struct em28xx
*dev
= fh
->dev
;
1286 chip
->ident
= V4L2_IDENT_NONE
;
1289 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_chip_ident
, chip
);
1295 static int vidioc_g_register(struct file
*file
, void *priv
,
1296 struct v4l2_dbg_register
*reg
)
1298 struct em28xx_fh
*fh
= priv
;
1299 struct em28xx
*dev
= fh
->dev
;
1302 switch (reg
->match
.type
) {
1303 case V4L2_CHIP_MATCH_AC97
:
1304 mutex_lock(&dev
->lock
);
1305 ret
= em28xx_read_ac97(dev
, reg
->reg
);
1306 mutex_unlock(&dev
->lock
);
1313 case V4L2_CHIP_MATCH_I2C_DRIVER
:
1314 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_register
, reg
);
1316 case V4L2_CHIP_MATCH_I2C_ADDR
:
1317 /* TODO: is this correct? */
1318 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_register
, reg
);
1321 if (!v4l2_chip_match_host(®
->match
))
1326 reg
->size
= em28xx_reg_len(reg
->reg
);
1327 if (reg
->size
== 1) {
1328 mutex_lock(&dev
->lock
);
1329 ret
= em28xx_read_reg(dev
, reg
->reg
);
1330 mutex_unlock(&dev
->lock
);
1338 mutex_lock(&dev
->lock
);
1339 ret
= em28xx_read_reg_req_len(dev
, USB_REQ_GET_STATUS
,
1340 reg
->reg
, (char *)&val
, 2);
1341 mutex_unlock(&dev
->lock
);
1345 reg
->val
= le16_to_cpu(val
);
1351 static int vidioc_s_register(struct file
*file
, void *priv
,
1352 struct v4l2_dbg_register
*reg
)
1354 struct em28xx_fh
*fh
= priv
;
1355 struct em28xx
*dev
= fh
->dev
;
1359 switch (reg
->match
.type
) {
1360 case V4L2_CHIP_MATCH_AC97
:
1361 mutex_lock(&dev
->lock
);
1362 rc
= em28xx_write_ac97(dev
, reg
->reg
, reg
->val
);
1363 mutex_unlock(&dev
->lock
);
1366 case V4L2_CHIP_MATCH_I2C_DRIVER
:
1367 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_register
, reg
);
1369 case V4L2_CHIP_MATCH_I2C_ADDR
:
1370 /* TODO: is this correct? */
1371 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_register
, reg
);
1374 if (!v4l2_chip_match_host(®
->match
))
1379 buf
= cpu_to_le16(reg
->val
);
1381 mutex_lock(&dev
->lock
);
1382 rc
= em28xx_write_regs(dev
, reg
->reg
, (char *)&buf
,
1383 em28xx_reg_len(reg
->reg
));
1384 mutex_unlock(&dev
->lock
);
1391 static int vidioc_cropcap(struct file
*file
, void *priv
,
1392 struct v4l2_cropcap
*cc
)
1394 struct em28xx_fh
*fh
= priv
;
1395 struct em28xx
*dev
= fh
->dev
;
1397 if (cc
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1400 cc
->bounds
.left
= 0;
1402 cc
->bounds
.width
= dev
->width
;
1403 cc
->bounds
.height
= dev
->height
;
1404 cc
->defrect
= cc
->bounds
;
1405 cc
->pixelaspect
.numerator
= 54; /* 4:3 FIXME: remove magic numbers */
1406 cc
->pixelaspect
.denominator
= 59;
1411 static int vidioc_streamon(struct file
*file
, void *priv
,
1412 enum v4l2_buf_type type
)
1414 struct em28xx_fh
*fh
= priv
;
1415 struct em28xx
*dev
= fh
->dev
;
1418 rc
= check_dev(dev
);
1423 mutex_lock(&dev
->lock
);
1426 if (likely(rc
>= 0))
1427 rc
= videobuf_streamon(&fh
->vb_vidq
);
1429 mutex_unlock(&dev
->lock
);
1434 static int vidioc_streamoff(struct file
*file
, void *priv
,
1435 enum v4l2_buf_type type
)
1437 struct em28xx_fh
*fh
= priv
;
1438 struct em28xx
*dev
= fh
->dev
;
1441 rc
= check_dev(dev
);
1445 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1447 if (type
!= fh
->type
)
1450 mutex_lock(&dev
->lock
);
1452 videobuf_streamoff(&fh
->vb_vidq
);
1455 mutex_unlock(&dev
->lock
);
1460 static int vidioc_querycap(struct file
*file
, void *priv
,
1461 struct v4l2_capability
*cap
)
1463 struct em28xx_fh
*fh
= priv
;
1464 struct em28xx
*dev
= fh
->dev
;
1466 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
1467 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
, sizeof(cap
->card
));
1468 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
1470 cap
->version
= EM28XX_VERSION_CODE
;
1473 V4L2_CAP_SLICED_VBI_CAPTURE
|
1474 V4L2_CAP_VIDEO_CAPTURE
|
1475 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1477 if (dev
->audio_mode
.has_audio
)
1478 cap
->capabilities
|= V4L2_CAP_AUDIO
;
1480 if (dev
->tuner_type
!= TUNER_ABSENT
)
1481 cap
->capabilities
|= V4L2_CAP_TUNER
;
1486 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1487 struct v4l2_fmtdesc
*f
)
1489 if (unlikely(f
->index
>= ARRAY_SIZE(format
)))
1492 strlcpy(f
->description
, format
[f
->index
].name
, sizeof(f
->description
));
1493 f
->pixelformat
= format
[f
->index
].fourcc
;
1498 /* Sliced VBI ioctls */
1499 static int vidioc_g_fmt_sliced_vbi_cap(struct file
*file
, void *priv
,
1500 struct v4l2_format
*f
)
1502 struct em28xx_fh
*fh
= priv
;
1503 struct em28xx
*dev
= fh
->dev
;
1506 rc
= check_dev(dev
);
1510 mutex_lock(&dev
->lock
);
1512 f
->fmt
.sliced
.service_set
= 0;
1513 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, g_fmt
, f
);
1515 if (f
->fmt
.sliced
.service_set
== 0)
1518 mutex_unlock(&dev
->lock
);
1523 static int vidioc_try_set_sliced_vbi_cap(struct file
*file
, void *priv
,
1524 struct v4l2_format
*f
)
1526 struct em28xx_fh
*fh
= priv
;
1527 struct em28xx
*dev
= fh
->dev
;
1530 rc
= check_dev(dev
);
1534 mutex_lock(&dev
->lock
);
1535 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, g_fmt
, f
);
1536 mutex_unlock(&dev
->lock
);
1538 if (f
->fmt
.sliced
.service_set
== 0)
1545 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1546 struct v4l2_requestbuffers
*rb
)
1548 struct em28xx_fh
*fh
= priv
;
1549 struct em28xx
*dev
= fh
->dev
;
1552 rc
= check_dev(dev
);
1556 return videobuf_reqbufs(&fh
->vb_vidq
, rb
);
1559 static int vidioc_querybuf(struct file
*file
, void *priv
,
1560 struct v4l2_buffer
*b
)
1562 struct em28xx_fh
*fh
= priv
;
1563 struct em28xx
*dev
= fh
->dev
;
1566 rc
= check_dev(dev
);
1570 return videobuf_querybuf(&fh
->vb_vidq
, b
);
1573 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1575 struct em28xx_fh
*fh
= priv
;
1576 struct em28xx
*dev
= fh
->dev
;
1579 rc
= check_dev(dev
);
1583 return videobuf_qbuf(&fh
->vb_vidq
, b
);
1586 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1588 struct em28xx_fh
*fh
= priv
;
1589 struct em28xx
*dev
= fh
->dev
;
1592 rc
= check_dev(dev
);
1596 return videobuf_dqbuf(&fh
->vb_vidq
, b
, file
->f_flags
& O_NONBLOCK
);
1599 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1600 static int vidiocgmbuf(struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
1602 struct em28xx_fh
*fh
= priv
;
1604 return videobuf_cgmbuf(&fh
->vb_vidq
, mbuf
, 8);
1609 /* ----------------------------------------------------------- */
1610 /* RADIO ESPECIFIC IOCTLS */
1611 /* ----------------------------------------------------------- */
1613 static int radio_querycap(struct file
*file
, void *priv
,
1614 struct v4l2_capability
*cap
)
1616 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
1618 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
1619 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
, sizeof(cap
->card
));
1620 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
1622 cap
->version
= EM28XX_VERSION_CODE
;
1623 cap
->capabilities
= V4L2_CAP_TUNER
;
1627 static int radio_g_tuner(struct file
*file
, void *priv
,
1628 struct v4l2_tuner
*t
)
1630 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
1632 if (unlikely(t
->index
> 0))
1635 strcpy(t
->name
, "Radio");
1636 t
->type
= V4L2_TUNER_RADIO
;
1638 mutex_lock(&dev
->lock
);
1639 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1640 mutex_unlock(&dev
->lock
);
1645 static int radio_enum_input(struct file
*file
, void *priv
,
1646 struct v4l2_input
*i
)
1650 strcpy(i
->name
, "Radio");
1651 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1656 static int radio_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1658 if (unlikely(a
->index
))
1661 strcpy(a
->name
, "Radio");
1665 static int radio_s_tuner(struct file
*file
, void *priv
,
1666 struct v4l2_tuner
*t
)
1668 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
1673 mutex_lock(&dev
->lock
);
1674 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
1675 mutex_unlock(&dev
->lock
);
1680 static int radio_s_audio(struct file
*file
, void *fh
,
1681 struct v4l2_audio
*a
)
1686 static int radio_s_input(struct file
*file
, void *fh
, unsigned int i
)
1691 static int radio_queryctrl(struct file
*file
, void *priv
,
1692 struct v4l2_queryctrl
*qc
)
1696 if (qc
->id
< V4L2_CID_BASE
||
1697 qc
->id
>= V4L2_CID_LASTP1
)
1700 for (i
= 0; i
< ARRAY_SIZE(ac97_qctrl
); i
++) {
1701 if (qc
->id
&& qc
->id
== ac97_qctrl
[i
].id
) {
1702 memcpy(qc
, &(ac97_qctrl
[i
]), sizeof(*qc
));
1711 * em28xx_v4l2_open()
1712 * inits the device and starts isoc transfer
1714 static int em28xx_v4l2_open(struct file
*filp
)
1716 int minor
= video_devdata(filp
)->minor
;
1717 int errCode
= 0, radio
;
1719 enum v4l2_buf_type fh_type
;
1720 struct em28xx_fh
*fh
;
1721 enum v4l2_field field
;
1723 dev
= em28xx_get_device(minor
, &fh_type
, &radio
);
1728 mutex_lock(&dev
->lock
);
1730 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1731 minor
, v4l2_type_names
[fh_type
], dev
->users
);
1734 fh
= kzalloc(sizeof(struct em28xx_fh
), GFP_KERNEL
);
1736 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1737 mutex_unlock(&dev
->lock
);
1743 filp
->private_data
= fh
;
1745 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&& dev
->users
== 0) {
1746 em28xx_set_mode(dev
, EM28XX_ANALOG_MODE
);
1747 em28xx_set_alternate(dev
);
1748 em28xx_resolution_set(dev
);
1750 /* Needed, since GPIO might have disabled power of
1753 em28xx_wake_i2c(dev
);
1757 em28xx_videodbg("video_open: setting radio device\n");
1758 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_radio
);
1763 if (dev
->progressive
)
1764 field
= V4L2_FIELD_NONE
;
1766 field
= V4L2_FIELD_INTERLACED
;
1768 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &em28xx_video_qops
,
1769 NULL
, &dev
->slock
, fh
->type
, field
,
1770 sizeof(struct em28xx_buffer
), fh
);
1772 mutex_unlock(&dev
->lock
);
1778 * em28xx_realease_resources()
1779 * unregisters the v4l2,i2c and usb devices
1780 * called when the device gets disconected or at module unload
1782 void em28xx_release_analog_resources(struct em28xx
*dev
)
1785 /*FIXME: I2C IR should be disconnected */
1787 if (dev
->radio_dev
) {
1788 if (-1 != dev
->radio_dev
->minor
)
1789 video_unregister_device(dev
->radio_dev
);
1791 video_device_release(dev
->radio_dev
);
1792 dev
->radio_dev
= NULL
;
1795 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1797 if (-1 != dev
->vbi_dev
->minor
)
1798 video_unregister_device(dev
->vbi_dev
);
1800 video_device_release(dev
->vbi_dev
);
1801 dev
->vbi_dev
= NULL
;
1804 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1806 if (-1 != dev
->vdev
->minor
)
1807 video_unregister_device(dev
->vdev
);
1809 video_device_release(dev
->vdev
);
1815 * em28xx_v4l2_close()
1816 * stops streaming and deallocates all resources allocated by the v4l2
1819 static int em28xx_v4l2_close(struct file
*filp
)
1821 struct em28xx_fh
*fh
= filp
->private_data
;
1822 struct em28xx
*dev
= fh
->dev
;
1825 em28xx_videodbg("users=%d\n", dev
->users
);
1828 mutex_lock(&dev
->lock
);
1832 if (dev
->users
== 1) {
1833 videobuf_stop(&fh
->vb_vidq
);
1834 videobuf_mmap_free(&fh
->vb_vidq
);
1836 /* the device is already disconnect,
1837 free the remaining resources */
1838 if (dev
->state
& DEV_DISCONNECTED
) {
1839 em28xx_release_resources(dev
);
1840 mutex_unlock(&dev
->lock
);
1845 /* Save some power by putting tuner to sleep */
1846 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_standby
);
1848 /* do this before setting alternate! */
1849 em28xx_uninit_isoc(dev
);
1850 em28xx_set_mode(dev
, EM28XX_SUSPEND
);
1852 /* set alternate 0 */
1854 em28xx_videodbg("setting alternate 0\n");
1855 errCode
= usb_set_interface(dev
->udev
, 0, 0);
1857 em28xx_errdev("cannot change alternate number to "
1858 "0 (error=%i)\n", errCode
);
1863 wake_up_interruptible_nr(&dev
->open
, 1);
1864 mutex_unlock(&dev
->lock
);
1869 * em28xx_v4l2_read()
1870 * will allocate buffers when called for the first time
1873 em28xx_v4l2_read(struct file
*filp
, char __user
*buf
, size_t count
,
1876 struct em28xx_fh
*fh
= filp
->private_data
;
1877 struct em28xx
*dev
= fh
->dev
;
1880 rc
= check_dev(dev
);
1884 /* FIXME: read() is not prepared to allow changing the video
1885 resolution while streaming. Seems a bug at em28xx_set_fmt
1888 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1889 mutex_lock(&dev
->lock
);
1891 mutex_unlock(&dev
->lock
);
1893 if (unlikely(rc
< 0))
1896 return videobuf_read_stream(&fh
->vb_vidq
, buf
, count
, pos
, 0,
1897 filp
->f_flags
& O_NONBLOCK
);
1903 * em28xx_v4l2_poll()
1904 * will allocate buffers when called for the first time
1906 static unsigned int em28xx_v4l2_poll(struct file
*filp
, poll_table
*wait
)
1908 struct em28xx_fh
*fh
= filp
->private_data
;
1909 struct em28xx
*dev
= fh
->dev
;
1912 rc
= check_dev(dev
);
1916 mutex_lock(&dev
->lock
);
1918 mutex_unlock(&dev
->lock
);
1920 if (unlikely(rc
< 0))
1923 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= fh
->type
)
1926 return videobuf_poll_stream(filp
, &fh
->vb_vidq
, wait
);
1930 * em28xx_v4l2_mmap()
1932 static int em28xx_v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1934 struct em28xx_fh
*fh
= filp
->private_data
;
1935 struct em28xx
*dev
= fh
->dev
;
1938 rc
= check_dev(dev
);
1942 mutex_lock(&dev
->lock
);
1944 mutex_unlock(&dev
->lock
);
1946 if (unlikely(rc
< 0))
1949 rc
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1951 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1952 (unsigned long)vma
->vm_start
,
1953 (unsigned long)vma
->vm_end
-(unsigned long)vma
->vm_start
,
1959 static const struct v4l2_file_operations em28xx_v4l_fops
= {
1960 .owner
= THIS_MODULE
,
1961 .open
= em28xx_v4l2_open
,
1962 .release
= em28xx_v4l2_close
,
1963 .read
= em28xx_v4l2_read
,
1964 .poll
= em28xx_v4l2_poll
,
1965 .mmap
= em28xx_v4l2_mmap
,
1966 .ioctl
= video_ioctl2
,
1969 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1970 .vidioc_querycap
= vidioc_querycap
,
1971 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1972 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1973 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1974 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1975 .vidioc_g_audio
= vidioc_g_audio
,
1976 .vidioc_s_audio
= vidioc_s_audio
,
1977 .vidioc_cropcap
= vidioc_cropcap
,
1979 .vidioc_g_fmt_sliced_vbi_cap
= vidioc_g_fmt_sliced_vbi_cap
,
1980 .vidioc_try_fmt_sliced_vbi_cap
= vidioc_try_set_sliced_vbi_cap
,
1981 .vidioc_s_fmt_sliced_vbi_cap
= vidioc_try_set_sliced_vbi_cap
,
1983 .vidioc_reqbufs
= vidioc_reqbufs
,
1984 .vidioc_querybuf
= vidioc_querybuf
,
1985 .vidioc_qbuf
= vidioc_qbuf
,
1986 .vidioc_dqbuf
= vidioc_dqbuf
,
1987 .vidioc_s_std
= vidioc_s_std
,
1988 .vidioc_g_parm
= vidioc_g_parm
,
1989 .vidioc_s_parm
= vidioc_s_parm
,
1990 .vidioc_enum_input
= vidioc_enum_input
,
1991 .vidioc_g_input
= vidioc_g_input
,
1992 .vidioc_s_input
= vidioc_s_input
,
1993 .vidioc_queryctrl
= vidioc_queryctrl
,
1994 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1995 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1996 .vidioc_streamon
= vidioc_streamon
,
1997 .vidioc_streamoff
= vidioc_streamoff
,
1998 .vidioc_g_tuner
= vidioc_g_tuner
,
1999 .vidioc_s_tuner
= vidioc_s_tuner
,
2000 .vidioc_g_frequency
= vidioc_g_frequency
,
2001 .vidioc_s_frequency
= vidioc_s_frequency
,
2002 #ifdef CONFIG_VIDEO_ADV_DEBUG
2003 .vidioc_g_register
= vidioc_g_register
,
2004 .vidioc_s_register
= vidioc_s_register
,
2005 .vidioc_g_chip_ident
= vidioc_g_chip_ident
,
2007 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2008 .vidiocgmbuf
= vidiocgmbuf
,
2012 static const struct video_device em28xx_video_template
= {
2013 .fops
= &em28xx_v4l_fops
,
2014 .release
= video_device_release
,
2015 .ioctl_ops
= &video_ioctl_ops
,
2019 .tvnorms
= V4L2_STD_ALL
,
2020 .current_norm
= V4L2_STD_PAL
,
2023 static const struct v4l2_file_operations radio_fops
= {
2024 .owner
= THIS_MODULE
,
2025 .open
= em28xx_v4l2_open
,
2026 .release
= em28xx_v4l2_close
,
2027 .ioctl
= video_ioctl2
,
2030 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
2031 .vidioc_querycap
= radio_querycap
,
2032 .vidioc_g_tuner
= radio_g_tuner
,
2033 .vidioc_enum_input
= radio_enum_input
,
2034 .vidioc_g_audio
= radio_g_audio
,
2035 .vidioc_s_tuner
= radio_s_tuner
,
2036 .vidioc_s_audio
= radio_s_audio
,
2037 .vidioc_s_input
= radio_s_input
,
2038 .vidioc_queryctrl
= radio_queryctrl
,
2039 .vidioc_g_ctrl
= vidioc_g_ctrl
,
2040 .vidioc_s_ctrl
= vidioc_s_ctrl
,
2041 .vidioc_g_frequency
= vidioc_g_frequency
,
2042 .vidioc_s_frequency
= vidioc_s_frequency
,
2043 #ifdef CONFIG_VIDEO_ADV_DEBUG
2044 .vidioc_g_register
= vidioc_g_register
,
2045 .vidioc_s_register
= vidioc_s_register
,
2049 static struct video_device em28xx_radio_template
= {
2050 .name
= "em28xx-radio",
2051 .fops
= &radio_fops
,
2052 .ioctl_ops
= &radio_ioctl_ops
,
2056 /******************************** usb interface ******************************/
2060 static struct video_device
*em28xx_vdev_init(struct em28xx
*dev
,
2061 const struct video_device
*template,
2062 const char *type_name
)
2064 struct video_device
*vfd
;
2066 vfd
= video_device_alloc();
2072 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
2073 vfd
->release
= video_device_release
;
2074 vfd
->debug
= video_debug
;
2076 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s",
2077 dev
->name
, type_name
);
2082 int em28xx_register_analog_devices(struct em28xx
*dev
)
2087 printk(KERN_INFO
"%s: v4l2 driver version %d.%d.%d\n",
2089 (EM28XX_VERSION_CODE
>> 16) & 0xff,
2090 (EM28XX_VERSION_CODE
>> 8) & 0xff, EM28XX_VERSION_CODE
& 0xff);
2092 /* set default norm */
2093 dev
->norm
= em28xx_video_template
.current_norm
;
2094 dev
->interlaced
= EM28XX_INTERLACED_DEFAULT
;
2097 /* Analog specific initialization */
2098 dev
->format
= &format
[0];
2099 em28xx_set_video_format(dev
, format
[0].fourcc
,
2100 norm_maxw(dev
), norm_maxh(dev
));
2102 video_mux(dev
, dev
->ctl_input
);
2104 /* Audio defaults */
2108 /* enable vbi capturing */
2110 /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
2111 val
= (u8
)em28xx_read_reg(dev
, EM28XX_R0F_XCLK
);
2112 em28xx_write_reg(dev
, EM28XX_R0F_XCLK
,
2113 (EM28XX_XCLK_AUDIO_UNMUTE
| val
));
2114 em28xx_write_reg(dev
, EM28XX_R11_VINCTRL
, 0x51);
2116 em28xx_set_outfmt(dev
);
2117 em28xx_colorlevels_set_default(dev
);
2118 em28xx_compression_disable(dev
);
2120 /* allocate and fill video video_device struct */
2121 dev
->vdev
= em28xx_vdev_init(dev
, &em28xx_video_template
, "video");
2123 em28xx_errdev("cannot allocate video_device.\n");
2127 /* register v4l2 video video_device */
2128 ret
= video_register_device(dev
->vdev
, VFL_TYPE_GRABBER
,
2129 video_nr
[dev
->devno
]);
2131 em28xx_errdev("unable to register video device (error=%i).\n",
2136 /* Allocate and fill vbi video_device struct */
2137 dev
->vbi_dev
= em28xx_vdev_init(dev
, &em28xx_video_template
, "vbi");
2139 /* register v4l2 vbi video_device */
2140 ret
= video_register_device(dev
->vbi_dev
, VFL_TYPE_VBI
,
2141 vbi_nr
[dev
->devno
]);
2143 em28xx_errdev("unable to register vbi device\n");
2147 if (em28xx_boards
[dev
->model
].radio
.type
== EM28XX_RADIO
) {
2148 dev
->radio_dev
= em28xx_vdev_init(dev
, &em28xx_radio_template
,
2150 if (!dev
->radio_dev
) {
2151 em28xx_errdev("cannot allocate video_device.\n");
2154 ret
= video_register_device(dev
->radio_dev
, VFL_TYPE_RADIO
,
2155 radio_nr
[dev
->devno
]);
2157 em28xx_errdev("can't register radio device\n");
2160 em28xx_info("Registered radio device as /dev/radio%d\n",
2161 dev
->radio_dev
->num
);
2164 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2165 dev
->vdev
->num
, dev
->vbi_dev
->num
);