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->is_em2800) {
521 retval = em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
523 em28xx_errdev("%s: em28xx_write_regs_req failed! retval [%d]\n",
529 /* enable vbi capturing */
531 /* em28xx_write_regs_req(dev, 0x00, 0x0e, "\xC0", 1); audio register */
532 /* em28xx_write_regs_req(dev, 0x00, 0x0f, "\x80", 1); clk register */
533 em28xx_write_regs_req(dev, 0x00, 0x11, "\x51", 1);
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 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
573 if (dev->has_msp34xx) {
574 if (dev->i2s_speed) {
575 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
578 route.input = dev->ctl_ainput;
579 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
580 /* Note: this is msp3400 specific */
581 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
585 em28xx_audio_analog_set(dev);
588 /* Usage lock check functions */
589 static int res_get(struct em28xx_fh *fh)
591 struct em28xx *dev = fh->dev;
594 /* This instance already has stream_on */
601 mutex_lock(&dev->lock);
604 mutex_unlock(&dev->lock);
608 static int res_check(struct em28xx_fh *fh)
610 return (fh->stream_on);
613 static void res_free(struct em28xx_fh *fh)
615 struct em28xx *dev = fh->dev;
617 mutex_lock(&dev->lock);
620 mutex_unlock(&dev->lock);
625 * return the current saturation, brightness or contrast, mute state
627 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
630 case V4L2_CID_AUDIO_MUTE:
631 ctrl->value = dev->mute;
633 case V4L2_CID_AUDIO_VOLUME:
634 ctrl->value = dev->volume;
643 * mute or set new saturation, brightness or contrast
645 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
648 case V4L2_CID_AUDIO_MUTE:
649 if (ctrl->value != dev->mute) {
650 dev->mute = ctrl->value;
651 return em28xx_audio_analog_set(dev);
654 case V4L2_CID_AUDIO_VOLUME:
655 dev->volume = ctrl->value;
656 return em28xx_audio_analog_set(dev);
662 static int check_dev(struct em28xx *dev)
664 if (dev->state & DEV_DISCONNECTED) {
665 em28xx_errdev("v4l2 ioctl: device not present\n");
669 if (dev->state & DEV_MISCONFIGURED) {
670 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
671 "close and open it again\n");
677 static void get_scale(struct em28xx *dev,
678 unsigned int width, unsigned int height,
679 unsigned int *hscale, unsigned int *vscale)
681 unsigned int maxw = norm_maxw(dev);
682 unsigned int maxh = norm_maxh(dev);
684 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
685 if (*hscale >= 0x4000)
688 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
689 if (*vscale >= 0x4000)
693 /* ------------------------------------------------------------------
694 IOCTL vidioc handling
695 ------------------------------------------------------------------*/
697 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
698 struct v4l2_format *f)
700 struct em28xx_fh *fh = priv;
701 struct em28xx *dev = fh->dev;
703 mutex_lock(&dev->lock);
705 f->fmt.pix.width = dev->width;
706 f->fmt.pix.height = dev->height;
707 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
708 f->fmt.pix.bytesperline = dev->width * 2;
709 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
710 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
712 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
713 f->fmt.pix.field = dev->interlaced ?
714 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
716 mutex_unlock(&dev->lock);
720 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
721 struct v4l2_format *f)
723 struct em28xx_fh *fh = priv;
724 struct em28xx *dev = fh->dev;
725 int width = f->fmt.pix.width;
726 int height = f->fmt.pix.height;
727 unsigned int maxw = norm_maxw(dev);
728 unsigned int maxh = norm_maxh(dev);
729 unsigned int hscale, vscale;
731 /* width must even because of the YUYV format
732 height must be even because of interlacing */
745 mutex_lock(&dev->lock);
747 if (dev->is_em2800) {
748 /* the em2800 can only scale down to 50% */
749 if (height % (maxh / 2))
751 if (width % (maxw / 2))
753 /* according to empiatech support */
754 /* the MaxPacketSize is to small to support */
755 /* framesizes larger than 640x480 @ 30 fps */
756 /* or 640x576 @ 25 fps. As this would cut */
757 /* of a part of the image we prefer */
758 /* 360x576 or 360x480 for now */
759 if (width == maxw && height == maxh)
763 get_scale(dev, width, height, &hscale, &vscale);
765 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
766 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
768 f->fmt.pix.width = width;
769 f->fmt.pix.height = height;
770 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
771 f->fmt.pix.bytesperline = width * 2;
772 f->fmt.pix.sizeimage = width * 2 * height;
773 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
774 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
776 mutex_unlock(&dev->lock);
780 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
781 struct v4l2_format *f)
783 struct em28xx_fh *fh = priv;
784 struct em28xx *dev = fh->dev;
791 vidioc_try_fmt_vid_cap(file, priv, f);
793 mutex_lock(&dev->lock);
795 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
796 em28xx_errdev("%s queue busy\n", __func__);
801 if (dev->stream_on && !fh->stream_on) {
802 em28xx_errdev("%s device in use by another fh\n", __func__);
807 /* set new image size */
808 dev->width = f->fmt.pix.width;
809 dev->height = f->fmt.pix.height;
810 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
812 em28xx_set_alternate(dev);
813 em28xx_resolution_set(dev);
818 mutex_unlock(&dev->lock);
822 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
824 struct em28xx_fh *fh = priv;
825 struct em28xx *dev = fh->dev;
826 struct v4l2_format f;
833 mutex_lock(&dev->lock);
835 mutex_unlock(&dev->lock);
837 /* Adjusts width/height, if needed */
838 f.fmt.pix.width = dev->width;
839 f.fmt.pix.height = dev->height;
840 vidioc_try_fmt_vid_cap(file, priv, &f);
842 mutex_lock(&dev->lock);
844 /* set new image size */
845 dev->width = f.fmt.pix.width;
846 dev->height = f.fmt.pix.height;
847 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
849 em28xx_resolution_set(dev);
850 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
852 mutex_unlock(&dev->lock);
856 static const char *iname[] = {
857 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
858 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
859 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
860 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
861 [EM28XX_VMUX_SVIDEO] = "S-Video",
862 [EM28XX_VMUX_TELEVISION] = "Television",
863 [EM28XX_VMUX_CABLE] = "Cable TV",
864 [EM28XX_VMUX_DVB] = "DVB",
865 [EM28XX_VMUX_DEBUG] = "for debug only",
868 static int vidioc_enum_input(struct file *file, void *priv,
869 struct v4l2_input *i)
871 struct em28xx_fh *fh = priv;
872 struct em28xx *dev = fh->dev;
876 if (n >= MAX_EM28XX_INPUT)
878 if (0 == INPUT(n)->type)
882 i->type = V4L2_INPUT_TYPE_CAMERA;
884 strcpy(i->name, iname[INPUT(n)->type]);
886 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
887 (EM28XX_VMUX_CABLE == INPUT(n)->type))
888 i->type = V4L2_INPUT_TYPE_TUNER;
890 i->std = dev->vdev->tvnorms;
895 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
897 struct em28xx_fh *fh = priv;
898 struct em28xx *dev = fh->dev;
905 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
907 struct em28xx_fh *fh = priv;
908 struct em28xx *dev = fh->dev;
915 if (i >= MAX_EM28XX_INPUT)
917 if (0 == INPUT(i)->type)
920 mutex_lock(&dev->lock);
924 mutex_unlock(&dev->lock);
928 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
930 struct em28xx_fh *fh = priv;
931 struct em28xx *dev = fh->dev;
934 case EM28XX_AMUX_VIDEO:
935 strcpy(a->name, "Television");
937 case EM28XX_AMUX_LINE_IN:
938 strcpy(a->name, "Line In");
940 case EM28XX_AMUX_VIDEO2:
941 strcpy(a->name, "Television alt");
943 case EM28XX_AMUX_PHONE:
944 strcpy(a->name, "Phone");
946 case EM28XX_AMUX_MIC:
947 strcpy(a->name, "Mic");
950 strcpy(a->name, "CD");
952 case EM28XX_AMUX_AUX:
953 strcpy(a->name, "Aux");
955 case EM28XX_AMUX_PCM_OUT:
956 strcpy(a->name, "PCM");
962 a->index = dev->ctl_ainput;
963 a->capability = V4L2_AUDCAP_STEREO;
968 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
970 struct em28xx_fh *fh = priv;
971 struct em28xx *dev = fh->dev;
973 dev->ctl_ainput = INPUT(a->index)->amux;
974 dev->ctl_aoutput = INPUT(a->index)->aout;
978 static int vidioc_queryctrl(struct file *file, void *priv,
979 struct v4l2_queryctrl *qc)
981 struct em28xx_fh *fh = priv;
982 struct em28xx *dev = fh->dev;
991 memset(qc, 0, sizeof(*qc));
995 if (!dev->has_msp34xx) {
996 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
997 if (qc->id && qc->id == em28xx_qctrl[i].id) {
998 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1003 mutex_lock(&dev->lock);
1004 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1005 mutex_unlock(&dev->lock);
1013 static int vidioc_g_ctrl(struct file *file, void *priv,
1014 struct v4l2_control *ctrl)
1016 struct em28xx_fh *fh = priv;
1017 struct em28xx *dev = fh->dev;
1020 rc = check_dev(dev);
1023 mutex_lock(&dev->lock);
1025 if (!dev->has_msp34xx)
1026 rc = em28xx_get_ctrl(dev, ctrl);
1030 if (rc == -EINVAL) {
1031 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1035 mutex_unlock(&dev->lock);
1039 static int vidioc_s_ctrl(struct file *file, void *priv,
1040 struct v4l2_control *ctrl)
1042 struct em28xx_fh *fh = priv;
1043 struct em28xx *dev = fh->dev;
1047 rc = check_dev(dev);
1051 mutex_lock(&dev->lock);
1053 if (dev->has_msp34xx)
1054 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1057 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1058 if (ctrl->id == em28xx_qctrl[i].id) {
1059 if (ctrl->value < em28xx_qctrl[i].minimum ||
1060 ctrl->value > em28xx_qctrl[i].maximum) {
1065 rc = em28xx_set_ctrl(dev, ctrl);
1071 /* Control not found - try to send it to the attached devices */
1073 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1077 mutex_unlock(&dev->lock);
1081 static int vidioc_g_tuner(struct file *file, void *priv,
1082 struct v4l2_tuner *t)
1084 struct em28xx_fh *fh = priv;
1085 struct em28xx *dev = fh->dev;
1088 rc = check_dev(dev);
1095 strcpy(t->name, "Tuner");
1097 mutex_lock(&dev->lock);
1099 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1101 mutex_unlock(&dev->lock);
1105 static int vidioc_s_tuner(struct file *file, void *priv,
1106 struct v4l2_tuner *t)
1108 struct em28xx_fh *fh = priv;
1109 struct em28xx *dev = fh->dev;
1112 rc = check_dev(dev);
1119 mutex_lock(&dev->lock);
1121 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1123 mutex_unlock(&dev->lock);
1127 static int vidioc_g_frequency(struct file *file, void *priv,
1128 struct v4l2_frequency *f)
1130 struct em28xx_fh *fh = priv;
1131 struct em28xx *dev = fh->dev;
1133 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1134 f->frequency = dev->ctl_freq;
1139 static int vidioc_s_frequency(struct file *file, void *priv,
1140 struct v4l2_frequency *f)
1142 struct em28xx_fh *fh = priv;
1143 struct em28xx *dev = fh->dev;
1146 rc = check_dev(dev);
1153 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1155 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1158 mutex_lock(&dev->lock);
1160 dev->ctl_freq = f->frequency;
1161 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1163 mutex_unlock(&dev->lock);
1167 #ifdef CONFIG_VIDEO_ADV_DEBUG
1168 static int em28xx_reg_len(int reg)
1171 case EM28XX_R40_AC97LSB:
1172 case EM28XX_R30_HSCALELOW:
1173 case EM28XX_R32_VSCALELOW:
1180 static int vidioc_g_register(struct file *file, void *priv,
1181 struct v4l2_register *reg)
1183 struct em28xx_fh *fh = priv;
1184 struct em28xx *dev = fh->dev;
1187 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1190 if (em28xx_reg_len(reg->reg) == 1) {
1191 ret = em28xx_read_reg(dev, reg->reg);
1198 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1199 reg->reg, (char *)&val, 2);
1203 reg->val = le64_to_cpu(val);
1209 static int vidioc_s_register(struct file *file, void *priv,
1210 struct v4l2_register *reg)
1212 struct em28xx_fh *fh = priv;
1213 struct em28xx *dev = fh->dev;
1216 buf = cpu_to_le64(reg->val);
1218 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1219 em28xx_reg_len(reg->reg));
1224 static int vidioc_cropcap(struct file *file, void *priv,
1225 struct v4l2_cropcap *cc)
1227 struct em28xx_fh *fh = priv;
1228 struct em28xx *dev = fh->dev;
1230 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1233 cc->bounds.left = 0;
1235 cc->bounds.width = dev->width;
1236 cc->bounds.height = dev->height;
1237 cc->defrect = cc->bounds;
1238 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1239 cc->pixelaspect.denominator = 59;
1244 static int vidioc_streamon(struct file *file, void *priv,
1245 enum v4l2_buf_type type)
1247 struct em28xx_fh *fh = priv;
1248 struct em28xx *dev = fh->dev;
1251 rc = check_dev(dev);
1256 if (unlikely(res_get(fh) < 0))
1259 return (videobuf_streamon(&fh->vb_vidq));
1262 static int vidioc_streamoff(struct file *file, void *priv,
1263 enum v4l2_buf_type type)
1265 struct em28xx_fh *fh = priv;
1266 struct em28xx *dev = fh->dev;
1269 rc = check_dev(dev);
1273 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1275 if (type != fh->type)
1278 videobuf_streamoff(&fh->vb_vidq);
1284 static int vidioc_querycap(struct file *file, void *priv,
1285 struct v4l2_capability *cap)
1287 struct em28xx_fh *fh = priv;
1288 struct em28xx *dev = fh->dev;
1290 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1291 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1292 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
1294 cap->version = EM28XX_VERSION_CODE;
1297 V4L2_CAP_SLICED_VBI_CAPTURE |
1298 V4L2_CAP_VIDEO_CAPTURE |
1300 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1302 if (dev->tuner_type != TUNER_ABSENT)
1303 cap->capabilities |= V4L2_CAP_TUNER;
1308 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1309 struct v4l2_fmtdesc *fmtd)
1311 if (fmtd->index != 0)
1314 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1315 strcpy(fmtd->description, "Packed YUY2");
1316 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1317 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1322 /* Sliced VBI ioctls */
1323 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1324 struct v4l2_format *f)
1326 struct em28xx_fh *fh = priv;
1327 struct em28xx *dev = fh->dev;
1330 rc = check_dev(dev);
1334 mutex_lock(&dev->lock);
1336 f->fmt.sliced.service_set = 0;
1338 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1340 if (f->fmt.sliced.service_set == 0)
1343 mutex_unlock(&dev->lock);
1347 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1348 struct v4l2_format *f)
1350 struct em28xx_fh *fh = priv;
1351 struct em28xx *dev = fh->dev;
1354 rc = check_dev(dev);
1358 mutex_lock(&dev->lock);
1359 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1360 mutex_unlock(&dev->lock);
1362 if (f->fmt.sliced.service_set == 0)
1369 static int vidioc_reqbufs(struct file *file, void *priv,
1370 struct v4l2_requestbuffers *rb)
1372 struct em28xx_fh *fh = priv;
1373 struct em28xx *dev = fh->dev;
1376 rc = check_dev(dev);
1380 return (videobuf_reqbufs(&fh->vb_vidq, rb));
1383 static int vidioc_querybuf(struct file *file, void *priv,
1384 struct v4l2_buffer *b)
1386 struct em28xx_fh *fh = priv;
1387 struct em28xx *dev = fh->dev;
1390 rc = check_dev(dev);
1394 return (videobuf_querybuf(&fh->vb_vidq, b));
1397 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1399 struct em28xx_fh *fh = priv;
1400 struct em28xx *dev = fh->dev;
1403 rc = check_dev(dev);
1407 return (videobuf_qbuf(&fh->vb_vidq, b));
1410 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1412 struct em28xx_fh *fh = priv;
1413 struct em28xx *dev = fh->dev;
1416 rc = check_dev(dev);
1420 return (videobuf_dqbuf(&fh->vb_vidq, b,
1421 file->f_flags & O_NONBLOCK));
1424 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1425 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1427 struct em28xx_fh *fh = priv;
1429 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1434 /* ----------------------------------------------------------- */
1435 /* RADIO ESPECIFIC IOCTLS */
1436 /* ----------------------------------------------------------- */
1438 static int radio_querycap(struct file *file, void *priv,
1439 struct v4l2_capability *cap)
1441 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1443 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1444 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1445 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
1447 cap->version = EM28XX_VERSION_CODE;
1448 cap->capabilities = V4L2_CAP_TUNER;
1452 static int radio_g_tuner(struct file *file, void *priv,
1453 struct v4l2_tuner *t)
1455 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1457 if (unlikely(t->index > 0))
1460 strcpy(t->name, "Radio");
1461 t->type = V4L2_TUNER_RADIO;
1463 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1467 static int radio_enum_input(struct file *file, void *priv,
1468 struct v4l2_input *i)
1472 strcpy(i->name, "Radio");
1473 i->type = V4L2_INPUT_TYPE_TUNER;
1478 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1480 if (unlikely(a->index))
1483 strcpy(a->name, "Radio");
1487 static int radio_s_tuner(struct file *file, void *priv,
1488 struct v4l2_tuner *t)
1490 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1495 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1500 static int radio_s_audio(struct file *file, void *fh,
1501 struct v4l2_audio *a)
1506 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1511 static int radio_queryctrl(struct file *file, void *priv,
1512 struct v4l2_queryctrl *qc)
1516 if (qc->id < V4L2_CID_BASE ||
1517 qc->id >= V4L2_CID_LASTP1)
1520 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1521 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1522 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1531 * em28xx_v4l2_open()
1532 * inits the device and starts isoc transfer
1534 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1536 int minor = iminor(inode);
1537 int errCode = 0, radio = 0;
1538 struct em28xx *h, *dev = NULL;
1539 struct em28xx_fh *fh;
1540 enum v4l2_buf_type fh_type = 0;
1542 mutex_lock(&em28xx_devlist_mutex);
1543 list_for_each_entry(h, &em28xx_devlist, devlist) {
1544 if (h->vdev->minor == minor) {
1546 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1548 if (h->vbi_dev->minor == minor) {
1550 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1553 h->radio_dev->minor == minor) {
1558 mutex_unlock(&em28xx_devlist_mutex);
1562 mutex_lock(&dev->lock);
1564 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1565 minor, v4l2_type_names[fh_type], dev->users);
1568 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1570 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1571 mutex_unlock(&dev->lock);
1577 filp->private_data = fh;
1579 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1580 dev->width = norm_maxw(dev);
1581 dev->height = norm_maxh(dev);
1585 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
1586 em28xx_set_alternate(dev);
1587 em28xx_resolution_set(dev);
1589 /* Needed, since GPIO might have disabled power of
1592 em28xx_config_i2c(dev);
1596 em28xx_videodbg("video_open: setting radio device\n");
1597 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1602 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1603 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1604 sizeof(struct em28xx_buffer), fh);
1606 mutex_unlock(&dev->lock);
1612 * em28xx_realease_resources()
1613 * unregisters the v4l2,i2c and usb devices
1614 * called when the device gets disconected or at module unload
1616 static void em28xx_release_resources(struct em28xx *dev)
1619 /*FIXME: I2C IR should be disconnected */
1621 list_del(&dev->devlist);
1622 if (dev->sbutton_input_dev)
1623 em28xx_deregister_snapshot_button(dev);
1626 em28xx_ir_fini(dev);
1628 if (dev->radio_dev) {
1629 if (-1 != dev->radio_dev->minor)
1630 video_unregister_device(dev->radio_dev);
1632 video_device_release(dev->radio_dev);
1633 dev->radio_dev = NULL;
1636 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1638 if (-1 != dev->vbi_dev->minor)
1639 video_unregister_device(dev->vbi_dev);
1641 video_device_release(dev->vbi_dev);
1642 dev->vbi_dev = NULL;
1645 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1647 if (-1 != dev->vdev->minor)
1648 video_unregister_device(dev->vdev);
1650 video_device_release(dev->vdev);
1653 em28xx_i2c_unregister(dev);
1654 usb_put_dev(dev->udev);
1656 /* Mark device as unused */
1657 em28xx_devused &= ~(1<<dev->devno);
1661 * em28xx_v4l2_close()
1662 * stops streaming and deallocates all resources allocated by the v4l2
1665 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1667 struct em28xx_fh *fh = filp->private_data;
1668 struct em28xx *dev = fh->dev;
1671 em28xx_videodbg("users=%d\n", dev->users);
1677 mutex_lock(&dev->lock);
1679 if (dev->users == 1) {
1680 videobuf_stop(&fh->vb_vidq);
1681 videobuf_mmap_free(&fh->vb_vidq);
1683 /* the device is already disconnect,
1684 free the remaining resources */
1685 if (dev->state & DEV_DISCONNECTED) {
1686 em28xx_release_resources(dev);
1687 mutex_unlock(&dev->lock);
1692 /* do this before setting alternate! */
1693 em28xx_uninit_isoc(dev);
1694 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
1696 /* set alternate 0 */
1698 em28xx_videodbg("setting alternate 0\n");
1699 errCode = usb_set_interface(dev->udev, 0, 0);
1701 em28xx_errdev("cannot change alternate number to "
1702 "0 (error=%i)\n", errCode);
1707 wake_up_interruptible_nr(&dev->open, 1);
1708 mutex_unlock(&dev->lock);
1713 * em28xx_v4l2_read()
1714 * will allocate buffers when called for the first time
1717 em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1720 struct em28xx_fh *fh = filp->private_data;
1721 struct em28xx *dev = fh->dev;
1724 rc = check_dev(dev);
1728 /* FIXME: read() is not prepared to allow changing the video
1729 resolution while streaming. Seems a bug at em28xx_set_fmt
1732 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1733 if (unlikely(res_get(fh)))
1736 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1737 filp->f_flags & O_NONBLOCK);
1743 * em28xx_v4l2_poll()
1744 * will allocate buffers when called for the first time
1746 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
1748 struct em28xx_fh *fh = filp->private_data;
1749 struct em28xx *dev = fh->dev;
1752 rc = check_dev(dev);
1756 if (unlikely(res_get(fh) < 0))
1759 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1762 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1766 * em28xx_v4l2_mmap()
1768 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1770 struct em28xx_fh *fh = filp->private_data;
1771 struct em28xx *dev = fh->dev;
1774 if (unlikely(res_get(fh) < 0))
1777 rc = check_dev(dev);
1781 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1783 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1784 (unsigned long)vma->vm_start,
1785 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1791 static const struct file_operations em28xx_v4l_fops = {
1792 .owner = THIS_MODULE,
1793 .open = em28xx_v4l2_open,
1794 .release = em28xx_v4l2_close,
1795 .read = em28xx_v4l2_read,
1796 .poll = em28xx_v4l2_poll,
1797 .mmap = em28xx_v4l2_mmap,
1798 .ioctl = video_ioctl2,
1799 .llseek = no_llseek,
1800 .compat_ioctl = v4l_compat_ioctl32,
1803 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1804 .vidioc_querycap = vidioc_querycap,
1805 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1806 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1807 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1808 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1809 .vidioc_g_audio = vidioc_g_audio,
1810 .vidioc_s_audio = vidioc_s_audio,
1811 .vidioc_cropcap = vidioc_cropcap,
1813 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1814 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1815 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1817 .vidioc_reqbufs = vidioc_reqbufs,
1818 .vidioc_querybuf = vidioc_querybuf,
1819 .vidioc_qbuf = vidioc_qbuf,
1820 .vidioc_dqbuf = vidioc_dqbuf,
1821 .vidioc_s_std = vidioc_s_std,
1822 .vidioc_enum_input = vidioc_enum_input,
1823 .vidioc_g_input = vidioc_g_input,
1824 .vidioc_s_input = vidioc_s_input,
1825 .vidioc_queryctrl = vidioc_queryctrl,
1826 .vidioc_g_ctrl = vidioc_g_ctrl,
1827 .vidioc_s_ctrl = vidioc_s_ctrl,
1828 .vidioc_streamon = vidioc_streamon,
1829 .vidioc_streamoff = vidioc_streamoff,
1830 .vidioc_g_tuner = vidioc_g_tuner,
1831 .vidioc_s_tuner = vidioc_s_tuner,
1832 .vidioc_g_frequency = vidioc_g_frequency,
1833 .vidioc_s_frequency = vidioc_s_frequency,
1834 #ifdef CONFIG_VIDEO_ADV_DEBUG
1835 .vidioc_g_register = vidioc_g_register,
1836 .vidioc_s_register = vidioc_s_register,
1838 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1839 .vidiocgmbuf = vidiocgmbuf,
1843 static const struct video_device em28xx_video_template = {
1844 .fops = &em28xx_v4l_fops,
1845 .release = video_device_release,
1846 .ioctl_ops = &video_ioctl_ops,
1850 .tvnorms = V4L2_STD_ALL,
1851 .current_norm = V4L2_STD_PAL,
1854 static const struct file_operations radio_fops = {
1855 .owner = THIS_MODULE,
1856 .open = em28xx_v4l2_open,
1857 .release = em28xx_v4l2_close,
1858 .ioctl = video_ioctl2,
1859 .compat_ioctl = v4l_compat_ioctl32,
1860 .llseek = no_llseek,
1863 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1864 .vidioc_querycap = radio_querycap,
1865 .vidioc_g_tuner = radio_g_tuner,
1866 .vidioc_enum_input = radio_enum_input,
1867 .vidioc_g_audio = radio_g_audio,
1868 .vidioc_s_tuner = radio_s_tuner,
1869 .vidioc_s_audio = radio_s_audio,
1870 .vidioc_s_input = radio_s_input,
1871 .vidioc_queryctrl = radio_queryctrl,
1872 .vidioc_g_ctrl = vidioc_g_ctrl,
1873 .vidioc_s_ctrl = vidioc_s_ctrl,
1874 .vidioc_g_frequency = vidioc_g_frequency,
1875 .vidioc_s_frequency = vidioc_s_frequency,
1876 #ifdef CONFIG_VIDEO_ADV_DEBUG
1877 .vidioc_g_register = vidioc_g_register,
1878 .vidioc_s_register = vidioc_s_register,
1882 static struct video_device em28xx_radio_template = {
1883 .name = "em28xx-radio",
1884 .fops = &radio_fops,
1885 .ioctl_ops = &radio_ioctl_ops,
1889 /******************************** usb interface ******************************/
1892 static LIST_HEAD(em28xx_extension_devlist);
1893 static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1895 int em28xx_register_extension(struct em28xx_ops *ops)
1897 struct em28xx *dev = NULL;
1899 mutex_lock(&em28xx_devlist_mutex);
1900 mutex_lock(&em28xx_extension_devlist_lock);
1901 list_add_tail(&ops->next, &em28xx_extension_devlist);
1902 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1906 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1907 mutex_unlock(&em28xx_extension_devlist_lock);
1908 mutex_unlock(&em28xx_devlist_mutex);
1911 EXPORT_SYMBOL(em28xx_register_extension);
1913 void em28xx_unregister_extension(struct em28xx_ops *ops)
1915 struct em28xx *dev = NULL;
1917 mutex_lock(&em28xx_devlist_mutex);
1918 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1923 mutex_lock(&em28xx_extension_devlist_lock);
1924 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1925 list_del(&ops->next);
1926 mutex_unlock(&em28xx_extension_devlist_lock);
1927 mutex_unlock(&em28xx_devlist_mutex);
1929 EXPORT_SYMBOL(em28xx_unregister_extension);
1931 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1932 const struct video_device *template,
1933 const char *type_name)
1935 struct video_device *vfd;
1937 vfd = video_device_alloc();
1942 vfd->parent = &dev->udev->dev;
1943 vfd->release = video_device_release;
1944 vfd->debug = video_debug;
1946 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1947 dev->name, type_name);
1952 static int register_analog_devices(struct em28xx *dev)
1956 /* allocate and fill video video_device struct */
1957 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
1959 em28xx_errdev("cannot allocate video_device.\n");
1963 /* register v4l2 video video_device */
1964 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1965 video_nr[dev->devno]);
1967 em28xx_errdev("unable to register video device (error=%i).\n",
1972 /* Allocate and fill vbi video_device struct */
1973 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
1975 /* register v4l2 vbi video_device */
1976 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1977 vbi_nr[dev->devno]);
1979 em28xx_errdev("unable to register vbi device\n");
1983 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
1984 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
1985 if (!dev->radio_dev) {
1986 em28xx_errdev("cannot allocate video_device.\n");
1989 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1990 radio_nr[dev->devno]);
1992 em28xx_errdev("can't register radio device\n");
1995 em28xx_info("Registered radio device as /dev/radio%d\n",
1996 dev->radio_dev->num);
1999 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2000 dev->vdev->num, dev->vbi_dev->num);
2008 * allocates and inits the device structs, registers i2c bus and v4l device
2010 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
2013 struct em28xx_ops *ops = NULL;
2014 struct em28xx *dev = *devhandle;
2015 int retval = -ENOMEM;
2017 unsigned int maxh, maxw;
2020 mutex_init(&dev->lock);
2021 mutex_init(&dev->ctrl_urb_lock);
2022 spin_lock_init(&dev->slock);
2023 init_waitqueue_head(&dev->open);
2024 init_waitqueue_head(&dev->wait_frame);
2025 init_waitqueue_head(&dev->wait_stream);
2027 dev->em28xx_write_regs = em28xx_write_regs;
2028 dev->em28xx_read_reg = em28xx_read_reg;
2029 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2030 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2031 dev->em28xx_read_reg_req = em28xx_read_reg_req;
2032 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
2034 em28xx_pre_card_setup(dev);
2036 errCode = em28xx_config(dev);
2038 em28xx_errdev("error configuring device\n");
2042 /* register i2c bus */
2043 errCode = em28xx_i2c_register(dev);
2045 em28xx_errdev("%s: em28xx_i2c_register - errCode [%d]!\n",
2050 /* Do board specific init and eeprom reading */
2051 em28xx_card_setup(dev);
2053 /* Configure audio */
2054 errCode = em28xx_audio_setup(dev);
2056 em28xx_errdev("%s: Error while setting audio - errCode [%d]!\n",
2060 /* configure the device */
2061 em28xx_config_i2c(dev);
2063 /* set default norm */
2064 dev->norm = em28xx_video_template.current_norm;
2066 maxw = norm_maxw(dev);
2067 maxh = norm_maxh(dev);
2069 /* set default image size */
2072 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
2077 errCode = em28xx_config(dev);
2079 em28xx_errdev("%s: em28xx_config - errCode [%d]!\n",
2084 /* init video dma queues */
2085 INIT_LIST_HEAD(&dev->vidq.active);
2086 INIT_LIST_HEAD(&dev->vidq.queued);
2089 if (dev->has_msp34xx) {
2090 /* Send a reset to other chips via gpio */
2091 errCode = em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2093 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(1) failed! errCode [%d]\n",
2099 errCode = em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2101 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(2) failed! errCode [%d]\n",
2110 mutex_lock(&em28xx_devlist_mutex);
2111 list_add_tail(&dev->devlist, &em28xx_devlist);
2112 retval = register_analog_devices(dev);
2114 em28xx_release_resources(dev);
2115 mutex_unlock(&em28xx_devlist_mutex);
2116 goto fail_reg_devices;
2119 mutex_lock(&em28xx_extension_devlist_lock);
2120 if (!list_empty(&em28xx_extension_devlist)) {
2121 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2126 mutex_unlock(&em28xx_extension_devlist_lock);
2127 mutex_unlock(&em28xx_devlist_mutex);
2132 mutex_unlock(&dev->lock);
2136 #if defined(CONFIG_MODULES) && defined(MODULE)
2137 static void request_module_async(struct work_struct *work)
2139 struct em28xx *dev = container_of(work,
2140 struct em28xx, request_module_wk);
2142 if (dev->has_audio_class)
2143 request_module("snd-usb-audio");
2144 else if (dev->has_alsa_audio)
2145 request_module("em28xx-alsa");
2148 request_module("em28xx-dvb");
2151 static void request_modules(struct em28xx *dev)
2153 INIT_WORK(&dev->request_module_wk, request_module_async);
2154 schedule_work(&dev->request_module_wk);
2157 #define request_modules(dev)
2158 #endif /* CONFIG_MODULES */
2161 * em28xx_usb_probe()
2162 * checks for supported devices
2164 static int em28xx_usb_probe(struct usb_interface *interface,
2165 const struct usb_device_id *id)
2167 const struct usb_endpoint_descriptor *endpoint;
2168 struct usb_device *udev;
2169 struct usb_interface *uif;
2170 struct em28xx *dev = NULL;
2171 int retval = -ENODEV;
2172 int i, nr, ifnum, isoc_pipe;
2174 udev = usb_get_dev(interface_to_usbdev(interface));
2175 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2177 /* Check to see next free device and mark as used */
2178 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2179 em28xx_devused |= 1<<nr;
2181 /* Don't register audio interfaces */
2182 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2183 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
2184 udev->descriptor.idVendor,
2185 udev->descriptor.idProduct,
2187 interface->altsetting[0].desc.bInterfaceClass);
2189 em28xx_devused &= ~(1<<nr);
2193 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
2194 udev->descriptor.idVendor,
2195 udev->descriptor.idProduct,
2197 interface->altsetting[0].desc.bInterfaceClass);
2199 endpoint = &interface->cur_altsetting->endpoint[0].desc;
2201 /* check if the device has the iso in endpoint at the correct place */
2202 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
2203 USB_ENDPOINT_XFER_ISOC &&
2204 (interface->altsetting[1].endpoint[0].desc.wMaxPacketSize == 940))
2206 /* It's a newer em2874/em2875 device */
2210 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2211 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2212 USB_ENDPOINT_XFER_ISOC) {
2213 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
2214 em28xx_devused &= ~(1<<nr);
2217 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
2218 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
2219 em28xx_devused &= ~(1<<nr);
2224 if (nr >= EM28XX_MAXBOARDS) {
2225 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2227 em28xx_devused &= ~(1<<nr);
2231 /* allocate memory for our device state and initialize it */
2232 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2234 em28xx_err(DRIVER_NAME ": out of memory!\n");
2235 em28xx_devused &= ~(1<<nr);
2239 snprintf(dev->name, 29, "em28xx #%d", nr);
2241 dev->model = id->driver_info;
2244 /* Checks if audio is provided by some interface */
2245 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2246 uif = udev->config->interface[i];
2247 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2248 dev->has_audio_class = 1;
2253 /* compute alternate max packet sizes */
2254 uif = udev->actconfig->interface[0];
2256 dev->num_alt = uif->num_altsetting;
2257 em28xx_info("Alternate settings: %i\n", dev->num_alt);
2258 /* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2259 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2261 if (dev->alt_max_pkt_size == NULL) {
2262 em28xx_errdev("out of memory!\n");
2263 em28xx_devused &= ~(1<<nr);
2268 for (i = 0; i < dev->num_alt ; i++) {
2269 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
2271 dev->alt_max_pkt_size[i] =
2272 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
2273 em28xx_info("Alternate setting %i, max size= %i\n", i,
2274 dev->alt_max_pkt_size[i]);
2277 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
2278 dev->model = card[nr];
2280 /* allocate device struct */
2281 retval = em28xx_init_dev(&dev, udev, nr);
2283 em28xx_devused &= ~(1<<dev->devno);
2289 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
2291 /* save our data pointer in this interface device */
2292 usb_set_intfdata(interface, dev);
2294 request_modules(dev);
2300 * em28xx_usb_disconnect()
2301 * called when the device gets diconencted
2302 * video device will be unregistered on v4l2_close in case it is still open
2304 static void em28xx_usb_disconnect(struct usb_interface *interface)
2307 struct em28xx_ops *ops = NULL;
2309 dev = usb_get_intfdata(interface);
2310 usb_set_intfdata(interface, NULL);
2315 em28xx_info("disconnecting %s\n", dev->vdev->name);
2317 /* wait until all current v4l2 io is finished then deallocate
2319 mutex_lock(&dev->lock);
2321 wake_up_interruptible_all(&dev->open);
2325 ("device /dev/video%d is open! Deregistration and memory "
2326 "deallocation are deferred on close.\n",
2329 dev->state |= DEV_MISCONFIGURED;
2330 em28xx_uninit_isoc(dev);
2331 dev->state |= DEV_DISCONNECTED;
2332 wake_up_interruptible(&dev->wait_frame);
2333 wake_up_interruptible(&dev->wait_stream);
2335 dev->state |= DEV_DISCONNECTED;
2336 em28xx_release_resources(dev);
2338 mutex_unlock(&dev->lock);
2340 mutex_lock(&em28xx_extension_devlist_lock);
2341 if (!list_empty(&em28xx_extension_devlist)) {
2342 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2346 mutex_unlock(&em28xx_extension_devlist_lock);
2349 kfree(dev->alt_max_pkt_size);
2354 static struct usb_driver em28xx_usb_driver = {
2356 .probe = em28xx_usb_probe,
2357 .disconnect = em28xx_usb_disconnect,
2358 .id_table = em28xx_id_table,
2361 static int __init em28xx_module_init(void)
2365 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
2366 (EM28XX_VERSION_CODE >> 16) & 0xff,
2367 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2369 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2370 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2373 /* register this driver with the USB subsystem */
2374 result = usb_register(&em28xx_usb_driver);
2376 em28xx_err(DRIVER_NAME
2377 " usb_register failed. Error number %d.\n", result);
2382 static void __exit em28xx_module_exit(void)
2384 /* deregister this driver with the USB subsystem */
2385 usb_deregister(&em28xx_usb_driver);
2388 module_init(em28xx_module_init);
2389 module_exit(em28xx_module_exit);