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);
76 static DEFINE_MUTEX(em28xx_devlist_mutex);
78 static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
83 module_param_array(card, int, NULL, 0444);
84 module_param_array(video_nr, int, NULL, 0444);
85 module_param_array(vbi_nr, int, NULL, 0444);
86 module_param_array(radio_nr, int, NULL, 0444);
87 MODULE_PARM_DESC(card, "card type");
88 MODULE_PARM_DESC(video_nr, "video device numbers");
89 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
90 MODULE_PARM_DESC(radio_nr, "radio device numbers");
92 static unsigned int video_debug;
93 module_param(video_debug, int, 0644);
94 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
96 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
97 static unsigned long em28xx_devused;
99 /* supported controls */
100 /* Common to all boards */
101 static struct v4l2_queryctrl em28xx_qctrl[] = {
103 .id = V4L2_CID_AUDIO_VOLUME,
104 .type = V4L2_CTRL_TYPE_INTEGER,
109 .default_value = 0x1f,
112 .id = V4L2_CID_AUDIO_MUTE,
113 .type = V4L2_CTRL_TYPE_BOOLEAN,
123 static struct usb_driver em28xx_usb_driver;
125 /* ------------------------------------------------------------------
126 DMA and thread functions
127 ------------------------------------------------------------------*/
130 * Announces that a buffer were filled and request the next
132 static inline void buffer_filled(struct em28xx *dev,
133 struct em28xx_dmaqueue *dma_q,
134 struct em28xx_buffer *buf)
136 /* Advice that buffer was filled */
137 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
138 buf->vb.state = VIDEOBUF_DONE;
139 buf->vb.field_count++;
140 do_gettimeofday(&buf->vb.ts);
142 dev->isoc_ctl.buf = NULL;
144 list_del(&buf->vb.queue);
145 wake_up(&buf->vb.done);
149 * Identify the buffer header type and properly handles
151 static void em28xx_copy_video(struct em28xx *dev,
152 struct em28xx_dmaqueue *dma_q,
153 struct em28xx_buffer *buf,
155 unsigned char *outp, unsigned long len)
157 void *fieldstart, *startwrite, *startread;
158 int linesdone, currlinedone, offset, lencopy, remain;
159 int bytesperline = dev->width << 1;
161 if (dma_q->pos + len > buf->vb.size)
162 len = buf->vb.size - dma_q->pos;
164 if (p[0] != 0x88 && p[0] != 0x22) {
165 em28xx_isocdbg("frame is not complete\n");
173 /* Interlaces frame */
177 fieldstart = outp + bytesperline;
179 linesdone = dma_q->pos / bytesperline;
180 currlinedone = dma_q->pos % bytesperline;
181 offset = linesdone * bytesperline * 2 + currlinedone;
182 startwrite = fieldstart + offset;
183 lencopy = bytesperline - currlinedone;
184 lencopy = lencopy > remain ? remain : lencopy;
186 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
187 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
188 ((char *)startwrite + lencopy) -
189 ((char *)outp + buf->vb.size));
190 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
194 memcpy(startwrite, startread, lencopy);
199 startwrite += lencopy + bytesperline;
200 startread += lencopy;
201 if (bytesperline > remain)
204 lencopy = bytesperline;
206 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
207 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
208 ((char *)startwrite + lencopy) -
209 ((char *)outp + buf->vb.size));
210 lencopy = remain = (char *)outp + buf->vb.size -
216 memcpy(startwrite, startread, lencopy);
224 static inline void print_err_status(struct em28xx *dev,
225 int packet, int status)
227 char *errmsg = "Unknown";
231 errmsg = "unlinked synchronuously";
234 errmsg = "unlinked asynchronuously";
237 errmsg = "Buffer error (overrun)";
240 errmsg = "Stalled (device not responding)";
243 errmsg = "Babble (bad cable?)";
246 errmsg = "Bit-stuff error (bad cable?)";
249 errmsg = "CRC/Timeout (could be anything)";
252 errmsg = "Device does not respond";
256 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
258 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
259 packet, status, errmsg);
264 * video-buf generic routine to get the next available buffer
266 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
267 struct em28xx_buffer **buf)
269 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
272 if (list_empty(&dma_q->active)) {
273 em28xx_isocdbg("No active queue to serve\n");
274 dev->isoc_ctl.buf = NULL;
279 /* Get the next buffer */
280 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
282 /* Cleans up buffer - Usefull for testing for frame/URB loss */
283 outp = videobuf_to_vmalloc(&(*buf)->vb);
284 memset(outp, 0, (*buf)->vb.size);
286 dev->isoc_ctl.buf = *buf;
292 * Controls the isoc copy of each urb packet
294 static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
296 struct em28xx_buffer *buf;
297 struct em28xx_dmaqueue *dma_q = urb->context;
298 unsigned char *outp = NULL;
299 int i, len = 0, rc = 1;
305 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
308 if (urb->status < 0) {
309 print_err_status(dev, -1, urb->status);
310 if (urb->status == -ENOENT)
314 buf = dev->isoc_ctl.buf;
316 outp = videobuf_to_vmalloc(&buf->vb);
318 for (i = 0; i < urb->number_of_packets; i++) {
319 int status = urb->iso_frame_desc[i].status;
322 print_err_status(dev, i, status);
323 if (urb->iso_frame_desc[i].status != -EPROTO)
327 len = urb->iso_frame_desc[i].actual_length - 4;
329 if (urb->iso_frame_desc[i].actual_length <= 0) {
330 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
333 if (urb->iso_frame_desc[i].actual_length >
335 em28xx_isocdbg("packet bigger than packet size");
339 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
341 /* FIXME: incomplete buffer checks where removed to make
342 logic simpler. Impacts of those changes should be evaluated
344 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
345 em28xx_isocdbg("VBI HEADER!!!\n");
346 /* FIXME: Should add vbi copy */
349 if (p[0] == 0x22 && p[1] == 0x5a) {
350 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
351 len, (p[2] & 1)? "odd" : "even");
355 buffer_filled(dev, dma_q, buf);
356 get_next_buf(dma_q, &buf);
360 outp = videobuf_to_vmalloc(&buf->vb);
373 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
378 /* ------------------------------------------------------------------
380 ------------------------------------------------------------------*/
383 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
385 struct em28xx_fh *fh = vq->priv_data;
386 struct em28xx *dev = fh->dev;
387 struct v4l2_frequency f;
389 *size = 16 * fh->dev->width * fh->dev->height >> 3;
391 *count = EM28XX_DEF_BUF;
393 if (*count < EM28XX_MIN_BUF)
394 *count = EM28XX_MIN_BUF;
396 /* Ask tuner to go to analog mode */
397 memset(&f, 0, sizeof(f));
398 f.frequency = dev->ctl_freq;
400 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
405 /* This is called *without* dev->slock held; please keep it that way */
406 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
408 struct em28xx_fh *fh = vq->priv_data;
409 struct em28xx *dev = fh->dev;
410 unsigned long flags = 0;
414 /* We used to wait for the buffer to finish here, but this didn't work
415 because, as we were keeping the state as VIDEOBUF_QUEUED,
416 videobuf_queue_cancel marked it as finished for us.
417 (Also, it could wedge forever if the hardware was misconfigured.)
419 This should be safe; by the time we get here, the buffer isn't
420 queued anymore. If we ever start marking the buffers as
421 VIDEOBUF_ACTIVE, it won't be, though.
423 spin_lock_irqsave(&dev->slock, flags);
424 if (dev->isoc_ctl.buf == buf)
425 dev->isoc_ctl.buf = NULL;
426 spin_unlock_irqrestore(&dev->slock, flags);
428 videobuf_vmalloc_free(&buf->vb);
429 buf->vb.state = VIDEOBUF_NEEDS_INIT;
433 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
434 enum v4l2_field field)
436 struct em28xx_fh *fh = vq->priv_data;
437 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
438 struct em28xx *dev = fh->dev;
439 int rc = 0, urb_init = 0;
441 /* FIXME: It assumes depth = 16 */
442 /* The only currently supported format is 16 bits/pixel */
443 buf->vb.size = 16 * dev->width * dev->height >> 3;
445 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
448 buf->vb.width = dev->width;
449 buf->vb.height = dev->height;
450 buf->vb.field = field;
452 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
453 rc = videobuf_iolock(vq, &buf->vb, NULL);
458 if (!dev->isoc_ctl.num_bufs)
462 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
463 EM28XX_NUM_BUFS, dev->max_pkt_size,
469 buf->vb.state = VIDEOBUF_PREPARED;
473 free_buffer(vq, buf);
478 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
480 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
481 struct em28xx_fh *fh = vq->priv_data;
482 struct em28xx *dev = fh->dev;
483 struct em28xx_dmaqueue *vidq = &dev->vidq;
485 buf->vb.state = VIDEOBUF_QUEUED;
486 list_add_tail(&buf->vb.queue, &vidq->active);
490 static void buffer_release(struct videobuf_queue *vq,
491 struct videobuf_buffer *vb)
493 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
494 struct em28xx_fh *fh = vq->priv_data;
495 struct em28xx *dev = (struct em28xx *)fh->dev;
497 em28xx_isocdbg("em28xx: called buffer_release\n");
499 free_buffer(vq, buf);
502 static struct videobuf_queue_ops em28xx_video_qops = {
503 .buf_setup = buffer_setup,
504 .buf_prepare = buffer_prepare,
505 .buf_queue = buffer_queue,
506 .buf_release = buffer_release,
509 /********************* v4l2 interface **************************************/
513 * inits registers with sane defaults
515 static int em28xx_config(struct em28xx *dev)
519 /* Sets I2C speed to 100 KHz */
520 if (!dev->board.is_em2800) {
521 retval = em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
523 em28xx_errdev("%s: em28xx_write_regs_req failed! retval [%d]\n",
529 /* enable vbi capturing */
531 /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
532 /* em28xx_write_reg(dev, EM28XX_R0F_XCLK, 0x80); clk register */
533 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
535 dev->mute = 1; /* maybe not the right place... */
538 em28xx_outfmt_set_yuv422(dev);
539 em28xx_colorlevels_set_default(dev);
540 em28xx_compression_disable(dev);
546 * em28xx_config_i2c()
547 * configure i2c attached devices
549 static void em28xx_config_i2c(struct em28xx *dev)
551 struct v4l2_routing route;
554 route.input = INPUT(dev->ctl_input)->vmux;
556 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, &zero);
557 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
558 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
561 static void video_mux(struct em28xx *dev, int index)
563 struct v4l2_routing route;
565 route.input = INPUT(index)->vmux;
567 dev->ctl_input = index;
568 dev->ctl_ainput = INPUT(index)->amux;
569 dev->ctl_aoutput = INPUT(index)->aout;
571 if (!dev->ctl_aoutput)
572 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
574 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
576 if (dev->board.has_msp34xx) {
577 if (dev->i2s_speed) {
578 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
581 route.input = dev->ctl_ainput;
582 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
583 /* Note: this is msp3400 specific */
584 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
588 em28xx_audio_analog_set(dev);
591 /* Usage lock check functions */
592 static int res_get(struct em28xx_fh *fh)
594 struct em28xx *dev = fh->dev;
597 /* This instance already has stream_on */
609 static int res_check(struct em28xx_fh *fh)
611 return (fh->stream_on);
614 static void res_free(struct em28xx_fh *fh)
616 struct em28xx *dev = fh->dev;
624 * return the current saturation, brightness or contrast, mute state
626 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
629 case V4L2_CID_AUDIO_MUTE:
630 ctrl->value = dev->mute;
632 case V4L2_CID_AUDIO_VOLUME:
633 ctrl->value = dev->volume;
642 * mute or set new saturation, brightness or contrast
644 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
647 case V4L2_CID_AUDIO_MUTE:
648 if (ctrl->value != dev->mute) {
649 dev->mute = ctrl->value;
650 return em28xx_audio_analog_set(dev);
653 case V4L2_CID_AUDIO_VOLUME:
654 dev->volume = ctrl->value;
655 return em28xx_audio_analog_set(dev);
661 static int check_dev(struct em28xx *dev)
663 if (dev->state & DEV_DISCONNECTED) {
664 em28xx_errdev("v4l2 ioctl: device not present\n");
668 if (dev->state & DEV_MISCONFIGURED) {
669 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
670 "close and open it again\n");
676 static void get_scale(struct em28xx *dev,
677 unsigned int width, unsigned int height,
678 unsigned int *hscale, unsigned int *vscale)
680 unsigned int maxw = norm_maxw(dev);
681 unsigned int maxh = norm_maxh(dev);
683 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
684 if (*hscale >= 0x4000)
687 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
688 if (*vscale >= 0x4000)
692 /* ------------------------------------------------------------------
693 IOCTL vidioc handling
694 ------------------------------------------------------------------*/
696 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
697 struct v4l2_format *f)
699 struct em28xx_fh *fh = priv;
700 struct em28xx *dev = fh->dev;
702 mutex_lock(&dev->lock);
704 f->fmt.pix.width = dev->width;
705 f->fmt.pix.height = dev->height;
706 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
707 f->fmt.pix.bytesperline = dev->width * 2;
708 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
709 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
711 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
712 f->fmt.pix.field = dev->interlaced ?
713 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
715 mutex_unlock(&dev->lock);
719 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
720 struct v4l2_format *f)
722 struct em28xx_fh *fh = priv;
723 struct em28xx *dev = fh->dev;
724 int width = f->fmt.pix.width;
725 int height = f->fmt.pix.height;
726 unsigned int maxw = norm_maxw(dev);
727 unsigned int maxh = norm_maxh(dev);
728 unsigned int hscale, vscale;
730 /* width must even because of the YUYV format
731 height must be even because of interlacing */
735 if (unlikely(height < 32))
737 if (unlikely(height > maxh))
739 if (unlikely(width < 48))
741 if (unlikely(width > maxw))
744 if (dev->board.is_em2800) {
745 /* the em2800 can only scale down to 50% */
746 if (height % (maxh / 2))
748 if (width % (maxw / 2))
750 /* according to empiatech support */
751 /* the MaxPacketSize is to small to support */
752 /* framesizes larger than 640x480 @ 30 fps */
753 /* or 640x576 @ 25 fps. As this would cut */
754 /* of a part of the image we prefer */
755 /* 360x576 or 360x480 for now */
756 if (width == maxw && height == maxh)
760 get_scale(dev, width, height, &hscale, &vscale);
762 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
763 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
765 f->fmt.pix.width = width;
766 f->fmt.pix.height = height;
767 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
768 f->fmt.pix.bytesperline = width * 2;
769 f->fmt.pix.sizeimage = width * 2 * height;
770 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
771 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
776 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
777 struct v4l2_format *f)
779 struct em28xx_fh *fh = priv;
780 struct em28xx *dev = fh->dev;
787 vidioc_try_fmt_vid_cap(file, priv, f);
789 mutex_lock(&dev->lock);
791 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
792 em28xx_errdev("%s queue busy\n", __func__);
797 if (dev->stream_on && !fh->stream_on) {
798 em28xx_errdev("%s device in use by another fh\n", __func__);
803 /* set new image size */
804 dev->width = f->fmt.pix.width;
805 dev->height = f->fmt.pix.height;
806 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
808 em28xx_set_alternate(dev);
809 em28xx_resolution_set(dev);
814 mutex_unlock(&dev->lock);
818 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
820 struct em28xx_fh *fh = priv;
821 struct em28xx *dev = fh->dev;
822 struct v4l2_format f;
829 mutex_lock(&dev->lock);
831 mutex_unlock(&dev->lock);
833 /* Adjusts width/height, if needed */
834 f.fmt.pix.width = dev->width;
835 f.fmt.pix.height = dev->height;
836 vidioc_try_fmt_vid_cap(file, priv, &f);
838 mutex_lock(&dev->lock);
840 /* set new image size */
841 dev->width = f.fmt.pix.width;
842 dev->height = f.fmt.pix.height;
843 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
845 em28xx_resolution_set(dev);
846 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
848 mutex_unlock(&dev->lock);
852 static const char *iname[] = {
853 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
854 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
855 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
856 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
857 [EM28XX_VMUX_SVIDEO] = "S-Video",
858 [EM28XX_VMUX_TELEVISION] = "Television",
859 [EM28XX_VMUX_CABLE] = "Cable TV",
860 [EM28XX_VMUX_DVB] = "DVB",
861 [EM28XX_VMUX_DEBUG] = "for debug only",
864 static int vidioc_enum_input(struct file *file, void *priv,
865 struct v4l2_input *i)
867 struct em28xx_fh *fh = priv;
868 struct em28xx *dev = fh->dev;
872 if (n >= MAX_EM28XX_INPUT)
874 if (0 == INPUT(n)->type)
878 i->type = V4L2_INPUT_TYPE_CAMERA;
880 strcpy(i->name, iname[INPUT(n)->type]);
882 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
883 (EM28XX_VMUX_CABLE == INPUT(n)->type))
884 i->type = V4L2_INPUT_TYPE_TUNER;
886 i->std = dev->vdev->tvnorms;
891 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
893 struct em28xx_fh *fh = priv;
894 struct em28xx *dev = fh->dev;
901 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
903 struct em28xx_fh *fh = priv;
904 struct em28xx *dev = fh->dev;
911 if (i >= MAX_EM28XX_INPUT)
913 if (0 == INPUT(i)->type)
916 mutex_lock(&dev->lock);
920 mutex_unlock(&dev->lock);
924 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
926 struct em28xx_fh *fh = priv;
927 struct em28xx *dev = fh->dev;
930 case EM28XX_AMUX_VIDEO:
931 strcpy(a->name, "Television");
933 case EM28XX_AMUX_LINE_IN:
934 strcpy(a->name, "Line In");
936 case EM28XX_AMUX_VIDEO2:
937 strcpy(a->name, "Television alt");
939 case EM28XX_AMUX_PHONE:
940 strcpy(a->name, "Phone");
942 case EM28XX_AMUX_MIC:
943 strcpy(a->name, "Mic");
946 strcpy(a->name, "CD");
948 case EM28XX_AMUX_AUX:
949 strcpy(a->name, "Aux");
951 case EM28XX_AMUX_PCM_OUT:
952 strcpy(a->name, "PCM");
958 a->index = dev->ctl_ainput;
959 a->capability = V4L2_AUDCAP_STEREO;
964 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
966 struct em28xx_fh *fh = priv;
967 struct em28xx *dev = fh->dev;
969 dev->ctl_ainput = INPUT(a->index)->amux;
970 dev->ctl_aoutput = INPUT(a->index)->aout;
972 if (!dev->ctl_aoutput)
973 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
977 static int vidioc_queryctrl(struct file *file, void *priv,
978 struct v4l2_queryctrl *qc)
980 struct em28xx_fh *fh = priv;
981 struct em28xx *dev = fh->dev;
990 memset(qc, 0, sizeof(*qc));
994 if (!dev->board.has_msp34xx) {
995 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
996 if (qc->id && qc->id == em28xx_qctrl[i].id) {
997 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1002 mutex_lock(&dev->lock);
1003 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1004 mutex_unlock(&dev->lock);
1012 static int vidioc_g_ctrl(struct file *file, void *priv,
1013 struct v4l2_control *ctrl)
1015 struct em28xx_fh *fh = priv;
1016 struct em28xx *dev = fh->dev;
1019 rc = check_dev(dev);
1022 mutex_lock(&dev->lock);
1024 if (!dev->board.has_msp34xx)
1025 rc = em28xx_get_ctrl(dev, ctrl);
1029 if (rc == -EINVAL) {
1030 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1034 mutex_unlock(&dev->lock);
1038 static int vidioc_s_ctrl(struct file *file, void *priv,
1039 struct v4l2_control *ctrl)
1041 struct em28xx_fh *fh = priv;
1042 struct em28xx *dev = fh->dev;
1046 rc = check_dev(dev);
1050 mutex_lock(&dev->lock);
1052 if (dev->board.has_msp34xx)
1053 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1056 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1057 if (ctrl->id == em28xx_qctrl[i].id) {
1058 if (ctrl->value < em28xx_qctrl[i].minimum ||
1059 ctrl->value > em28xx_qctrl[i].maximum) {
1064 rc = em28xx_set_ctrl(dev, ctrl);
1070 /* Control not found - try to send it to the attached devices */
1072 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1076 mutex_unlock(&dev->lock);
1080 static int vidioc_g_tuner(struct file *file, void *priv,
1081 struct v4l2_tuner *t)
1083 struct em28xx_fh *fh = priv;
1084 struct em28xx *dev = fh->dev;
1087 rc = check_dev(dev);
1094 strcpy(t->name, "Tuner");
1096 mutex_lock(&dev->lock);
1098 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1100 mutex_unlock(&dev->lock);
1104 static int vidioc_s_tuner(struct file *file, void *priv,
1105 struct v4l2_tuner *t)
1107 struct em28xx_fh *fh = priv;
1108 struct em28xx *dev = fh->dev;
1111 rc = check_dev(dev);
1118 mutex_lock(&dev->lock);
1120 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1122 mutex_unlock(&dev->lock);
1126 static int vidioc_g_frequency(struct file *file, void *priv,
1127 struct v4l2_frequency *f)
1129 struct em28xx_fh *fh = priv;
1130 struct em28xx *dev = fh->dev;
1132 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1133 f->frequency = dev->ctl_freq;
1138 static int vidioc_s_frequency(struct file *file, void *priv,
1139 struct v4l2_frequency *f)
1141 struct em28xx_fh *fh = priv;
1142 struct em28xx *dev = fh->dev;
1145 rc = check_dev(dev);
1152 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1154 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1157 mutex_lock(&dev->lock);
1159 dev->ctl_freq = f->frequency;
1160 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1162 mutex_unlock(&dev->lock);
1166 #ifdef CONFIG_VIDEO_ADV_DEBUG
1167 static int em28xx_reg_len(int reg)
1170 case EM28XX_R40_AC97LSB:
1171 case EM28XX_R30_HSCALELOW:
1172 case EM28XX_R32_VSCALELOW:
1179 static int vidioc_g_register(struct file *file, void *priv,
1180 struct v4l2_register *reg)
1182 struct em28xx_fh *fh = priv;
1183 struct em28xx *dev = fh->dev;
1186 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1189 if (em28xx_reg_len(reg->reg) == 1) {
1190 ret = em28xx_read_reg(dev, reg->reg);
1197 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1198 reg->reg, (char *)&val, 2);
1202 reg->val = le64_to_cpu(val);
1208 static int vidioc_s_register(struct file *file, void *priv,
1209 struct v4l2_register *reg)
1211 struct em28xx_fh *fh = priv;
1212 struct em28xx *dev = fh->dev;
1215 buf = cpu_to_le64(reg->val);
1217 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1218 em28xx_reg_len(reg->reg));
1223 static int vidioc_cropcap(struct file *file, void *priv,
1224 struct v4l2_cropcap *cc)
1226 struct em28xx_fh *fh = priv;
1227 struct em28xx *dev = fh->dev;
1229 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1232 cc->bounds.left = 0;
1234 cc->bounds.width = dev->width;
1235 cc->bounds.height = dev->height;
1236 cc->defrect = cc->bounds;
1237 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1238 cc->pixelaspect.denominator = 59;
1243 static int vidioc_streamon(struct file *file, void *priv,
1244 enum v4l2_buf_type type)
1246 struct em28xx_fh *fh = priv;
1247 struct em28xx *dev = fh->dev;
1250 rc = check_dev(dev);
1255 mutex_lock(&dev->lock);
1257 mutex_unlock(&dev->lock);
1259 if (unlikely(rc < 0))
1262 return (videobuf_streamon(&fh->vb_vidq));
1265 static int vidioc_streamoff(struct file *file, void *priv,
1266 enum v4l2_buf_type type)
1268 struct em28xx_fh *fh = priv;
1269 struct em28xx *dev = fh->dev;
1272 rc = check_dev(dev);
1276 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1278 if (type != fh->type)
1281 videobuf_streamoff(&fh->vb_vidq);
1282 mutex_lock(&dev->lock);
1284 mutex_unlock(&dev->lock);
1289 static int vidioc_querycap(struct file *file, void *priv,
1290 struct v4l2_capability *cap)
1292 struct em28xx_fh *fh = priv;
1293 struct em28xx *dev = fh->dev;
1295 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1296 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1297 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
1299 cap->version = EM28XX_VERSION_CODE;
1302 V4L2_CAP_SLICED_VBI_CAPTURE |
1303 V4L2_CAP_VIDEO_CAPTURE |
1305 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1307 if (dev->tuner_type != TUNER_ABSENT)
1308 cap->capabilities |= V4L2_CAP_TUNER;
1313 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1314 struct v4l2_fmtdesc *fmtd)
1316 if (fmtd->index != 0)
1319 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1320 strcpy(fmtd->description, "Packed YUY2");
1321 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1322 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1327 /* Sliced VBI ioctls */
1328 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1329 struct v4l2_format *f)
1331 struct em28xx_fh *fh = priv;
1332 struct em28xx *dev = fh->dev;
1335 rc = check_dev(dev);
1339 mutex_lock(&dev->lock);
1341 f->fmt.sliced.service_set = 0;
1343 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1345 if (f->fmt.sliced.service_set == 0)
1348 mutex_unlock(&dev->lock);
1352 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1353 struct v4l2_format *f)
1355 struct em28xx_fh *fh = priv;
1356 struct em28xx *dev = fh->dev;
1359 rc = check_dev(dev);
1363 mutex_lock(&dev->lock);
1364 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1365 mutex_unlock(&dev->lock);
1367 if (f->fmt.sliced.service_set == 0)
1374 static int vidioc_reqbufs(struct file *file, void *priv,
1375 struct v4l2_requestbuffers *rb)
1377 struct em28xx_fh *fh = priv;
1378 struct em28xx *dev = fh->dev;
1381 rc = check_dev(dev);
1385 return (videobuf_reqbufs(&fh->vb_vidq, rb));
1388 static int vidioc_querybuf(struct file *file, void *priv,
1389 struct v4l2_buffer *b)
1391 struct em28xx_fh *fh = priv;
1392 struct em28xx *dev = fh->dev;
1395 rc = check_dev(dev);
1399 return (videobuf_querybuf(&fh->vb_vidq, b));
1402 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1404 struct em28xx_fh *fh = priv;
1405 struct em28xx *dev = fh->dev;
1408 rc = check_dev(dev);
1412 return (videobuf_qbuf(&fh->vb_vidq, b));
1415 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1417 struct em28xx_fh *fh = priv;
1418 struct em28xx *dev = fh->dev;
1421 rc = check_dev(dev);
1425 return (videobuf_dqbuf(&fh->vb_vidq, b,
1426 file->f_flags & O_NONBLOCK));
1429 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1430 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1432 struct em28xx_fh *fh = priv;
1434 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1439 /* ----------------------------------------------------------- */
1440 /* RADIO ESPECIFIC IOCTLS */
1441 /* ----------------------------------------------------------- */
1443 static int radio_querycap(struct file *file, void *priv,
1444 struct v4l2_capability *cap)
1446 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1448 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1449 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1450 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
1452 cap->version = EM28XX_VERSION_CODE;
1453 cap->capabilities = V4L2_CAP_TUNER;
1457 static int radio_g_tuner(struct file *file, void *priv,
1458 struct v4l2_tuner *t)
1460 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1462 if (unlikely(t->index > 0))
1465 strcpy(t->name, "Radio");
1466 t->type = V4L2_TUNER_RADIO;
1468 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1472 static int radio_enum_input(struct file *file, void *priv,
1473 struct v4l2_input *i)
1477 strcpy(i->name, "Radio");
1478 i->type = V4L2_INPUT_TYPE_TUNER;
1483 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1485 if (unlikely(a->index))
1488 strcpy(a->name, "Radio");
1492 static int radio_s_tuner(struct file *file, void *priv,
1493 struct v4l2_tuner *t)
1495 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1500 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1505 static int radio_s_audio(struct file *file, void *fh,
1506 struct v4l2_audio *a)
1511 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1516 static int radio_queryctrl(struct file *file, void *priv,
1517 struct v4l2_queryctrl *qc)
1521 if (qc->id < V4L2_CID_BASE ||
1522 qc->id >= V4L2_CID_LASTP1)
1525 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1526 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1527 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1536 * em28xx_v4l2_open()
1537 * inits the device and starts isoc transfer
1539 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1541 int minor = iminor(inode);
1542 int errCode = 0, radio = 0;
1543 struct em28xx *h, *dev = NULL;
1544 struct em28xx_fh *fh;
1545 enum v4l2_buf_type fh_type = 0;
1547 mutex_lock(&em28xx_devlist_mutex);
1548 list_for_each_entry(h, &em28xx_devlist, devlist) {
1549 if (h->vdev->minor == minor) {
1551 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1553 if (h->vbi_dev->minor == minor) {
1555 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1558 h->radio_dev->minor == minor) {
1563 mutex_unlock(&em28xx_devlist_mutex);
1567 mutex_lock(&dev->lock);
1569 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1570 minor, v4l2_type_names[fh_type], dev->users);
1573 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1575 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1576 mutex_unlock(&dev->lock);
1582 filp->private_data = fh;
1584 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1585 dev->width = norm_maxw(dev);
1586 dev->height = norm_maxh(dev);
1590 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
1591 em28xx_set_alternate(dev);
1592 em28xx_resolution_set(dev);
1594 /* Needed, since GPIO might have disabled power of
1597 em28xx_config_i2c(dev);
1601 em28xx_videodbg("video_open: setting radio device\n");
1602 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1607 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1608 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1609 sizeof(struct em28xx_buffer), fh);
1611 mutex_unlock(&dev->lock);
1617 * em28xx_realease_resources()
1618 * unregisters the v4l2,i2c and usb devices
1619 * called when the device gets disconected or at module unload
1621 static void em28xx_release_resources(struct em28xx *dev)
1624 /*FIXME: I2C IR should be disconnected */
1626 list_del(&dev->devlist);
1627 if (dev->sbutton_input_dev)
1628 em28xx_deregister_snapshot_button(dev);
1631 em28xx_ir_fini(dev);
1633 if (dev->radio_dev) {
1634 if (-1 != dev->radio_dev->minor)
1635 video_unregister_device(dev->radio_dev);
1637 video_device_release(dev->radio_dev);
1638 dev->radio_dev = NULL;
1641 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1643 if (-1 != dev->vbi_dev->minor)
1644 video_unregister_device(dev->vbi_dev);
1646 video_device_release(dev->vbi_dev);
1647 dev->vbi_dev = NULL;
1650 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1652 if (-1 != dev->vdev->minor)
1653 video_unregister_device(dev->vdev);
1655 video_device_release(dev->vdev);
1658 em28xx_i2c_unregister(dev);
1659 usb_put_dev(dev->udev);
1661 /* Mark device as unused */
1662 em28xx_devused &= ~(1<<dev->devno);
1666 * em28xx_v4l2_close()
1667 * stops streaming and deallocates all resources allocated by the v4l2
1670 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1672 struct em28xx_fh *fh = filp->private_data;
1673 struct em28xx *dev = fh->dev;
1676 em28xx_videodbg("users=%d\n", dev->users);
1679 mutex_lock(&dev->lock);
1683 if (dev->users == 1) {
1684 videobuf_stop(&fh->vb_vidq);
1685 videobuf_mmap_free(&fh->vb_vidq);
1687 /* the device is already disconnect,
1688 free the remaining resources */
1689 if (dev->state & DEV_DISCONNECTED) {
1690 em28xx_release_resources(dev);
1691 mutex_unlock(&dev->lock);
1696 /* Save some power by putting tuner to sleep */
1697 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1699 /* do this before setting alternate! */
1700 em28xx_uninit_isoc(dev);
1701 em28xx_set_mode(dev, EM28XX_SUSPEND);
1703 /* set alternate 0 */
1705 em28xx_videodbg("setting alternate 0\n");
1706 errCode = usb_set_interface(dev->udev, 0, 0);
1708 em28xx_errdev("cannot change alternate number to "
1709 "0 (error=%i)\n", errCode);
1714 wake_up_interruptible_nr(&dev->open, 1);
1715 mutex_unlock(&dev->lock);
1720 * em28xx_v4l2_read()
1721 * will allocate buffers when called for the first time
1724 em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1727 struct em28xx_fh *fh = filp->private_data;
1728 struct em28xx *dev = fh->dev;
1731 rc = check_dev(dev);
1735 /* FIXME: read() is not prepared to allow changing the video
1736 resolution while streaming. Seems a bug at em28xx_set_fmt
1739 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1740 mutex_lock(&dev->lock);
1742 mutex_unlock(&dev->lock);
1744 if (unlikely(rc < 0))
1747 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1748 filp->f_flags & O_NONBLOCK);
1754 * em28xx_v4l2_poll()
1755 * will allocate buffers when called for the first time
1757 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
1759 struct em28xx_fh *fh = filp->private_data;
1760 struct em28xx *dev = fh->dev;
1763 rc = check_dev(dev);
1767 mutex_lock(&dev->lock);
1769 mutex_unlock(&dev->lock);
1771 if (unlikely(rc < 0))
1774 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1777 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1781 * em28xx_v4l2_mmap()
1783 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1785 struct em28xx_fh *fh = filp->private_data;
1786 struct em28xx *dev = fh->dev;
1789 rc = check_dev(dev);
1793 mutex_lock(&dev->lock);
1795 mutex_unlock(&dev->lock);
1797 if (unlikely(rc < 0))
1800 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1802 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1803 (unsigned long)vma->vm_start,
1804 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1810 static const struct file_operations em28xx_v4l_fops = {
1811 .owner = THIS_MODULE,
1812 .open = em28xx_v4l2_open,
1813 .release = em28xx_v4l2_close,
1814 .read = em28xx_v4l2_read,
1815 .poll = em28xx_v4l2_poll,
1816 .mmap = em28xx_v4l2_mmap,
1817 .ioctl = video_ioctl2,
1818 .llseek = no_llseek,
1819 .compat_ioctl = v4l_compat_ioctl32,
1822 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1823 .vidioc_querycap = vidioc_querycap,
1824 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1825 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1826 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1827 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1828 .vidioc_g_audio = vidioc_g_audio,
1829 .vidioc_s_audio = vidioc_s_audio,
1830 .vidioc_cropcap = vidioc_cropcap,
1832 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1833 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1834 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1836 .vidioc_reqbufs = vidioc_reqbufs,
1837 .vidioc_querybuf = vidioc_querybuf,
1838 .vidioc_qbuf = vidioc_qbuf,
1839 .vidioc_dqbuf = vidioc_dqbuf,
1840 .vidioc_s_std = vidioc_s_std,
1841 .vidioc_enum_input = vidioc_enum_input,
1842 .vidioc_g_input = vidioc_g_input,
1843 .vidioc_s_input = vidioc_s_input,
1844 .vidioc_queryctrl = vidioc_queryctrl,
1845 .vidioc_g_ctrl = vidioc_g_ctrl,
1846 .vidioc_s_ctrl = vidioc_s_ctrl,
1847 .vidioc_streamon = vidioc_streamon,
1848 .vidioc_streamoff = vidioc_streamoff,
1849 .vidioc_g_tuner = vidioc_g_tuner,
1850 .vidioc_s_tuner = vidioc_s_tuner,
1851 .vidioc_g_frequency = vidioc_g_frequency,
1852 .vidioc_s_frequency = vidioc_s_frequency,
1853 #ifdef CONFIG_VIDEO_ADV_DEBUG
1854 .vidioc_g_register = vidioc_g_register,
1855 .vidioc_s_register = vidioc_s_register,
1857 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1858 .vidiocgmbuf = vidiocgmbuf,
1862 static const struct video_device em28xx_video_template = {
1863 .fops = &em28xx_v4l_fops,
1864 .release = video_device_release,
1865 .ioctl_ops = &video_ioctl_ops,
1869 .tvnorms = V4L2_STD_ALL,
1870 .current_norm = V4L2_STD_PAL,
1873 static const struct file_operations radio_fops = {
1874 .owner = THIS_MODULE,
1875 .open = em28xx_v4l2_open,
1876 .release = em28xx_v4l2_close,
1877 .ioctl = video_ioctl2,
1878 .compat_ioctl = v4l_compat_ioctl32,
1879 .llseek = no_llseek,
1882 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1883 .vidioc_querycap = radio_querycap,
1884 .vidioc_g_tuner = radio_g_tuner,
1885 .vidioc_enum_input = radio_enum_input,
1886 .vidioc_g_audio = radio_g_audio,
1887 .vidioc_s_tuner = radio_s_tuner,
1888 .vidioc_s_audio = radio_s_audio,
1889 .vidioc_s_input = radio_s_input,
1890 .vidioc_queryctrl = radio_queryctrl,
1891 .vidioc_g_ctrl = vidioc_g_ctrl,
1892 .vidioc_s_ctrl = vidioc_s_ctrl,
1893 .vidioc_g_frequency = vidioc_g_frequency,
1894 .vidioc_s_frequency = vidioc_s_frequency,
1895 #ifdef CONFIG_VIDEO_ADV_DEBUG
1896 .vidioc_g_register = vidioc_g_register,
1897 .vidioc_s_register = vidioc_s_register,
1901 static struct video_device em28xx_radio_template = {
1902 .name = "em28xx-radio",
1903 .fops = &radio_fops,
1904 .ioctl_ops = &radio_ioctl_ops,
1908 /******************************** usb interface ******************************/
1911 static LIST_HEAD(em28xx_extension_devlist);
1912 static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1914 int em28xx_register_extension(struct em28xx_ops *ops)
1916 struct em28xx *dev = NULL;
1918 mutex_lock(&em28xx_devlist_mutex);
1919 mutex_lock(&em28xx_extension_devlist_lock);
1920 list_add_tail(&ops->next, &em28xx_extension_devlist);
1921 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1925 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1926 mutex_unlock(&em28xx_extension_devlist_lock);
1927 mutex_unlock(&em28xx_devlist_mutex);
1930 EXPORT_SYMBOL(em28xx_register_extension);
1932 void em28xx_unregister_extension(struct em28xx_ops *ops)
1934 struct em28xx *dev = NULL;
1936 mutex_lock(&em28xx_devlist_mutex);
1937 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1942 mutex_lock(&em28xx_extension_devlist_lock);
1943 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1944 list_del(&ops->next);
1945 mutex_unlock(&em28xx_extension_devlist_lock);
1946 mutex_unlock(&em28xx_devlist_mutex);
1948 EXPORT_SYMBOL(em28xx_unregister_extension);
1950 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1951 const struct video_device *template,
1952 const char *type_name)
1954 struct video_device *vfd;
1956 vfd = video_device_alloc();
1961 vfd->parent = &dev->udev->dev;
1962 vfd->release = video_device_release;
1963 vfd->debug = video_debug;
1965 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1966 dev->name, type_name);
1971 static int register_analog_devices(struct em28xx *dev)
1975 /* allocate and fill video video_device struct */
1976 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
1978 em28xx_errdev("cannot allocate video_device.\n");
1982 /* register v4l2 video video_device */
1983 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1984 video_nr[dev->devno]);
1986 em28xx_errdev("unable to register video device (error=%i).\n",
1991 /* Allocate and fill vbi video_device struct */
1992 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
1994 /* register v4l2 vbi video_device */
1995 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1996 vbi_nr[dev->devno]);
1998 em28xx_errdev("unable to register vbi device\n");
2002 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2003 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
2004 if (!dev->radio_dev) {
2005 em28xx_errdev("cannot allocate video_device.\n");
2008 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2009 radio_nr[dev->devno]);
2011 em28xx_errdev("can't register radio device\n");
2014 em28xx_info("Registered radio device as /dev/radio%d\n",
2015 dev->radio_dev->num);
2018 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2019 dev->vdev->num, dev->vbi_dev->num);
2027 * allocates and inits the device structs, registers i2c bus and v4l device
2029 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
2032 struct em28xx_ops *ops = NULL;
2033 struct em28xx *dev = *devhandle;
2034 int retval = -ENOMEM;
2036 unsigned int maxh, maxw;
2039 mutex_init(&dev->lock);
2040 mutex_init(&dev->ctrl_urb_lock);
2041 spin_lock_init(&dev->slock);
2042 init_waitqueue_head(&dev->open);
2043 init_waitqueue_head(&dev->wait_frame);
2044 init_waitqueue_head(&dev->wait_stream);
2046 dev->em28xx_write_regs = em28xx_write_regs;
2047 dev->em28xx_read_reg = em28xx_read_reg;
2048 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2049 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2050 dev->em28xx_read_reg_req = em28xx_read_reg_req;
2051 dev->board.is_em2800 = em28xx_boards[dev->model].is_em2800;
2053 em28xx_pre_card_setup(dev);
2055 errCode = em28xx_config(dev);
2057 em28xx_errdev("error configuring device\n");
2061 /* register i2c bus */
2062 errCode = em28xx_i2c_register(dev);
2064 em28xx_errdev("%s: em28xx_i2c_register - errCode [%d]!\n",
2069 /* Do board specific init and eeprom reading */
2070 em28xx_card_setup(dev);
2072 /* Configure audio */
2073 errCode = em28xx_audio_setup(dev);
2075 em28xx_errdev("%s: Error while setting audio - errCode [%d]!\n",
2079 /* configure the device */
2080 em28xx_config_i2c(dev);
2082 /* set default norm */
2083 dev->norm = em28xx_video_template.current_norm;
2085 maxw = norm_maxw(dev);
2086 maxh = norm_maxh(dev);
2088 /* set default image size */
2091 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
2096 errCode = em28xx_config(dev);
2098 em28xx_errdev("%s: em28xx_config - errCode [%d]!\n",
2103 /* init video dma queues */
2104 INIT_LIST_HEAD(&dev->vidq.active);
2105 INIT_LIST_HEAD(&dev->vidq.queued);
2108 if (dev->board.has_msp34xx) {
2109 /* Send a reset to other chips via gpio */
2110 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xf7);
2112 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(1) failed! errCode [%d]\n",
2118 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xff);
2120 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(2) failed! errCode [%d]\n",
2129 mutex_lock(&em28xx_devlist_mutex);
2130 list_add_tail(&dev->devlist, &em28xx_devlist);
2131 retval = register_analog_devices(dev);
2133 em28xx_release_resources(dev);
2134 mutex_unlock(&em28xx_devlist_mutex);
2135 goto fail_reg_devices;
2138 mutex_lock(&em28xx_extension_devlist_lock);
2139 if (!list_empty(&em28xx_extension_devlist)) {
2140 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2145 mutex_unlock(&em28xx_extension_devlist_lock);
2146 mutex_unlock(&em28xx_devlist_mutex);
2148 /* Save some power by putting tuner to sleep */
2149 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
2154 mutex_unlock(&dev->lock);
2158 #if defined(CONFIG_MODULES) && defined(MODULE)
2159 static void request_module_async(struct work_struct *work)
2161 struct em28xx *dev = container_of(work,
2162 struct em28xx, request_module_wk);
2164 if (dev->has_audio_class)
2165 request_module("snd-usb-audio");
2166 else if (dev->has_alsa_audio)
2167 request_module("em28xx-alsa");
2169 if (dev->board.has_dvb)
2170 request_module("em28xx-dvb");
2173 static void request_modules(struct em28xx *dev)
2175 INIT_WORK(&dev->request_module_wk, request_module_async);
2176 schedule_work(&dev->request_module_wk);
2179 #define request_modules(dev)
2180 #endif /* CONFIG_MODULES */
2183 * em28xx_usb_probe()
2184 * checks for supported devices
2186 static int em28xx_usb_probe(struct usb_interface *interface,
2187 const struct usb_device_id *id)
2189 const struct usb_endpoint_descriptor *endpoint;
2190 struct usb_device *udev;
2191 struct usb_interface *uif;
2192 struct em28xx *dev = NULL;
2193 int retval = -ENODEV;
2194 int i, nr, ifnum, isoc_pipe;
2196 char descr[255] = "";
2198 udev = usb_get_dev(interface_to_usbdev(interface));
2199 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2201 /* Check to see next free device and mark as used */
2202 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2203 em28xx_devused |= 1<<nr;
2205 /* Don't register audio interfaces */
2206 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2207 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): "
2208 "interface %i, class %i\n",
2209 le16_to_cpu(udev->descriptor.idVendor),
2210 le16_to_cpu(udev->descriptor.idProduct),
2212 interface->altsetting[0].desc.bInterfaceClass);
2214 em28xx_devused &= ~(1<<nr);
2218 endpoint = &interface->cur_altsetting->endpoint[0].desc;
2220 /* check if the device has the iso in endpoint at the correct place */
2221 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
2222 USB_ENDPOINT_XFER_ISOC &&
2223 (interface->altsetting[1].endpoint[0].desc.wMaxPacketSize == 940))
2225 /* It's a newer em2874/em2875 device */
2228 int check_interface = 1;
2230 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2231 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2232 USB_ENDPOINT_XFER_ISOC)
2233 check_interface = 0;
2235 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
2236 check_interface = 0;
2238 if (!check_interface) {
2239 em28xx_err(DRIVER_NAME " video device (%04x:%04x): "
2240 "interface %i, class %i found.\n",
2241 le16_to_cpu(udev->descriptor.idVendor),
2242 le16_to_cpu(udev->descriptor.idProduct),
2244 interface->altsetting[0].desc.bInterfaceClass);
2246 em28xx_err(DRIVER_NAME " This is an anciliary "
2247 "interface not used by the driver\n");
2249 em28xx_devused &= ~(1<<nr);
2254 switch (udev->speed) {
2258 case USB_SPEED_UNKNOWN:
2259 case USB_SPEED_FULL:
2262 case USB_SPEED_HIGH:
2269 if (udev->manufacturer)
2270 strlcpy(descr, udev->manufacturer, sizeof(descr));
2272 if (udev->product) {
2274 strlcat(descr, " ", sizeof(descr));
2275 strlcat(descr, udev->product, sizeof(descr));
2278 strlcat(descr, " ", sizeof(descr));
2280 printk(DRIVER_NAME ": New device %s@ %s Mbps "
2281 "(%04x:%04x, interface %d, class %d)\n",
2284 le16_to_cpu(udev->descriptor.idVendor),
2285 le16_to_cpu(udev->descriptor.idProduct),
2287 interface->altsetting->desc.bInterfaceNumber);
2289 if (nr >= EM28XX_MAXBOARDS) {
2290 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2292 em28xx_devused &= ~(1<<nr);
2296 /* allocate memory for our device state and initialize it */
2297 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2299 em28xx_err(DRIVER_NAME ": out of memory!\n");
2300 em28xx_devused &= ~(1<<nr);
2304 snprintf(dev->name, 29, "em28xx #%d", nr);
2306 dev->model = id->driver_info;
2309 /* Checks if audio is provided by some interface */
2310 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2311 uif = udev->config->interface[i];
2312 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2313 dev->has_audio_class = 1;
2318 /* compute alternate max packet sizes */
2319 uif = udev->actconfig->interface[0];
2321 dev->num_alt = uif->num_altsetting;
2322 em28xx_videodbg("Alternate settings: %i\n", dev->num_alt);
2323 /* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2324 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2326 if (dev->alt_max_pkt_size == NULL) {
2327 em28xx_errdev("out of memory!\n");
2328 em28xx_devused &= ~(1<<nr);
2333 for (i = 0; i < dev->num_alt ; i++) {
2334 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
2336 dev->alt_max_pkt_size[i] =
2337 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
2338 em28xx_videodbg("Alternate setting %i, max size= %i\n", i,
2339 dev->alt_max_pkt_size[i]);
2342 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
2343 dev->model = card[nr];
2345 /* allocate device struct */
2346 retval = em28xx_init_dev(&dev, udev, nr);
2348 em28xx_devused &= ~(1<<dev->devno);
2354 /* save our data pointer in this interface device */
2355 usb_set_intfdata(interface, dev);
2357 request_modules(dev);
2363 * em28xx_usb_disconnect()
2364 * called when the device gets diconencted
2365 * video device will be unregistered on v4l2_close in case it is still open
2367 static void em28xx_usb_disconnect(struct usb_interface *interface)
2370 struct em28xx_ops *ops = NULL;
2372 dev = usb_get_intfdata(interface);
2373 usb_set_intfdata(interface, NULL);
2378 em28xx_info("disconnecting %s\n", dev->vdev->name);
2380 /* wait until all current v4l2 io is finished then deallocate
2382 mutex_lock(&dev->lock);
2384 wake_up_interruptible_all(&dev->open);
2388 ("device /dev/video%d is open! Deregistration and memory "
2389 "deallocation are deferred on close.\n",
2392 dev->state |= DEV_MISCONFIGURED;
2393 em28xx_uninit_isoc(dev);
2394 dev->state |= DEV_DISCONNECTED;
2395 wake_up_interruptible(&dev->wait_frame);
2396 wake_up_interruptible(&dev->wait_stream);
2398 dev->state |= DEV_DISCONNECTED;
2399 em28xx_release_resources(dev);
2401 mutex_unlock(&dev->lock);
2403 mutex_lock(&em28xx_extension_devlist_lock);
2404 if (!list_empty(&em28xx_extension_devlist)) {
2405 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2409 mutex_unlock(&em28xx_extension_devlist_lock);
2412 kfree(dev->alt_max_pkt_size);
2417 static struct usb_driver em28xx_usb_driver = {
2419 .probe = em28xx_usb_probe,
2420 .disconnect = em28xx_usb_disconnect,
2421 .id_table = em28xx_id_table,
2424 static int __init em28xx_module_init(void)
2428 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
2429 (EM28XX_VERSION_CODE >> 16) & 0xff,
2430 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2432 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2433 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2436 /* register this driver with the USB subsystem */
2437 result = usb_register(&em28xx_usb_driver);
2439 em28xx_err(DRIVER_NAME
2440 " usb_register failed. Error number %d.\n", result);
2445 static void __exit em28xx_module_exit(void)
2447 /* deregister this driver with the USB subsystem */
2448 usb_deregister(&em28xx_usb_driver);
2451 module_init(em28xx_module_init);
2452 module_exit(em28xx_module_exit);