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/msp3400.h>
43 #include <media/tuner.h>
45 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
46 "Markus Rechberger <mrechberger@gmail.com>, " \
47 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
48 "Sascha Sommer <saschasommer@freenet.de>"
50 #define DRIVER_NAME "em28xx"
51 #define DRIVER_DESC "Empia em28xx based USB video device driver"
52 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
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 LIST_HEAD(em28xx_devlist
);
77 static unsigned int card
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
78 static unsigned int video_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
79 static unsigned int vbi_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
80 static unsigned int radio_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = UNSET
};
82 module_param_array(card
, int, NULL
, 0444);
83 module_param_array(video_nr
, int, NULL
, 0444);
84 module_param_array(vbi_nr
, int, NULL
, 0444);
85 module_param_array(radio_nr
, int, NULL
, 0444);
86 MODULE_PARM_DESC(card
, "card type");
87 MODULE_PARM_DESC(video_nr
, "video device numbers");
88 MODULE_PARM_DESC(vbi_nr
, "vbi device numbers");
89 MODULE_PARM_DESC(radio_nr
, "radio device numbers");
91 static unsigned int video_debug
;
92 module_param(video_debug
, int, 0644);
93 MODULE_PARM_DESC(video_debug
, "enable debug messages [video]");
95 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
96 static unsigned long em28xx_devused
;
98 /* supported controls */
99 /* Common to all boards */
100 static struct v4l2_queryctrl em28xx_qctrl
[] = {
102 .id
= V4L2_CID_AUDIO_VOLUME
,
103 .type
= V4L2_CTRL_TYPE_INTEGER
,
108 .default_value
= 0x1f,
111 .id
= V4L2_CID_AUDIO_MUTE
,
112 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
122 static struct usb_driver em28xx_usb_driver
;
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
= 16 * fh
->dev
->width
* fh
->dev
->height
>> 3;
390 *count
= EM28XX_DEF_BUF
;
392 if (*count
< EM28XX_MIN_BUF
)
393 *count
= EM28XX_MIN_BUF
;
395 /* Ask tuner to go to analog mode */
396 memset(&f
, 0, sizeof(f
));
397 f
.frequency
= dev
->ctl_freq
;
399 em28xx_i2c_call_clients(dev
, VIDIOC_S_FREQUENCY
, &f
);
404 /* This is called *without* dev->slock held; please keep it that way */
405 static void free_buffer(struct videobuf_queue
*vq
, struct em28xx_buffer
*buf
)
407 struct em28xx_fh
*fh
= vq
->priv_data
;
408 struct em28xx
*dev
= fh
->dev
;
409 unsigned long flags
= 0;
413 /* We used to wait for the buffer to finish here, but this didn't work
414 because, as we were keeping the state as VIDEOBUF_QUEUED,
415 videobuf_queue_cancel marked it as finished for us.
416 (Also, it could wedge forever if the hardware was misconfigured.)
418 This should be safe; by the time we get here, the buffer isn't
419 queued anymore. If we ever start marking the buffers as
420 VIDEOBUF_ACTIVE, it won't be, though.
422 spin_lock_irqsave(&dev
->slock
, flags
);
423 if (dev
->isoc_ctl
.buf
== buf
)
424 dev
->isoc_ctl
.buf
= NULL
;
425 spin_unlock_irqrestore(&dev
->slock
, flags
);
427 videobuf_vmalloc_free(&buf
->vb
);
428 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
432 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
433 enum v4l2_field field
)
435 struct em28xx_fh
*fh
= vq
->priv_data
;
436 struct em28xx_buffer
*buf
= container_of(vb
, struct em28xx_buffer
, vb
);
437 struct em28xx
*dev
= fh
->dev
;
438 int rc
= 0, urb_init
= 0;
440 /* FIXME: It assumes depth = 16 */
441 /* The only currently supported format is 16 bits/pixel */
442 buf
->vb
.size
= 16 * dev
->width
* dev
->height
>> 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 **************************************/
512 * inits registers with sane defaults
514 static int em28xx_config(struct em28xx
*dev
)
517 /* Sets I2C speed to 100 KHz */
519 em28xx_write_regs_req(dev
, 0x00, 0x06, "\x40", 1);
521 /* enable vbi capturing */
523 /* em28xx_write_regs_req(dev, 0x00, 0x0e, "\xC0", 1); audio register */
524 /* em28xx_write_regs_req(dev, 0x00, 0x0f, "\x80", 1); clk register */
525 em28xx_write_regs_req(dev
, 0x00, 0x11, "\x51", 1);
527 dev
->mute
= 1; /* maybe not the right place... */
530 em28xx_outfmt_set_yuv422(dev
);
531 em28xx_colorlevels_set_default(dev
);
532 em28xx_compression_disable(dev
);
538 * em28xx_config_i2c()
539 * configure i2c attached devices
541 static void em28xx_config_i2c(struct em28xx
*dev
)
543 struct v4l2_routing route
;
545 route
.input
= INPUT(dev
->ctl_input
)->vmux
;
547 em28xx_i2c_call_clients(dev
, VIDIOC_INT_RESET
, NULL
);
548 em28xx_i2c_call_clients(dev
, VIDIOC_INT_S_VIDEO_ROUTING
, &route
);
549 em28xx_i2c_call_clients(dev
, VIDIOC_STREAMON
, NULL
);
552 static void video_mux(struct em28xx
*dev
, int index
)
554 struct v4l2_routing route
;
556 route
.input
= INPUT(index
)->vmux
;
558 dev
->ctl_input
= index
;
559 dev
->ctl_ainput
= INPUT(index
)->amux
;
561 em28xx_i2c_call_clients(dev
, VIDIOC_INT_S_VIDEO_ROUTING
, &route
);
563 if (dev
->has_msp34xx
) {
564 if (dev
->i2s_speed
) {
565 em28xx_i2c_call_clients(dev
, VIDIOC_INT_I2S_CLOCK_FREQ
,
568 route
.input
= dev
->ctl_ainput
;
569 route
.output
= MSP_OUTPUT(MSP_SC_IN_DSP_SCART1
);
570 /* Note: this is msp3400 specific */
571 em28xx_i2c_call_clients(dev
, VIDIOC_INT_S_AUDIO_ROUTING
,
575 em28xx_audio_analog_set(dev
);
578 /* Usage lock check functions */
579 static int res_get(struct em28xx_fh
*fh
)
581 struct em28xx
*dev
= fh
->dev
;
584 /* This instance already has stream_on */
591 mutex_lock(&dev
->lock
);
594 mutex_unlock(&dev
->lock
);
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
;
607 mutex_lock(&dev
->lock
);
610 mutex_unlock(&dev
->lock
);
615 * return the current saturation, brightness or contrast, mute state
617 static int em28xx_get_ctrl(struct em28xx
*dev
, struct v4l2_control
*ctrl
)
620 case V4L2_CID_AUDIO_MUTE
:
621 ctrl
->value
= dev
->mute
;
623 case V4L2_CID_AUDIO_VOLUME
:
624 ctrl
->value
= dev
->volume
;
633 * mute or set new saturation, brightness or contrast
635 static int em28xx_set_ctrl(struct em28xx
*dev
, const struct v4l2_control
*ctrl
)
638 case V4L2_CID_AUDIO_MUTE
:
639 if (ctrl
->value
!= dev
->mute
) {
640 dev
->mute
= ctrl
->value
;
641 return em28xx_audio_analog_set(dev
);
644 case V4L2_CID_AUDIO_VOLUME
:
645 dev
->volume
= ctrl
->value
;
646 return em28xx_audio_analog_set(dev
);
652 static int check_dev(struct em28xx
*dev
)
654 if (dev
->state
& DEV_DISCONNECTED
) {
655 em28xx_errdev("v4l2 ioctl: device not present\n");
659 if (dev
->state
& DEV_MISCONFIGURED
) {
660 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
661 "close and open it again\n");
667 static void get_scale(struct em28xx
*dev
,
668 unsigned int width
, unsigned int height
,
669 unsigned int *hscale
, unsigned int *vscale
)
671 unsigned int maxw
= norm_maxw(dev
);
672 unsigned int maxh
= norm_maxh(dev
);
674 *hscale
= (((unsigned long)maxw
) << 12) / width
- 4096L;
675 if (*hscale
>= 0x4000)
678 *vscale
= (((unsigned long)maxh
) << 12) / height
- 4096L;
679 if (*vscale
>= 0x4000)
683 /* ------------------------------------------------------------------
684 IOCTL vidioc handling
685 ------------------------------------------------------------------*/
687 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
688 struct v4l2_format
*f
)
690 struct em28xx_fh
*fh
= priv
;
691 struct em28xx
*dev
= fh
->dev
;
693 mutex_lock(&dev
->lock
);
695 f
->fmt
.pix
.width
= dev
->width
;
696 f
->fmt
.pix
.height
= dev
->height
;
697 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUYV
;
698 f
->fmt
.pix
.bytesperline
= dev
->width
* 2;
699 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
* dev
->height
;
700 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
702 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
703 f
->fmt
.pix
.field
= dev
->interlaced
?
704 V4L2_FIELD_INTERLACED
: V4L2_FIELD_TOP
;
706 mutex_unlock(&dev
->lock
);
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 int width
= f
->fmt
.pix
.width
;
716 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
;
721 /* width must even because of the YUYV format
722 height must be even because of interlacing */
735 mutex_lock(&dev
->lock
);
737 if (dev
->is_em2800
) {
738 /* the em2800 can only scale down to 50% */
739 if (height
% (maxh
/ 2))
741 if (width
% (maxw
/ 2))
743 /* according to empiatech support */
744 /* the MaxPacketSize is to small to support */
745 /* framesizes larger than 640x480 @ 30 fps */
746 /* or 640x576 @ 25 fps. As this would cut */
747 /* of a part of the image we prefer */
748 /* 360x576 or 360x480 for now */
749 if (width
== maxw
&& height
== maxh
)
753 get_scale(dev
, width
, height
, &hscale
, &vscale
);
755 width
= (((unsigned long)maxw
) << 12) / (hscale
+ 4096L);
756 height
= (((unsigned long)maxh
) << 12) / (vscale
+ 4096L);
758 f
->fmt
.pix
.width
= width
;
759 f
->fmt
.pix
.height
= height
;
760 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUYV
;
761 f
->fmt
.pix
.bytesperline
= width
* 2;
762 f
->fmt
.pix
.sizeimage
= width
* 2 * height
;
763 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
764 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
766 mutex_unlock(&dev
->lock
);
770 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
771 struct v4l2_format
*f
)
773 struct em28xx_fh
*fh
= priv
;
774 struct em28xx
*dev
= fh
->dev
;
781 vidioc_try_fmt_vid_cap(file
, priv
, f
);
783 mutex_lock(&dev
->lock
);
785 if (videobuf_queue_is_busy(&fh
->vb_vidq
)) {
786 em28xx_errdev("%s queue busy\n", __func__
);
791 if (dev
->stream_on
&& !fh
->stream_on
) {
792 em28xx_errdev("%s device in use by another fh\n", __func__
);
797 /* set new image size */
798 dev
->width
= f
->fmt
.pix
.width
;
799 dev
->height
= f
->fmt
.pix
.height
;
800 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
802 em28xx_set_alternate(dev
);
803 em28xx_resolution_set(dev
);
808 mutex_unlock(&dev
->lock
);
812 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
* norm
)
814 struct em28xx_fh
*fh
= priv
;
815 struct em28xx
*dev
= fh
->dev
;
816 struct v4l2_format f
;
823 mutex_lock(&dev
->lock
);
825 mutex_unlock(&dev
->lock
);
827 /* Adjusts width/height, if needed */
828 f
.fmt
.pix
.width
= dev
->width
;
829 f
.fmt
.pix
.height
= dev
->height
;
830 vidioc_try_fmt_vid_cap(file
, priv
, &f
);
832 mutex_lock(&dev
->lock
);
834 /* set new image size */
835 dev
->width
= f
.fmt
.pix
.width
;
836 dev
->height
= f
.fmt
.pix
.height
;
837 get_scale(dev
, dev
->width
, dev
->height
, &dev
->hscale
, &dev
->vscale
);
839 em28xx_resolution_set(dev
);
840 em28xx_i2c_call_clients(dev
, VIDIOC_S_STD
, &dev
->norm
);
842 mutex_unlock(&dev
->lock
);
846 static const char *iname
[] = {
847 [EM28XX_VMUX_COMPOSITE1
] = "Composite1",
848 [EM28XX_VMUX_COMPOSITE2
] = "Composite2",
849 [EM28XX_VMUX_COMPOSITE3
] = "Composite3",
850 [EM28XX_VMUX_COMPOSITE4
] = "Composite4",
851 [EM28XX_VMUX_SVIDEO
] = "S-Video",
852 [EM28XX_VMUX_TELEVISION
] = "Television",
853 [EM28XX_VMUX_CABLE
] = "Cable TV",
854 [EM28XX_VMUX_DVB
] = "DVB",
855 [EM28XX_VMUX_DEBUG
] = "for debug only",
858 static int vidioc_enum_input(struct file
*file
, void *priv
,
859 struct v4l2_input
*i
)
861 struct em28xx_fh
*fh
= priv
;
862 struct em28xx
*dev
= fh
->dev
;
866 if (n
>= MAX_EM28XX_INPUT
)
868 if (0 == INPUT(n
)->type
)
872 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
874 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
876 if ((EM28XX_VMUX_TELEVISION
== INPUT(n
)->type
) ||
877 (EM28XX_VMUX_CABLE
== INPUT(n
)->type
))
878 i
->type
= V4L2_INPUT_TYPE_TUNER
;
880 i
->std
= dev
->vdev
->tvnorms
;
885 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
887 struct em28xx_fh
*fh
= priv
;
888 struct em28xx
*dev
= fh
->dev
;
895 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
897 struct em28xx_fh
*fh
= priv
;
898 struct em28xx
*dev
= fh
->dev
;
905 if (i
>= MAX_EM28XX_INPUT
)
907 if (0 == INPUT(i
)->type
)
910 mutex_lock(&dev
->lock
);
914 mutex_unlock(&dev
->lock
);
918 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
920 struct em28xx_fh
*fh
= priv
;
921 struct em28xx
*dev
= fh
->dev
;
922 unsigned int index
= a
->index
;
927 index
= dev
->ctl_ainput
;
930 strcpy(a
->name
, "Television");
932 strcpy(a
->name
, "Line In");
934 a
->capability
= V4L2_AUDCAP_STEREO
;
940 static int vidioc_s_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
942 struct em28xx_fh
*fh
= priv
;
943 struct em28xx
*dev
= fh
->dev
;
945 if (a
->index
!= dev
->ctl_ainput
)
951 static int vidioc_queryctrl(struct file
*file
, void *priv
,
952 struct v4l2_queryctrl
*qc
)
954 struct em28xx_fh
*fh
= priv
;
955 struct em28xx
*dev
= fh
->dev
;
964 memset(qc
, 0, sizeof(*qc
));
968 if (!dev
->has_msp34xx
) {
969 for (i
= 0; i
< ARRAY_SIZE(em28xx_qctrl
); i
++) {
970 if (qc
->id
&& qc
->id
== em28xx_qctrl
[i
].id
) {
971 memcpy(qc
, &(em28xx_qctrl
[i
]), sizeof(*qc
));
976 mutex_lock(&dev
->lock
);
977 em28xx_i2c_call_clients(dev
, VIDIOC_QUERYCTRL
, qc
);
978 mutex_unlock(&dev
->lock
);
986 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
987 struct v4l2_control
*ctrl
)
989 struct em28xx_fh
*fh
= priv
;
990 struct em28xx
*dev
= fh
->dev
;
996 mutex_lock(&dev
->lock
);
998 if (!dev
->has_msp34xx
)
999 rc
= em28xx_get_ctrl(dev
, ctrl
);
1003 if (rc
== -EINVAL
) {
1004 em28xx_i2c_call_clients(dev
, VIDIOC_G_CTRL
, 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
->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 f
->type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1107 f
->frequency
= dev
->ctl_freq
;
1112 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1113 struct v4l2_frequency
*f
)
1115 struct em28xx_fh
*fh
= priv
;
1116 struct em28xx
*dev
= fh
->dev
;
1119 rc
= check_dev(dev
);
1126 if (unlikely(0 == fh
->radio
&& f
->type
!= V4L2_TUNER_ANALOG_TV
))
1128 if (unlikely(1 == fh
->radio
&& f
->type
!= V4L2_TUNER_RADIO
))
1131 mutex_lock(&dev
->lock
);
1133 dev
->ctl_freq
= f
->frequency
;
1134 em28xx_i2c_call_clients(dev
, VIDIOC_S_FREQUENCY
, f
);
1136 mutex_unlock(&dev
->lock
);
1140 #ifdef CONFIG_VIDEO_ADV_DEBUG
1141 static int em28xx_reg_len(int reg
)
1144 case EM28XX_R40_AC97LSB
:
1145 case EM28XX_R30_HSCALELOW
:
1146 case EM28XX_R32_VSCALELOW
:
1153 static int vidioc_g_register(struct file
*file
, void *priv
,
1154 struct v4l2_register
*reg
)
1156 struct em28xx_fh
*fh
= priv
;
1157 struct em28xx
*dev
= fh
->dev
;
1160 if (!v4l2_chip_match_host(reg
->match_type
, reg
->match_chip
))
1163 if (em28xx_reg_len(reg
->reg
) == 1) {
1164 ret
= em28xx_read_reg(dev
, reg
->reg
);
1171 ret
= em28xx_read_reg_req_len(dev
, USB_REQ_GET_STATUS
,
1172 reg
->reg
, (char *)&val
, 2);
1176 reg
->val
= le64_to_cpu(val
);
1182 static int vidioc_s_register(struct file
*file
, void *priv
,
1183 struct v4l2_register
*reg
)
1185 struct em28xx_fh
*fh
= priv
;
1186 struct em28xx
*dev
= fh
->dev
;
1189 buf
= cpu_to_le64(reg
->val
);
1191 return em28xx_write_regs(dev
, reg
->reg
, (char *)&buf
,
1192 em28xx_reg_len(reg
->reg
));
1197 static int vidioc_cropcap(struct file
*file
, void *priv
,
1198 struct v4l2_cropcap
*cc
)
1200 struct em28xx_fh
*fh
= priv
;
1201 struct em28xx
*dev
= fh
->dev
;
1203 if (cc
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1206 cc
->bounds
.left
= 0;
1208 cc
->bounds
.width
= dev
->width
;
1209 cc
->bounds
.height
= dev
->height
;
1210 cc
->defrect
= cc
->bounds
;
1211 cc
->pixelaspect
.numerator
= 54; /* 4:3 FIXME: remove magic numbers */
1212 cc
->pixelaspect
.denominator
= 59;
1217 static int vidioc_streamon(struct file
*file
, void *priv
,
1218 enum v4l2_buf_type type
)
1220 struct em28xx_fh
*fh
= priv
;
1221 struct em28xx
*dev
= fh
->dev
;
1224 rc
= check_dev(dev
);
1229 if (unlikely(res_get(fh
) < 0))
1232 return (videobuf_streamon(&fh
->vb_vidq
));
1235 static int vidioc_streamoff(struct file
*file
, void *priv
,
1236 enum v4l2_buf_type type
)
1238 struct em28xx_fh
*fh
= priv
;
1239 struct em28xx
*dev
= fh
->dev
;
1242 rc
= check_dev(dev
);
1246 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1248 if (type
!= fh
->type
)
1251 videobuf_streamoff(&fh
->vb_vidq
);
1257 static int vidioc_querycap(struct file
*file
, void *priv
,
1258 struct v4l2_capability
*cap
)
1260 struct em28xx_fh
*fh
= priv
;
1261 struct em28xx
*dev
= fh
->dev
;
1263 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
1264 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
, sizeof(cap
->card
));
1265 strlcpy(cap
->bus_info
, dev
->udev
->dev
.bus_id
, sizeof(cap
->bus_info
));
1267 cap
->version
= EM28XX_VERSION_CODE
;
1270 V4L2_CAP_SLICED_VBI_CAPTURE
|
1271 V4L2_CAP_VIDEO_CAPTURE
|
1273 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1275 if (dev
->tuner_type
!= TUNER_ABSENT
)
1276 cap
->capabilities
|= V4L2_CAP_TUNER
;
1281 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1282 struct v4l2_fmtdesc
*fmtd
)
1284 if (fmtd
->index
!= 0)
1287 fmtd
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1288 strcpy(fmtd
->description
, "Packed YUY2");
1289 fmtd
->pixelformat
= V4L2_PIX_FMT_YUYV
;
1290 memset(fmtd
->reserved
, 0, sizeof(fmtd
->reserved
));
1295 /* Sliced VBI ioctls */
1296 static int vidioc_g_fmt_sliced_vbi_cap(struct file
*file
, void *priv
,
1297 struct v4l2_format
*f
)
1299 struct em28xx_fh
*fh
= priv
;
1300 struct em28xx
*dev
= fh
->dev
;
1303 rc
= check_dev(dev
);
1307 mutex_lock(&dev
->lock
);
1309 f
->fmt
.sliced
.service_set
= 0;
1311 em28xx_i2c_call_clients(dev
, VIDIOC_G_FMT
, f
);
1313 if (f
->fmt
.sliced
.service_set
== 0)
1316 mutex_unlock(&dev
->lock
);
1320 static int vidioc_try_set_sliced_vbi_cap(struct file
*file
, void *priv
,
1321 struct v4l2_format
*f
)
1323 struct em28xx_fh
*fh
= priv
;
1324 struct em28xx
*dev
= fh
->dev
;
1327 rc
= check_dev(dev
);
1331 mutex_lock(&dev
->lock
);
1332 em28xx_i2c_call_clients(dev
, VIDIOC_G_FMT
, f
);
1333 mutex_unlock(&dev
->lock
);
1335 if (f
->fmt
.sliced
.service_set
== 0)
1342 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1343 struct v4l2_requestbuffers
*rb
)
1345 struct em28xx_fh
*fh
= priv
;
1346 struct em28xx
*dev
= fh
->dev
;
1349 rc
= check_dev(dev
);
1353 return (videobuf_reqbufs(&fh
->vb_vidq
, rb
));
1356 static int vidioc_querybuf(struct file
*file
, void *priv
,
1357 struct v4l2_buffer
*b
)
1359 struct em28xx_fh
*fh
= priv
;
1360 struct em28xx
*dev
= fh
->dev
;
1363 rc
= check_dev(dev
);
1367 return (videobuf_querybuf(&fh
->vb_vidq
, b
));
1370 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1372 struct em28xx_fh
*fh
= priv
;
1373 struct em28xx
*dev
= fh
->dev
;
1376 rc
= check_dev(dev
);
1380 return (videobuf_qbuf(&fh
->vb_vidq
, b
));
1383 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1385 struct em28xx_fh
*fh
= priv
;
1386 struct em28xx
*dev
= fh
->dev
;
1389 rc
= check_dev(dev
);
1393 return (videobuf_dqbuf(&fh
->vb_vidq
, b
,
1394 file
->f_flags
& O_NONBLOCK
));
1397 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1398 static int vidiocgmbuf(struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
1400 struct em28xx_fh
*fh
= priv
;
1402 return videobuf_cgmbuf(&fh
->vb_vidq
, mbuf
, 8);
1407 /* ----------------------------------------------------------- */
1408 /* RADIO ESPECIFIC IOCTLS */
1409 /* ----------------------------------------------------------- */
1411 static int radio_querycap(struct file
*file
, void *priv
,
1412 struct v4l2_capability
*cap
)
1414 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
1416 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
1417 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
, sizeof(cap
->card
));
1418 strlcpy(cap
->bus_info
, dev
->udev
->dev
.bus_id
, sizeof(cap
->bus_info
));
1420 cap
->version
= EM28XX_VERSION_CODE
;
1421 cap
->capabilities
= V4L2_CAP_TUNER
;
1425 static int radio_g_tuner(struct file
*file
, void *priv
,
1426 struct v4l2_tuner
*t
)
1428 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
1430 if (unlikely(t
->index
> 0))
1433 strcpy(t
->name
, "Radio");
1434 t
->type
= V4L2_TUNER_RADIO
;
1436 em28xx_i2c_call_clients(dev
, VIDIOC_G_TUNER
, t
);
1440 static int radio_enum_input(struct file
*file
, void *priv
,
1441 struct v4l2_input
*i
)
1445 strcpy(i
->name
, "Radio");
1446 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1451 static int radio_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1453 if (unlikely(a
->index
))
1456 strcpy(a
->name
, "Radio");
1460 static int radio_s_tuner(struct file
*file
, void *priv
,
1461 struct v4l2_tuner
*t
)
1463 struct em28xx
*dev
= ((struct em28xx_fh
*)priv
)->dev
;
1468 em28xx_i2c_call_clients(dev
, VIDIOC_S_TUNER
, t
);
1473 static int radio_s_audio(struct file
*file
, void *fh
,
1474 struct v4l2_audio
*a
)
1479 static int radio_s_input(struct file
*file
, void *fh
, unsigned int i
)
1484 static int radio_queryctrl(struct file
*file
, void *priv
,
1485 struct v4l2_queryctrl
*qc
)
1489 if (qc
->id
< V4L2_CID_BASE
||
1490 qc
->id
>= V4L2_CID_LASTP1
)
1493 for (i
= 0; i
< ARRAY_SIZE(em28xx_qctrl
); i
++) {
1494 if (qc
->id
&& qc
->id
== em28xx_qctrl
[i
].id
) {
1495 memcpy(qc
, &(em28xx_qctrl
[i
]), sizeof(*qc
));
1504 * em28xx_v4l2_open()
1505 * inits the device and starts isoc transfer
1507 static int em28xx_v4l2_open(struct inode
*inode
, struct file
*filp
)
1509 int minor
= iminor(inode
);
1510 int errCode
= 0, radio
= 0;
1511 struct em28xx
*h
, *dev
= NULL
;
1512 struct em28xx_fh
*fh
;
1513 enum v4l2_buf_type fh_type
= 0;
1515 list_for_each_entry(h
, &em28xx_devlist
, devlist
) {
1516 if (h
->vdev
->minor
== minor
) {
1518 fh_type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1520 if (h
->vbi_dev
->minor
== minor
) {
1522 fh_type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
1525 h
->radio_dev
->minor
== minor
) {
1533 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1534 minor
, v4l2_type_names
[fh_type
], dev
->users
);
1537 fh
= kzalloc(sizeof(struct em28xx_fh
), GFP_KERNEL
);
1539 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1542 mutex_lock(&dev
->lock
);
1546 filp
->private_data
= fh
;
1548 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&& dev
->users
== 0) {
1549 dev
->width
= norm_maxw(dev
);
1550 dev
->height
= norm_maxh(dev
);
1554 em28xx_set_mode(dev
, EM28XX_ANALOG_MODE
);
1555 em28xx_set_alternate(dev
);
1556 em28xx_resolution_set(dev
);
1558 /* Needed, since GPIO might have disabled power of
1561 em28xx_config_i2c(dev
);
1565 em28xx_videodbg("video_open: setting radio device\n");
1566 em28xx_i2c_call_clients(dev
, AUDC_SET_RADIO
, NULL
);
1571 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &em28xx_video_qops
,
1572 NULL
, &dev
->slock
, fh
->type
, V4L2_FIELD_INTERLACED
,
1573 sizeof(struct em28xx_buffer
), fh
);
1575 mutex_unlock(&dev
->lock
);
1581 * em28xx_realease_resources()
1582 * unregisters the v4l2,i2c and usb devices
1583 * called when the device gets disconected or at module unload
1585 static void em28xx_release_resources(struct em28xx
*dev
)
1588 /*FIXME: I2C IR should be disconnected */
1590 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1591 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
,
1592 dev
->vbi_dev
->minor
-MINOR_VFL_TYPE_VBI_MIN
);
1593 list_del(&dev
->devlist
);
1594 if (dev
->sbutton_input_dev
)
1595 em28xx_deregister_snapshot_button(dev
);
1596 if (dev
->radio_dev
) {
1597 if (-1 != dev
->radio_dev
->minor
)
1598 video_unregister_device(dev
->radio_dev
);
1600 video_device_release(dev
->radio_dev
);
1601 dev
->radio_dev
= NULL
;
1604 if (-1 != dev
->vbi_dev
->minor
)
1605 video_unregister_device(dev
->vbi_dev
);
1607 video_device_release(dev
->vbi_dev
);
1608 dev
->vbi_dev
= NULL
;
1611 if (-1 != dev
->vdev
->minor
)
1612 video_unregister_device(dev
->vdev
);
1614 video_device_release(dev
->vdev
);
1617 em28xx_i2c_unregister(dev
);
1618 usb_put_dev(dev
->udev
);
1620 /* Mark device as unused */
1621 em28xx_devused
&= ~(1<<dev
->devno
);
1625 * em28xx_v4l2_close()
1626 * stops streaming and deallocates all resources allocated by the v4l2
1629 static int em28xx_v4l2_close(struct inode
*inode
, struct file
*filp
)
1631 struct em28xx_fh
*fh
= filp
->private_data
;
1632 struct em28xx
*dev
= fh
->dev
;
1635 em28xx_videodbg("users=%d\n", dev
->users
);
1641 mutex_lock(&dev
->lock
);
1643 if (dev
->users
== 1) {
1644 videobuf_stop(&fh
->vb_vidq
);
1645 videobuf_mmap_free(&fh
->vb_vidq
);
1647 /* the device is already disconnect,
1648 free the remaining resources */
1649 if (dev
->state
& DEV_DISCONNECTED
) {
1650 em28xx_release_resources(dev
);
1651 mutex_unlock(&dev
->lock
);
1656 /* do this before setting alternate! */
1657 em28xx_uninit_isoc(dev
);
1658 em28xx_set_mode(dev
, EM28XX_MODE_UNDEFINED
);
1660 /* set alternate 0 */
1662 em28xx_videodbg("setting alternate 0\n");
1663 errCode
= usb_set_interface(dev
->udev
, 0, 0);
1665 em28xx_errdev("cannot change alternate number to "
1666 "0 (error=%i)\n", errCode
);
1671 wake_up_interruptible_nr(&dev
->open
, 1);
1672 mutex_unlock(&dev
->lock
);
1677 * em28xx_v4l2_read()
1678 * will allocate buffers when called for the first time
1681 em28xx_v4l2_read(struct file
*filp
, char __user
*buf
, size_t count
,
1684 struct em28xx_fh
*fh
= filp
->private_data
;
1685 struct em28xx
*dev
= fh
->dev
;
1688 rc
= check_dev(dev
);
1692 /* FIXME: read() is not prepared to allow changing the video
1693 resolution while streaming. Seems a bug at em28xx_set_fmt
1696 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1697 if (unlikely(res_get(fh
)))
1700 return videobuf_read_stream(&fh
->vb_vidq
, buf
, count
, pos
, 0,
1701 filp
->f_flags
& O_NONBLOCK
);
1707 * em28xx_v4l2_poll()
1708 * will allocate buffers when called for the first time
1710 static unsigned int em28xx_v4l2_poll(struct file
*filp
, poll_table
* wait
)
1712 struct em28xx_fh
*fh
= filp
->private_data
;
1713 struct em28xx
*dev
= fh
->dev
;
1716 rc
= check_dev(dev
);
1720 if (unlikely(res_get(fh
) < 0))
1723 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= fh
->type
)
1726 return videobuf_poll_stream(filp
, &fh
->vb_vidq
, wait
);
1730 * em28xx_v4l2_mmap()
1732 static int em28xx_v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1734 struct em28xx_fh
*fh
= filp
->private_data
;
1735 struct em28xx
*dev
= fh
->dev
;
1738 if (unlikely(res_get(fh
) < 0))
1741 rc
= check_dev(dev
);
1745 rc
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1747 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1748 (unsigned long)vma
->vm_start
,
1749 (unsigned long)vma
->vm_end
-(unsigned long)vma
->vm_start
,
1755 static const struct file_operations em28xx_v4l_fops
= {
1756 .owner
= THIS_MODULE
,
1757 .open
= em28xx_v4l2_open
,
1758 .release
= em28xx_v4l2_close
,
1759 .read
= em28xx_v4l2_read
,
1760 .poll
= em28xx_v4l2_poll
,
1761 .mmap
= em28xx_v4l2_mmap
,
1762 .ioctl
= video_ioctl2
,
1763 .llseek
= no_llseek
,
1764 .compat_ioctl
= v4l_compat_ioctl32
,
1767 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1768 .vidioc_querycap
= vidioc_querycap
,
1769 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1770 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1771 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1772 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1773 .vidioc_g_audio
= vidioc_g_audio
,
1774 .vidioc_s_audio
= vidioc_s_audio
,
1775 .vidioc_cropcap
= vidioc_cropcap
,
1777 .vidioc_g_fmt_sliced_vbi_cap
= vidioc_g_fmt_sliced_vbi_cap
,
1778 .vidioc_try_fmt_sliced_vbi_cap
= vidioc_try_set_sliced_vbi_cap
,
1779 .vidioc_s_fmt_sliced_vbi_cap
= vidioc_try_set_sliced_vbi_cap
,
1781 .vidioc_reqbufs
= vidioc_reqbufs
,
1782 .vidioc_querybuf
= vidioc_querybuf
,
1783 .vidioc_qbuf
= vidioc_qbuf
,
1784 .vidioc_dqbuf
= vidioc_dqbuf
,
1785 .vidioc_s_std
= vidioc_s_std
,
1786 .vidioc_enum_input
= vidioc_enum_input
,
1787 .vidioc_g_input
= vidioc_g_input
,
1788 .vidioc_s_input
= vidioc_s_input
,
1789 .vidioc_queryctrl
= vidioc_queryctrl
,
1790 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1791 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1792 .vidioc_streamon
= vidioc_streamon
,
1793 .vidioc_streamoff
= vidioc_streamoff
,
1794 .vidioc_g_tuner
= vidioc_g_tuner
,
1795 .vidioc_s_tuner
= vidioc_s_tuner
,
1796 .vidioc_g_frequency
= vidioc_g_frequency
,
1797 .vidioc_s_frequency
= vidioc_s_frequency
,
1798 #ifdef CONFIG_VIDEO_ADV_DEBUG
1799 .vidioc_g_register
= vidioc_g_register
,
1800 .vidioc_s_register
= vidioc_s_register
,
1802 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1803 .vidiocgmbuf
= vidiocgmbuf
,
1807 static const struct video_device em28xx_video_template
= {
1808 .fops
= &em28xx_v4l_fops
,
1809 .release
= video_device_release
,
1810 .ioctl_ops
= &video_ioctl_ops
,
1814 .tvnorms
= V4L2_STD_ALL
,
1815 .current_norm
= V4L2_STD_PAL
,
1818 static const struct file_operations radio_fops
= {
1819 .owner
= THIS_MODULE
,
1820 .open
= em28xx_v4l2_open
,
1821 .release
= em28xx_v4l2_close
,
1822 .ioctl
= video_ioctl2
,
1823 .compat_ioctl
= v4l_compat_ioctl32
,
1824 .llseek
= no_llseek
,
1827 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
1828 .vidioc_querycap
= radio_querycap
,
1829 .vidioc_g_tuner
= radio_g_tuner
,
1830 .vidioc_enum_input
= radio_enum_input
,
1831 .vidioc_g_audio
= radio_g_audio
,
1832 .vidioc_s_tuner
= radio_s_tuner
,
1833 .vidioc_s_audio
= radio_s_audio
,
1834 .vidioc_s_input
= radio_s_input
,
1835 .vidioc_queryctrl
= radio_queryctrl
,
1836 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1837 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1838 .vidioc_g_frequency
= vidioc_g_frequency
,
1839 .vidioc_s_frequency
= vidioc_s_frequency
,
1840 #ifdef CONFIG_VIDEO_ADV_DEBUG
1841 .vidioc_g_register
= vidioc_g_register
,
1842 .vidioc_s_register
= vidioc_s_register
,
1846 static struct video_device em28xx_radio_template
= {
1847 .name
= "em28xx-radio",
1848 .type
= VID_TYPE_TUNER
,
1849 .fops
= &radio_fops
,
1850 .ioctl_ops
= &radio_ioctl_ops
,
1854 /******************************** usb interface ******************************/
1857 static LIST_HEAD(em28xx_extension_devlist
);
1858 static DEFINE_MUTEX(em28xx_extension_devlist_lock
);
1860 int em28xx_register_extension(struct em28xx_ops
*ops
)
1862 struct em28xx
*dev
= NULL
;
1864 mutex_lock(&em28xx_extension_devlist_lock
);
1865 list_add_tail(&ops
->next
, &em28xx_extension_devlist
);
1866 list_for_each_entry(dev
, &em28xx_devlist
, devlist
) {
1870 printk(KERN_INFO
"Em28xx: Initialized (%s) extension\n", ops
->name
);
1871 mutex_unlock(&em28xx_extension_devlist_lock
);
1874 EXPORT_SYMBOL(em28xx_register_extension
);
1876 void em28xx_unregister_extension(struct em28xx_ops
*ops
)
1878 struct em28xx
*dev
= NULL
;
1880 list_for_each_entry(dev
, &em28xx_devlist
, devlist
) {
1885 mutex_lock(&em28xx_extension_devlist_lock
);
1886 printk(KERN_INFO
"Em28xx: Removed (%s) extension\n", ops
->name
);
1887 list_del(&ops
->next
);
1888 mutex_unlock(&em28xx_extension_devlist_lock
);
1890 EXPORT_SYMBOL(em28xx_unregister_extension
);
1892 static struct video_device
*em28xx_vdev_init(struct em28xx
*dev
,
1893 const struct video_device
*template,
1895 const char *type_name
)
1897 struct video_device
*vfd
;
1899 vfd
= video_device_alloc();
1904 vfd
->parent
= &dev
->udev
->dev
;
1905 vfd
->release
= video_device_release
;
1907 vfd
->debug
= video_debug
;
1909 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s",
1910 dev
->name
, type_name
);
1918 * allocates and inits the device structs, registers i2c bus and v4l device
1920 static int em28xx_init_dev(struct em28xx
**devhandle
, struct usb_device
*udev
,
1923 struct em28xx_ops
*ops
= NULL
;
1924 struct em28xx
*dev
= *devhandle
;
1925 int retval
= -ENOMEM
;
1927 unsigned int maxh
, maxw
;
1930 mutex_init(&dev
->lock
);
1931 spin_lock_init(&dev
->slock
);
1932 init_waitqueue_head(&dev
->open
);
1933 init_waitqueue_head(&dev
->wait_frame
);
1934 init_waitqueue_head(&dev
->wait_stream
);
1936 dev
->em28xx_write_regs
= em28xx_write_regs
;
1937 dev
->em28xx_read_reg
= em28xx_read_reg
;
1938 dev
->em28xx_read_reg_req_len
= em28xx_read_reg_req_len
;
1939 dev
->em28xx_write_regs_req
= em28xx_write_regs_req
;
1940 dev
->em28xx_read_reg_req
= em28xx_read_reg_req
;
1941 dev
->is_em2800
= em28xx_boards
[dev
->model
].is_em2800
;
1943 em28xx_pre_card_setup(dev
);
1945 errCode
= em28xx_config(dev
);
1947 em28xx_errdev("error configuring device\n");
1948 em28xx_devused
&= ~(1<<dev
->devno
);
1953 /* register i2c bus */
1954 em28xx_i2c_register(dev
);
1956 /* Do board specific init and eeprom reading */
1957 em28xx_card_setup(dev
);
1959 /* Configure audio */
1960 em28xx_audio_analog_set(dev
);
1962 /* configure the device */
1963 em28xx_config_i2c(dev
);
1965 /* set default norm */
1966 dev
->norm
= em28xx_video_template
.current_norm
;
1968 maxw
= norm_maxw(dev
);
1969 maxh
= norm_maxh(dev
);
1971 /* set default image size */
1974 dev
->interlaced
= EM28XX_INTERLACED_DEFAULT
;
1979 errCode
= em28xx_config(dev
);
1981 list_add_tail(&dev
->devlist
, &em28xx_devlist
);
1983 /* allocate and fill video video_device struct */
1984 dev
->vdev
= em28xx_vdev_init(dev
, &em28xx_video_template
,
1985 VID_TYPE_CAPTURE
, "video");
1986 if (NULL
== dev
->vdev
) {
1987 em28xx_errdev("cannot allocate video_device.\n");
1990 if (dev
->tuner_type
!= TUNER_ABSENT
)
1991 dev
->vdev
->type
|= VID_TYPE_TUNER
;
1993 /* register v4l2 video video_device */
1994 retval
= video_register_device(dev
->vdev
, VFL_TYPE_GRABBER
,
1995 video_nr
[dev
->devno
]);
1997 em28xx_errdev("unable to register video device (error=%i).\n",
2002 /* Allocate and fill vbi video_device struct */
2003 dev
->vbi_dev
= em28xx_vdev_init(dev
, &em28xx_video_template
,
2004 VFL_TYPE_VBI
, "vbi");
2005 /* register v4l2 vbi video_device */
2006 if (video_register_device(dev
->vbi_dev
, VFL_TYPE_VBI
,
2007 vbi_nr
[dev
->devno
]) < 0) {
2008 em28xx_errdev("unable to register vbi device\n");
2013 if (em28xx_boards
[dev
->model
].radio
.type
== EM28XX_RADIO
) {
2014 dev
->radio_dev
= em28xx_vdev_init(dev
, &em28xx_radio_template
,
2015 VFL_TYPE_RADIO
, "radio");
2016 if (NULL
== dev
->radio_dev
) {
2017 em28xx_errdev("cannot allocate video_device.\n");
2020 retval
= video_register_device(dev
->radio_dev
, VFL_TYPE_RADIO
,
2021 radio_nr
[dev
->devno
]);
2023 em28xx_errdev("can't register radio device\n");
2026 em28xx_info("Registered radio device as /dev/radio%d\n",
2027 dev
->radio_dev
->minor
& 0x1f);
2030 /* init video dma queues */
2031 INIT_LIST_HEAD(&dev
->vidq
.active
);
2032 INIT_LIST_HEAD(&dev
->vidq
.queued
);
2035 if (dev
->has_msp34xx
) {
2036 /* Send a reset to other chips via gpio */
2037 em28xx_write_regs_req(dev
, 0x00, 0x08, "\xf7", 1);
2039 em28xx_write_regs_req(dev
, 0x00, 0x08, "\xff", 1);
2045 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2046 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
,
2047 dev
->vbi_dev
->minor
-MINOR_VFL_TYPE_VBI_MIN
);
2049 mutex_lock(&em28xx_extension_devlist_lock
);
2050 if (!list_empty(&em28xx_extension_devlist
)) {
2051 list_for_each_entry(ops
, &em28xx_extension_devlist
, next
) {
2056 mutex_unlock(&em28xx_extension_devlist_lock
);
2061 em28xx_release_resources(dev
);
2062 mutex_unlock(&dev
->lock
);
2067 #if defined(CONFIG_MODULES) && defined(MODULE)
2068 static void request_module_async(struct work_struct
*work
)
2070 struct em28xx
*dev
= container_of(work
,
2071 struct em28xx
, request_module_wk
);
2073 if (dev
->has_audio_class
)
2074 request_module("snd-usb-audio");
2076 request_module("em28xx-alsa");
2079 request_module("em28xx-dvb");
2082 static void request_modules(struct em28xx
*dev
)
2084 INIT_WORK(&dev
->request_module_wk
, request_module_async
);
2085 schedule_work(&dev
->request_module_wk
);
2088 #define request_modules(dev)
2089 #endif /* CONFIG_MODULES */
2092 * em28xx_usb_probe()
2093 * checks for supported devices
2095 static int em28xx_usb_probe(struct usb_interface
*interface
,
2096 const struct usb_device_id
*id
)
2098 const struct usb_endpoint_descriptor
*endpoint
;
2099 struct usb_device
*udev
;
2100 struct usb_interface
*uif
;
2101 struct em28xx
*dev
= NULL
;
2102 int retval
= -ENODEV
;
2105 udev
= usb_get_dev(interface_to_usbdev(interface
));
2106 ifnum
= interface
->altsetting
[0].desc
.bInterfaceNumber
;
2108 /* Check to see next free device and mark as used */
2109 nr
= find_first_zero_bit(&em28xx_devused
, EM28XX_MAXBOARDS
);
2110 em28xx_devused
|= 1<<nr
;
2112 /* Don't register audio interfaces */
2113 if (interface
->altsetting
[0].desc
.bInterfaceClass
== USB_CLASS_AUDIO
) {
2114 em28xx_err(DRIVER_NAME
" audio device (%04x:%04x): interface %i, class %i\n",
2115 udev
->descriptor
.idVendor
,
2116 udev
->descriptor
.idProduct
,
2118 interface
->altsetting
[0].desc
.bInterfaceClass
);
2120 em28xx_devused
&= ~(1<<nr
);
2124 em28xx_err(DRIVER_NAME
" new video device (%04x:%04x): interface %i, class %i\n",
2125 udev
->descriptor
.idVendor
,
2126 udev
->descriptor
.idProduct
,
2128 interface
->altsetting
[0].desc
.bInterfaceClass
);
2130 endpoint
= &interface
->cur_altsetting
->endpoint
[1].desc
;
2132 /* check if the device has the iso in endpoint at the correct place */
2133 if ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) !=
2134 USB_ENDPOINT_XFER_ISOC
) {
2135 em28xx_err(DRIVER_NAME
" probing error: endpoint is non-ISO endpoint!\n");
2136 em28xx_devused
&= ~(1<<nr
);
2139 if ((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
) {
2140 em28xx_err(DRIVER_NAME
" probing error: endpoint is ISO OUT endpoint!\n");
2141 em28xx_devused
&= ~(1<<nr
);
2145 if (nr
>= EM28XX_MAXBOARDS
) {
2146 printk(DRIVER_NAME
": Supports only %i em28xx boards.\n",
2148 em28xx_devused
&= ~(1<<nr
);
2152 /* allocate memory for our device state and initialize it */
2153 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2155 em28xx_err(DRIVER_NAME
": out of memory!\n");
2156 em28xx_devused
&= ~(1<<nr
);
2160 snprintf(dev
->name
, 29, "em28xx #%d", nr
);
2162 dev
->model
= id
->driver_info
;
2165 /* Checks if audio is provided by some interface */
2166 for (i
= 0; i
< udev
->config
->desc
.bNumInterfaces
; i
++) {
2167 uif
= udev
->config
->interface
[i
];
2168 if (uif
->altsetting
[0].desc
.bInterfaceClass
== USB_CLASS_AUDIO
) {
2169 dev
->has_audio_class
= 1;
2174 printk(KERN_INFO DRIVER_NAME
" %s usb audio class\n",
2175 dev
->has_audio_class
? "Has" : "Doesn't have");
2177 /* compute alternate max packet sizes */
2178 uif
= udev
->actconfig
->interface
[0];
2180 dev
->num_alt
= uif
->num_altsetting
;
2181 em28xx_info("Alternate settings: %i\n", dev
->num_alt
);
2182 /* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2183 dev
->alt_max_pkt_size
= kmalloc(32 * dev
->num_alt
, GFP_KERNEL
);
2185 if (dev
->alt_max_pkt_size
== NULL
) {
2186 em28xx_errdev("out of memory!\n");
2187 em28xx_devused
&= ~(1<<nr
);
2192 for (i
= 0; i
< dev
->num_alt
; i
++) {
2193 u16 tmp
= le16_to_cpu(uif
->altsetting
[i
].endpoint
[1].desc
.
2195 dev
->alt_max_pkt_size
[i
] =
2196 (tmp
& 0x07ff) * (((tmp
& 0x1800) >> 11) + 1);
2197 em28xx_info("Alternate setting %i, max size= %i\n", i
,
2198 dev
->alt_max_pkt_size
[i
]);
2201 if ((card
[nr
] >= 0) && (card
[nr
] < em28xx_bcount
))
2202 dev
->model
= card
[nr
];
2204 /* allocate device struct */
2205 retval
= em28xx_init_dev(&dev
, udev
, nr
);
2209 em28xx_info("Found %s\n", em28xx_boards
[dev
->model
].name
);
2211 /* save our data pointer in this interface device */
2212 usb_set_intfdata(interface
, dev
);
2214 request_modules(dev
);
2220 * em28xx_usb_disconnect()
2221 * called when the device gets diconencted
2222 * video device will be unregistered on v4l2_close in case it is still open
2224 static void em28xx_usb_disconnect(struct usb_interface
*interface
)
2227 struct em28xx_ops
*ops
= NULL
;
2229 dev
= usb_get_intfdata(interface
);
2230 usb_set_intfdata(interface
, NULL
);
2235 em28xx_info("disconnecting %s\n", dev
->vdev
->name
);
2237 /* wait until all current v4l2 io is finished then deallocate
2239 mutex_lock(&dev
->lock
);
2241 wake_up_interruptible_all(&dev
->open
);
2245 ("device /dev/video%d is open! Deregistration and memory "
2246 "deallocation are deferred on close.\n",
2247 dev
->vdev
->minor
-MINOR_VFL_TYPE_GRABBER_MIN
);
2249 dev
->state
|= DEV_MISCONFIGURED
;
2250 em28xx_uninit_isoc(dev
);
2251 dev
->state
|= DEV_DISCONNECTED
;
2252 wake_up_interruptible(&dev
->wait_frame
);
2253 wake_up_interruptible(&dev
->wait_stream
);
2255 dev
->state
|= DEV_DISCONNECTED
;
2256 em28xx_release_resources(dev
);
2258 mutex_unlock(&dev
->lock
);
2260 mutex_lock(&em28xx_extension_devlist_lock
);
2261 if (!list_empty(&em28xx_extension_devlist
)) {
2262 list_for_each_entry(ops
, &em28xx_extension_devlist
, next
) {
2266 mutex_unlock(&em28xx_extension_devlist_lock
);
2269 kfree(dev
->alt_max_pkt_size
);
2274 static struct usb_driver em28xx_usb_driver
= {
2276 .probe
= em28xx_usb_probe
,
2277 .disconnect
= em28xx_usb_disconnect
,
2278 .id_table
= em28xx_id_table
,
2281 static int __init
em28xx_module_init(void)
2285 printk(KERN_INFO DRIVER_NAME
" v4l2 driver version %d.%d.%d loaded\n",
2286 (EM28XX_VERSION_CODE
>> 16) & 0xff,
2287 (EM28XX_VERSION_CODE
>> 8) & 0xff, EM28XX_VERSION_CODE
& 0xff);
2289 printk(KERN_INFO DRIVER_NAME
" snapshot date %04d-%02d-%02d\n",
2290 SNAPSHOT
/ 10000, (SNAPSHOT
/ 100) % 100, SNAPSHOT
% 100);
2293 /* register this driver with the USB subsystem */
2294 result
= usb_register(&em28xx_usb_driver
);
2296 em28xx_err(DRIVER_NAME
2297 " usb_register failed. Error number %d.\n", result
);
2302 static void __exit
em28xx_module_exit(void)
2304 /* deregister this driver with the USB subsystem */
2305 usb_deregister(&em28xx_usb_driver
);
2308 module_init(em28xx_module_init
);
2309 module_exit(em28xx_module_exit
);