2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
5 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
7 Mauro Carvalho Chehab <mchehab@infradead.org>
8 Sascha Sommer <saschasommer@freenet.de>
10 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/bitmap.h>
33 #include <linux/usb.h>
34 #include <linux/i2c.h>
35 #include <linux/version.h>
37 #include <linux/mutex.h>
40 #include <media/v4l2-common.h>
41 #include <media/v4l2-ioctl.h>
42 #include <media/msp3400.h>
43 #include <media/tuner.h>
45 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
46 "Markus Rechberger <mrechberger@gmail.com>, " \
47 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
48 "Sascha Sommer <saschasommer@freenet.de>"
50 #define DRIVER_NAME "em28xx"
51 #define DRIVER_DESC "Empia em28xx based USB video device driver"
52 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
54 #define em28xx_videodbg(fmt, arg...) do {\
56 printk(KERN_INFO "%s %s :"fmt, \
57 dev->name, __func__ , ##arg); } while (0)
59 static unsigned int isoc_debug;
60 module_param(isoc_debug, int, 0644);
61 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
63 #define em28xx_isocdbg(fmt, arg...) \
66 printk(KERN_INFO "%s %s :"fmt, \
67 dev->name, __func__ , ##arg); \
71 MODULE_AUTHOR(DRIVER_AUTHOR);
72 MODULE_DESCRIPTION(DRIVER_DESC);
73 MODULE_LICENSE("GPL");
75 static LIST_HEAD(em28xx_devlist);
77 static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
78 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
82 module_param_array(card, int, NULL, 0444);
83 module_param_array(video_nr, int, NULL, 0444);
84 module_param_array(vbi_nr, int, NULL, 0444);
85 module_param_array(radio_nr, int, NULL, 0444);
86 MODULE_PARM_DESC(card, "card type");
87 MODULE_PARM_DESC(video_nr, "video device numbers");
88 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
89 MODULE_PARM_DESC(radio_nr, "radio device numbers");
91 static unsigned int video_debug;
92 module_param(video_debug, int, 0644);
93 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
95 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
96 static unsigned long em28xx_devused;
98 /* supported controls */
99 /* Common to all boards */
100 static struct v4l2_queryctrl em28xx_qctrl[] = {
102 .id = V4L2_CID_AUDIO_VOLUME,
103 .type = V4L2_CTRL_TYPE_INTEGER,
108 .default_value = 0x1f,
111 .id = V4L2_CID_AUDIO_MUTE,
112 .type = V4L2_CTRL_TYPE_BOOLEAN,
122 static struct usb_driver em28xx_usb_driver;
124 /* ------------------------------------------------------------------
125 DMA and thread functions
126 ------------------------------------------------------------------*/
129 * Announces that a buffer were filled and request the next
131 static inline void buffer_filled(struct em28xx *dev,
132 struct em28xx_dmaqueue *dma_q,
133 struct em28xx_buffer *buf)
135 /* Advice that buffer was filled */
136 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
137 buf->vb.state = VIDEOBUF_DONE;
138 buf->vb.field_count++;
139 do_gettimeofday(&buf->vb.ts);
141 dev->isoc_ctl.buf = NULL;
143 list_del(&buf->vb.queue);
144 wake_up(&buf->vb.done);
148 * Identify the buffer header type and properly handles
150 static void em28xx_copy_video(struct em28xx *dev,
151 struct em28xx_dmaqueue *dma_q,
152 struct em28xx_buffer *buf,
154 unsigned char *outp, unsigned long len)
156 void *fieldstart, *startwrite, *startread;
157 int linesdone, currlinedone, offset, lencopy, remain;
158 int bytesperline = dev->width << 1;
160 if (dma_q->pos + len > buf->vb.size)
161 len = buf->vb.size - dma_q->pos;
163 if (p[0] != 0x88 && p[0] != 0x22) {
164 em28xx_isocdbg("frame is not complete\n");
172 /* Interlaces frame */
176 fieldstart = outp + bytesperline;
178 linesdone = dma_q->pos / bytesperline;
179 currlinedone = dma_q->pos % bytesperline;
180 offset = linesdone * bytesperline * 2 + currlinedone;
181 startwrite = fieldstart + offset;
182 lencopy = bytesperline - currlinedone;
183 lencopy = lencopy > remain ? remain : lencopy;
185 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
186 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
187 ((char *)startwrite + lencopy) -
188 ((char *)outp + buf->vb.size));
189 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
193 memcpy(startwrite, startread, lencopy);
198 startwrite += lencopy + bytesperline;
199 startread += lencopy;
200 if (bytesperline > remain)
203 lencopy = bytesperline;
205 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
206 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
207 ((char *)startwrite + lencopy) -
208 ((char *)outp + buf->vb.size));
209 lencopy = remain = (char *)outp + buf->vb.size -
215 memcpy(startwrite, startread, lencopy);
223 static inline void print_err_status(struct em28xx *dev,
224 int packet, int status)
226 char *errmsg = "Unknown";
230 errmsg = "unlinked synchronuously";
233 errmsg = "unlinked asynchronuously";
236 errmsg = "Buffer error (overrun)";
239 errmsg = "Stalled (device not responding)";
242 errmsg = "Babble (bad cable?)";
245 errmsg = "Bit-stuff error (bad cable?)";
248 errmsg = "CRC/Timeout (could be anything)";
251 errmsg = "Device does not respond";
255 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
257 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
258 packet, status, errmsg);
263 * video-buf generic routine to get the next available buffer
265 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
266 struct em28xx_buffer **buf)
268 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
271 if (list_empty(&dma_q->active)) {
272 em28xx_isocdbg("No active queue to serve\n");
273 dev->isoc_ctl.buf = NULL;
278 /* Get the next buffer */
279 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
281 /* Cleans up buffer - Usefull for testing for frame/URB loss */
282 outp = videobuf_to_vmalloc(&(*buf)->vb);
283 memset(outp, 0, (*buf)->vb.size);
285 dev->isoc_ctl.buf = *buf;
291 * Controls the isoc copy of each urb packet
293 static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
295 struct em28xx_buffer *buf;
296 struct em28xx_dmaqueue *dma_q = urb->context;
297 unsigned char *outp = NULL;
298 int i, len = 0, rc = 1;
304 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
307 if (urb->status < 0) {
308 print_err_status(dev, -1, urb->status);
309 if (urb->status == -ENOENT)
313 buf = dev->isoc_ctl.buf;
315 outp = videobuf_to_vmalloc(&buf->vb);
317 for (i = 0; i < urb->number_of_packets; i++) {
318 int status = urb->iso_frame_desc[i].status;
321 print_err_status(dev, i, status);
322 if (urb->iso_frame_desc[i].status != -EPROTO)
326 len = urb->iso_frame_desc[i].actual_length - 4;
328 if (urb->iso_frame_desc[i].actual_length <= 0) {
329 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
332 if (urb->iso_frame_desc[i].actual_length >
334 em28xx_isocdbg("packet bigger than packet size");
338 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
340 /* FIXME: incomplete buffer checks where removed to make
341 logic simpler. Impacts of those changes should be evaluated
343 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
344 em28xx_isocdbg("VBI HEADER!!!\n");
345 /* FIXME: Should add vbi copy */
348 if (p[0] == 0x22 && p[1] == 0x5a) {
349 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
350 len, (p[2] & 1)? "odd" : "even");
354 buffer_filled(dev, dma_q, buf);
355 get_next_buf(dma_q, &buf);
359 outp = videobuf_to_vmalloc(&buf->vb);
372 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
377 /* ------------------------------------------------------------------
379 ------------------------------------------------------------------*/
382 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
384 struct em28xx_fh *fh = vq->priv_data;
385 struct em28xx *dev = fh->dev;
386 struct v4l2_frequency f;
388 *size = 16 * fh->dev->width * fh->dev->height >> 3;
390 *count = EM28XX_DEF_BUF;
392 if (*count < EM28XX_MIN_BUF)
393 *count = EM28XX_MIN_BUF;
395 /* Ask tuner to go to analog mode */
396 memset(&f, 0, sizeof(f));
397 f.frequency = dev->ctl_freq;
399 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
404 /* This is called *without* dev->slock held; please keep it that way */
405 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
407 struct em28xx_fh *fh = vq->priv_data;
408 struct em28xx *dev = fh->dev;
409 unsigned long flags = 0;
413 /* We used to wait for the buffer to finish here, but this didn't work
414 because, as we were keeping the state as VIDEOBUF_QUEUED,
415 videobuf_queue_cancel marked it as finished for us.
416 (Also, it could wedge forever if the hardware was misconfigured.)
418 This should be safe; by the time we get here, the buffer isn't
419 queued anymore. If we ever start marking the buffers as
420 VIDEOBUF_ACTIVE, it won't be, though.
422 spin_lock_irqsave(&dev->slock, flags);
423 if (dev->isoc_ctl.buf == buf)
424 dev->isoc_ctl.buf = NULL;
425 spin_unlock_irqrestore(&dev->slock, flags);
427 videobuf_vmalloc_free(&buf->vb);
428 buf->vb.state = VIDEOBUF_NEEDS_INIT;
432 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
433 enum v4l2_field field)
435 struct em28xx_fh *fh = vq->priv_data;
436 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
437 struct em28xx *dev = fh->dev;
438 int rc = 0, urb_init = 0;
440 /* FIXME: It assumes depth = 16 */
441 /* The only currently supported format is 16 bits/pixel */
442 buf->vb.size = 16 * dev->width * dev->height >> 3;
444 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
447 buf->vb.width = dev->width;
448 buf->vb.height = dev->height;
449 buf->vb.field = field;
451 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
452 rc = videobuf_iolock(vq, &buf->vb, NULL);
457 if (!dev->isoc_ctl.num_bufs)
461 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
462 EM28XX_NUM_BUFS, dev->max_pkt_size,
468 buf->vb.state = VIDEOBUF_PREPARED;
472 free_buffer(vq, buf);
477 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
479 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
480 struct em28xx_fh *fh = vq->priv_data;
481 struct em28xx *dev = fh->dev;
482 struct em28xx_dmaqueue *vidq = &dev->vidq;
484 buf->vb.state = VIDEOBUF_QUEUED;
485 list_add_tail(&buf->vb.queue, &vidq->active);
489 static void buffer_release(struct videobuf_queue *vq,
490 struct videobuf_buffer *vb)
492 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
493 struct em28xx_fh *fh = vq->priv_data;
494 struct em28xx *dev = (struct em28xx *)fh->dev;
496 em28xx_isocdbg("em28xx: called buffer_release\n");
498 free_buffer(vq, buf);
501 static struct videobuf_queue_ops em28xx_video_qops = {
502 .buf_setup = buffer_setup,
503 .buf_prepare = buffer_prepare,
504 .buf_queue = buffer_queue,
505 .buf_release = buffer_release,
508 /********************* v4l2 interface **************************************/
512 * inits registers with sane defaults
514 static int em28xx_config(struct em28xx *dev)
518 /* Sets I2C speed to 100 KHz */
519 if (!dev->is_em2800) {
520 retval = em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
522 em28xx_errdev("%s: em28xx_write_regs_req failed! retval [%d]\n",
528 /* enable vbi capturing */
530 /* em28xx_write_regs_req(dev, 0x00, 0x0e, "\xC0", 1); audio register */
531 /* em28xx_write_regs_req(dev, 0x00, 0x0f, "\x80", 1); clk register */
532 em28xx_write_regs_req(dev, 0x00, 0x11, "\x51", 1);
534 dev->mute = 1; /* maybe not the right place... */
537 em28xx_outfmt_set_yuv422(dev);
538 em28xx_colorlevels_set_default(dev);
539 em28xx_compression_disable(dev);
545 * em28xx_config_i2c()
546 * configure i2c attached devices
548 static void em28xx_config_i2c(struct em28xx *dev)
550 struct v4l2_routing route;
552 route.input = INPUT(dev->ctl_input)->vmux;
554 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
555 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
556 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
559 static void video_mux(struct em28xx *dev, int index)
561 struct v4l2_routing route;
563 route.input = INPUT(index)->vmux;
565 dev->ctl_input = index;
566 dev->ctl_ainput = INPUT(index)->amux;
568 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
570 if (dev->has_msp34xx) {
571 if (dev->i2s_speed) {
572 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
575 route.input = dev->ctl_ainput;
576 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
577 /* Note: this is msp3400 specific */
578 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
582 em28xx_audio_analog_set(dev);
585 /* Usage lock check functions */
586 static int res_get(struct em28xx_fh *fh)
588 struct em28xx *dev = fh->dev;
591 /* This instance already has stream_on */
598 mutex_lock(&dev->lock);
601 mutex_unlock(&dev->lock);
605 static int res_check(struct em28xx_fh *fh)
607 return (fh->stream_on);
610 static void res_free(struct em28xx_fh *fh)
612 struct em28xx *dev = fh->dev;
614 mutex_lock(&dev->lock);
617 mutex_unlock(&dev->lock);
622 * return the current saturation, brightness or contrast, mute state
624 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
627 case V4L2_CID_AUDIO_MUTE:
628 ctrl->value = dev->mute;
630 case V4L2_CID_AUDIO_VOLUME:
631 ctrl->value = dev->volume;
640 * mute or set new saturation, brightness or contrast
642 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
645 case V4L2_CID_AUDIO_MUTE:
646 if (ctrl->value != dev->mute) {
647 dev->mute = ctrl->value;
648 return em28xx_audio_analog_set(dev);
651 case V4L2_CID_AUDIO_VOLUME:
652 dev->volume = ctrl->value;
653 return em28xx_audio_analog_set(dev);
659 static int check_dev(struct em28xx *dev)
661 if (dev->state & DEV_DISCONNECTED) {
662 em28xx_errdev("v4l2 ioctl: device not present\n");
666 if (dev->state & DEV_MISCONFIGURED) {
667 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
668 "close and open it again\n");
674 static void get_scale(struct em28xx *dev,
675 unsigned int width, unsigned int height,
676 unsigned int *hscale, unsigned int *vscale)
678 unsigned int maxw = norm_maxw(dev);
679 unsigned int maxh = norm_maxh(dev);
681 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
682 if (*hscale >= 0x4000)
685 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
686 if (*vscale >= 0x4000)
690 /* ------------------------------------------------------------------
691 IOCTL vidioc handling
692 ------------------------------------------------------------------*/
694 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
695 struct v4l2_format *f)
697 struct em28xx_fh *fh = priv;
698 struct em28xx *dev = fh->dev;
700 mutex_lock(&dev->lock);
702 f->fmt.pix.width = dev->width;
703 f->fmt.pix.height = dev->height;
704 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
705 f->fmt.pix.bytesperline = dev->width * 2;
706 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
707 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
709 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
710 f->fmt.pix.field = dev->interlaced ?
711 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
713 mutex_unlock(&dev->lock);
717 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
718 struct v4l2_format *f)
720 struct em28xx_fh *fh = priv;
721 struct em28xx *dev = fh->dev;
722 int width = f->fmt.pix.width;
723 int height = f->fmt.pix.height;
724 unsigned int maxw = norm_maxw(dev);
725 unsigned int maxh = norm_maxh(dev);
726 unsigned int hscale, vscale;
728 /* width must even because of the YUYV format
729 height must be even because of interlacing */
742 mutex_lock(&dev->lock);
744 if (dev->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;
773 mutex_unlock(&dev->lock);
777 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
778 struct v4l2_format *f)
780 struct em28xx_fh *fh = priv;
781 struct em28xx *dev = fh->dev;
788 vidioc_try_fmt_vid_cap(file, priv, f);
790 mutex_lock(&dev->lock);
792 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
793 em28xx_errdev("%s queue busy\n", __func__);
798 if (dev->stream_on && !fh->stream_on) {
799 em28xx_errdev("%s device in use by another fh\n", __func__);
804 /* set new image size */
805 dev->width = f->fmt.pix.width;
806 dev->height = f->fmt.pix.height;
807 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
809 em28xx_set_alternate(dev);
810 em28xx_resolution_set(dev);
815 mutex_unlock(&dev->lock);
819 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
821 struct em28xx_fh *fh = priv;
822 struct em28xx *dev = fh->dev;
823 struct v4l2_format f;
830 mutex_lock(&dev->lock);
832 mutex_unlock(&dev->lock);
834 /* Adjusts width/height, if needed */
835 f.fmt.pix.width = dev->width;
836 f.fmt.pix.height = dev->height;
837 vidioc_try_fmt_vid_cap(file, priv, &f);
839 mutex_lock(&dev->lock);
841 /* set new image size */
842 dev->width = f.fmt.pix.width;
843 dev->height = f.fmt.pix.height;
844 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
846 em28xx_resolution_set(dev);
847 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
849 mutex_unlock(&dev->lock);
853 static const char *iname[] = {
854 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
855 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
856 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
857 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
858 [EM28XX_VMUX_SVIDEO] = "S-Video",
859 [EM28XX_VMUX_TELEVISION] = "Television",
860 [EM28XX_VMUX_CABLE] = "Cable TV",
861 [EM28XX_VMUX_DVB] = "DVB",
862 [EM28XX_VMUX_DEBUG] = "for debug only",
865 static int vidioc_enum_input(struct file *file, void *priv,
866 struct v4l2_input *i)
868 struct em28xx_fh *fh = priv;
869 struct em28xx *dev = fh->dev;
873 if (n >= MAX_EM28XX_INPUT)
875 if (0 == INPUT(n)->type)
879 i->type = V4L2_INPUT_TYPE_CAMERA;
881 strcpy(i->name, iname[INPUT(n)->type]);
883 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
884 (EM28XX_VMUX_CABLE == INPUT(n)->type))
885 i->type = V4L2_INPUT_TYPE_TUNER;
887 i->std = dev->vdev->tvnorms;
892 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
894 struct em28xx_fh *fh = priv;
895 struct em28xx *dev = fh->dev;
902 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
904 struct em28xx_fh *fh = priv;
905 struct em28xx *dev = fh->dev;
912 if (i >= MAX_EM28XX_INPUT)
914 if (0 == INPUT(i)->type)
917 mutex_lock(&dev->lock);
921 mutex_unlock(&dev->lock);
925 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
927 struct em28xx_fh *fh = priv;
928 struct em28xx *dev = fh->dev;
929 unsigned int index = a->index;
934 index = dev->ctl_ainput;
937 strcpy(a->name, "Television");
939 strcpy(a->name, "Line In");
941 a->capability = V4L2_AUDCAP_STEREO;
947 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
949 struct em28xx_fh *fh = priv;
950 struct em28xx *dev = fh->dev;
952 if (a->index != dev->ctl_ainput)
958 static int vidioc_queryctrl(struct file *file, void *priv,
959 struct v4l2_queryctrl *qc)
961 struct em28xx_fh *fh = priv;
962 struct em28xx *dev = fh->dev;
971 memset(qc, 0, sizeof(*qc));
975 if (!dev->has_msp34xx) {
976 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
977 if (qc->id && qc->id == em28xx_qctrl[i].id) {
978 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
983 mutex_lock(&dev->lock);
984 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
985 mutex_unlock(&dev->lock);
993 static int vidioc_g_ctrl(struct file *file, void *priv,
994 struct v4l2_control *ctrl)
996 struct em28xx_fh *fh = priv;
997 struct em28xx *dev = fh->dev;
1000 rc = check_dev(dev);
1003 mutex_lock(&dev->lock);
1005 if (!dev->has_msp34xx)
1006 rc = em28xx_get_ctrl(dev, ctrl);
1010 if (rc == -EINVAL) {
1011 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1015 mutex_unlock(&dev->lock);
1019 static int vidioc_s_ctrl(struct file *file, void *priv,
1020 struct v4l2_control *ctrl)
1022 struct em28xx_fh *fh = priv;
1023 struct em28xx *dev = fh->dev;
1027 rc = check_dev(dev);
1031 mutex_lock(&dev->lock);
1033 if (dev->has_msp34xx)
1034 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1037 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1038 if (ctrl->id == em28xx_qctrl[i].id) {
1039 if (ctrl->value < em28xx_qctrl[i].minimum ||
1040 ctrl->value > em28xx_qctrl[i].maximum) {
1045 rc = em28xx_set_ctrl(dev, ctrl);
1051 /* Control not found - try to send it to the attached devices */
1053 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1057 mutex_unlock(&dev->lock);
1061 static int vidioc_g_tuner(struct file *file, void *priv,
1062 struct v4l2_tuner *t)
1064 struct em28xx_fh *fh = priv;
1065 struct em28xx *dev = fh->dev;
1068 rc = check_dev(dev);
1075 strcpy(t->name, "Tuner");
1077 mutex_lock(&dev->lock);
1079 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1081 mutex_unlock(&dev->lock);
1085 static int vidioc_s_tuner(struct file *file, void *priv,
1086 struct v4l2_tuner *t)
1088 struct em28xx_fh *fh = priv;
1089 struct em28xx *dev = fh->dev;
1092 rc = check_dev(dev);
1099 mutex_lock(&dev->lock);
1101 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1103 mutex_unlock(&dev->lock);
1107 static int vidioc_g_frequency(struct file *file, void *priv,
1108 struct v4l2_frequency *f)
1110 struct em28xx_fh *fh = priv;
1111 struct em28xx *dev = fh->dev;
1113 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1114 f->frequency = dev->ctl_freq;
1119 static int vidioc_s_frequency(struct file *file, void *priv,
1120 struct v4l2_frequency *f)
1122 struct em28xx_fh *fh = priv;
1123 struct em28xx *dev = fh->dev;
1126 rc = check_dev(dev);
1133 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1135 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1138 mutex_lock(&dev->lock);
1140 dev->ctl_freq = f->frequency;
1141 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1143 mutex_unlock(&dev->lock);
1147 #ifdef CONFIG_VIDEO_ADV_DEBUG
1148 static int em28xx_reg_len(int reg)
1151 case EM28XX_R40_AC97LSB:
1152 case EM28XX_R30_HSCALELOW:
1153 case EM28XX_R32_VSCALELOW:
1160 static int vidioc_g_register(struct file *file, void *priv,
1161 struct v4l2_register *reg)
1163 struct em28xx_fh *fh = priv;
1164 struct em28xx *dev = fh->dev;
1167 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1170 if (em28xx_reg_len(reg->reg) == 1) {
1171 ret = em28xx_read_reg(dev, reg->reg);
1178 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1179 reg->reg, (char *)&val, 2);
1183 reg->val = le64_to_cpu(val);
1189 static int vidioc_s_register(struct file *file, void *priv,
1190 struct v4l2_register *reg)
1192 struct em28xx_fh *fh = priv;
1193 struct em28xx *dev = fh->dev;
1196 buf = cpu_to_le64(reg->val);
1198 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1199 em28xx_reg_len(reg->reg));
1204 static int vidioc_cropcap(struct file *file, void *priv,
1205 struct v4l2_cropcap *cc)
1207 struct em28xx_fh *fh = priv;
1208 struct em28xx *dev = fh->dev;
1210 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1213 cc->bounds.left = 0;
1215 cc->bounds.width = dev->width;
1216 cc->bounds.height = dev->height;
1217 cc->defrect = cc->bounds;
1218 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1219 cc->pixelaspect.denominator = 59;
1224 static int vidioc_streamon(struct file *file, void *priv,
1225 enum v4l2_buf_type type)
1227 struct em28xx_fh *fh = priv;
1228 struct em28xx *dev = fh->dev;
1231 rc = check_dev(dev);
1236 if (unlikely(res_get(fh) < 0))
1239 return (videobuf_streamon(&fh->vb_vidq));
1242 static int vidioc_streamoff(struct file *file, void *priv,
1243 enum v4l2_buf_type type)
1245 struct em28xx_fh *fh = priv;
1246 struct em28xx *dev = fh->dev;
1249 rc = check_dev(dev);
1253 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1255 if (type != fh->type)
1258 videobuf_streamoff(&fh->vb_vidq);
1264 static int vidioc_querycap(struct file *file, void *priv,
1265 struct v4l2_capability *cap)
1267 struct em28xx_fh *fh = priv;
1268 struct em28xx *dev = fh->dev;
1270 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1271 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1272 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1274 cap->version = EM28XX_VERSION_CODE;
1277 V4L2_CAP_SLICED_VBI_CAPTURE |
1278 V4L2_CAP_VIDEO_CAPTURE |
1280 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1282 if (dev->tuner_type != TUNER_ABSENT)
1283 cap->capabilities |= V4L2_CAP_TUNER;
1288 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1289 struct v4l2_fmtdesc *fmtd)
1291 if (fmtd->index != 0)
1294 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1295 strcpy(fmtd->description, "Packed YUY2");
1296 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1297 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1302 /* Sliced VBI ioctls */
1303 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1304 struct v4l2_format *f)
1306 struct em28xx_fh *fh = priv;
1307 struct em28xx *dev = fh->dev;
1310 rc = check_dev(dev);
1314 mutex_lock(&dev->lock);
1316 f->fmt.sliced.service_set = 0;
1318 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1320 if (f->fmt.sliced.service_set == 0)
1323 mutex_unlock(&dev->lock);
1327 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1328 struct v4l2_format *f)
1330 struct em28xx_fh *fh = priv;
1331 struct em28xx *dev = fh->dev;
1334 rc = check_dev(dev);
1338 mutex_lock(&dev->lock);
1339 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1340 mutex_unlock(&dev->lock);
1342 if (f->fmt.sliced.service_set == 0)
1349 static int vidioc_reqbufs(struct file *file, void *priv,
1350 struct v4l2_requestbuffers *rb)
1352 struct em28xx_fh *fh = priv;
1353 struct em28xx *dev = fh->dev;
1356 rc = check_dev(dev);
1360 return (videobuf_reqbufs(&fh->vb_vidq, rb));
1363 static int vidioc_querybuf(struct file *file, void *priv,
1364 struct v4l2_buffer *b)
1366 struct em28xx_fh *fh = priv;
1367 struct em28xx *dev = fh->dev;
1370 rc = check_dev(dev);
1374 return (videobuf_querybuf(&fh->vb_vidq, b));
1377 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1379 struct em28xx_fh *fh = priv;
1380 struct em28xx *dev = fh->dev;
1383 rc = check_dev(dev);
1387 return (videobuf_qbuf(&fh->vb_vidq, b));
1390 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1392 struct em28xx_fh *fh = priv;
1393 struct em28xx *dev = fh->dev;
1396 rc = check_dev(dev);
1400 return (videobuf_dqbuf(&fh->vb_vidq, b,
1401 file->f_flags & O_NONBLOCK));
1404 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1405 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1407 struct em28xx_fh *fh = priv;
1409 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1414 /* ----------------------------------------------------------- */
1415 /* RADIO ESPECIFIC IOCTLS */
1416 /* ----------------------------------------------------------- */
1418 static int radio_querycap(struct file *file, void *priv,
1419 struct v4l2_capability *cap)
1421 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1423 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1424 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1425 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1427 cap->version = EM28XX_VERSION_CODE;
1428 cap->capabilities = V4L2_CAP_TUNER;
1432 static int radio_g_tuner(struct file *file, void *priv,
1433 struct v4l2_tuner *t)
1435 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1437 if (unlikely(t->index > 0))
1440 strcpy(t->name, "Radio");
1441 t->type = V4L2_TUNER_RADIO;
1443 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1447 static int radio_enum_input(struct file *file, void *priv,
1448 struct v4l2_input *i)
1452 strcpy(i->name, "Radio");
1453 i->type = V4L2_INPUT_TYPE_TUNER;
1458 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1460 if (unlikely(a->index))
1463 strcpy(a->name, "Radio");
1467 static int radio_s_tuner(struct file *file, void *priv,
1468 struct v4l2_tuner *t)
1470 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1475 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1480 static int radio_s_audio(struct file *file, void *fh,
1481 struct v4l2_audio *a)
1486 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1491 static int radio_queryctrl(struct file *file, void *priv,
1492 struct v4l2_queryctrl *qc)
1496 if (qc->id < V4L2_CID_BASE ||
1497 qc->id >= V4L2_CID_LASTP1)
1500 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1501 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1502 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1511 * em28xx_v4l2_open()
1512 * inits the device and starts isoc transfer
1514 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1516 int minor = iminor(inode);
1517 int errCode = 0, radio = 0;
1518 struct em28xx *h, *dev = NULL;
1519 struct em28xx_fh *fh;
1520 enum v4l2_buf_type fh_type = 0;
1523 list_for_each_entry(h, &em28xx_devlist, devlist) {
1524 if (h->vdev->minor == minor) {
1526 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1528 if (h->vbi_dev->minor == minor) {
1530 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1533 h->radio_dev->minor == minor) {
1543 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1544 minor, v4l2_type_names[fh_type], dev->users);
1547 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1549 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1553 mutex_lock(&dev->lock);
1557 filp->private_data = fh;
1559 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1560 dev->width = norm_maxw(dev);
1561 dev->height = norm_maxh(dev);
1565 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
1566 em28xx_set_alternate(dev);
1567 em28xx_resolution_set(dev);
1569 /* Needed, since GPIO might have disabled power of
1572 em28xx_config_i2c(dev);
1576 em28xx_videodbg("video_open: setting radio device\n");
1577 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1582 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1583 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1584 sizeof(struct em28xx_buffer), fh);
1586 mutex_unlock(&dev->lock);
1593 * em28xx_realease_resources()
1594 * unregisters the v4l2,i2c and usb devices
1595 * called when the device gets disconected or at module unload
1597 static void em28xx_release_resources(struct em28xx *dev)
1600 /*FIXME: I2C IR should be disconnected */
1602 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1603 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1604 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
1605 list_del(&dev->devlist);
1606 if (dev->sbutton_input_dev)
1607 em28xx_deregister_snapshot_button(dev);
1608 if (dev->radio_dev) {
1609 if (-1 != dev->radio_dev->minor)
1610 video_unregister_device(dev->radio_dev);
1612 video_device_release(dev->radio_dev);
1613 dev->radio_dev = NULL;
1616 if (-1 != dev->vbi_dev->minor)
1617 video_unregister_device(dev->vbi_dev);
1619 video_device_release(dev->vbi_dev);
1620 dev->vbi_dev = NULL;
1623 if (-1 != dev->vdev->minor)
1624 video_unregister_device(dev->vdev);
1626 video_device_release(dev->vdev);
1629 em28xx_i2c_unregister(dev);
1630 usb_put_dev(dev->udev);
1632 /* Mark device as unused */
1633 em28xx_devused &= ~(1<<dev->devno);
1637 * em28xx_v4l2_close()
1638 * stops streaming and deallocates all resources allocated by the v4l2
1641 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1643 struct em28xx_fh *fh = filp->private_data;
1644 struct em28xx *dev = fh->dev;
1647 em28xx_videodbg("users=%d\n", dev->users);
1653 mutex_lock(&dev->lock);
1655 if (dev->users == 1) {
1656 videobuf_stop(&fh->vb_vidq);
1657 videobuf_mmap_free(&fh->vb_vidq);
1659 /* the device is already disconnect,
1660 free the remaining resources */
1661 if (dev->state & DEV_DISCONNECTED) {
1662 em28xx_release_resources(dev);
1663 mutex_unlock(&dev->lock);
1668 /* do this before setting alternate! */
1669 em28xx_uninit_isoc(dev);
1670 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
1672 /* set alternate 0 */
1674 em28xx_videodbg("setting alternate 0\n");
1675 errCode = usb_set_interface(dev->udev, 0, 0);
1677 em28xx_errdev("cannot change alternate number to "
1678 "0 (error=%i)\n", errCode);
1683 wake_up_interruptible_nr(&dev->open, 1);
1684 mutex_unlock(&dev->lock);
1689 * em28xx_v4l2_read()
1690 * will allocate buffers when called for the first time
1693 em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1696 struct em28xx_fh *fh = filp->private_data;
1697 struct em28xx *dev = fh->dev;
1700 rc = check_dev(dev);
1704 /* FIXME: read() is not prepared to allow changing the video
1705 resolution while streaming. Seems a bug at em28xx_set_fmt
1708 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1709 if (unlikely(res_get(fh)))
1712 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1713 filp->f_flags & O_NONBLOCK);
1719 * em28xx_v4l2_poll()
1720 * will allocate buffers when called for the first time
1722 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
1724 struct em28xx_fh *fh = filp->private_data;
1725 struct em28xx *dev = fh->dev;
1728 rc = check_dev(dev);
1732 if (unlikely(res_get(fh) < 0))
1735 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1738 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1742 * em28xx_v4l2_mmap()
1744 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1746 struct em28xx_fh *fh = filp->private_data;
1747 struct em28xx *dev = fh->dev;
1750 if (unlikely(res_get(fh) < 0))
1753 rc = check_dev(dev);
1757 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1759 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1760 (unsigned long)vma->vm_start,
1761 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1767 static const struct file_operations em28xx_v4l_fops = {
1768 .owner = THIS_MODULE,
1769 .open = em28xx_v4l2_open,
1770 .release = em28xx_v4l2_close,
1771 .read = em28xx_v4l2_read,
1772 .poll = em28xx_v4l2_poll,
1773 .mmap = em28xx_v4l2_mmap,
1774 .ioctl = video_ioctl2,
1775 .llseek = no_llseek,
1776 .compat_ioctl = v4l_compat_ioctl32,
1779 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1780 .vidioc_querycap = vidioc_querycap,
1781 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1782 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1783 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1784 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1785 .vidioc_g_audio = vidioc_g_audio,
1786 .vidioc_s_audio = vidioc_s_audio,
1787 .vidioc_cropcap = vidioc_cropcap,
1789 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1790 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1791 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1793 .vidioc_reqbufs = vidioc_reqbufs,
1794 .vidioc_querybuf = vidioc_querybuf,
1795 .vidioc_qbuf = vidioc_qbuf,
1796 .vidioc_dqbuf = vidioc_dqbuf,
1797 .vidioc_s_std = vidioc_s_std,
1798 .vidioc_enum_input = vidioc_enum_input,
1799 .vidioc_g_input = vidioc_g_input,
1800 .vidioc_s_input = vidioc_s_input,
1801 .vidioc_queryctrl = vidioc_queryctrl,
1802 .vidioc_g_ctrl = vidioc_g_ctrl,
1803 .vidioc_s_ctrl = vidioc_s_ctrl,
1804 .vidioc_streamon = vidioc_streamon,
1805 .vidioc_streamoff = vidioc_streamoff,
1806 .vidioc_g_tuner = vidioc_g_tuner,
1807 .vidioc_s_tuner = vidioc_s_tuner,
1808 .vidioc_g_frequency = vidioc_g_frequency,
1809 .vidioc_s_frequency = vidioc_s_frequency,
1810 #ifdef CONFIG_VIDEO_ADV_DEBUG
1811 .vidioc_g_register = vidioc_g_register,
1812 .vidioc_s_register = vidioc_s_register,
1814 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1815 .vidiocgmbuf = vidiocgmbuf,
1819 static const struct video_device em28xx_video_template = {
1820 .fops = &em28xx_v4l_fops,
1821 .release = video_device_release,
1822 .ioctl_ops = &video_ioctl_ops,
1826 .tvnorms = V4L2_STD_ALL,
1827 .current_norm = V4L2_STD_PAL,
1830 static const struct file_operations radio_fops = {
1831 .owner = THIS_MODULE,
1832 .open = em28xx_v4l2_open,
1833 .release = em28xx_v4l2_close,
1834 .ioctl = video_ioctl2,
1835 .compat_ioctl = v4l_compat_ioctl32,
1836 .llseek = no_llseek,
1839 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1840 .vidioc_querycap = radio_querycap,
1841 .vidioc_g_tuner = radio_g_tuner,
1842 .vidioc_enum_input = radio_enum_input,
1843 .vidioc_g_audio = radio_g_audio,
1844 .vidioc_s_tuner = radio_s_tuner,
1845 .vidioc_s_audio = radio_s_audio,
1846 .vidioc_s_input = radio_s_input,
1847 .vidioc_queryctrl = radio_queryctrl,
1848 .vidioc_g_ctrl = vidioc_g_ctrl,
1849 .vidioc_s_ctrl = vidioc_s_ctrl,
1850 .vidioc_g_frequency = vidioc_g_frequency,
1851 .vidioc_s_frequency = vidioc_s_frequency,
1852 #ifdef CONFIG_VIDEO_ADV_DEBUG
1853 .vidioc_g_register = vidioc_g_register,
1854 .vidioc_s_register = vidioc_s_register,
1858 static struct video_device em28xx_radio_template = {
1859 .name = "em28xx-radio",
1860 .fops = &radio_fops,
1861 .ioctl_ops = &radio_ioctl_ops,
1865 /******************************** usb interface ******************************/
1868 static LIST_HEAD(em28xx_extension_devlist);
1869 static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1871 int em28xx_register_extension(struct em28xx_ops *ops)
1873 struct em28xx *dev = NULL;
1875 mutex_lock(&em28xx_extension_devlist_lock);
1876 list_add_tail(&ops->next, &em28xx_extension_devlist);
1877 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1881 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1882 mutex_unlock(&em28xx_extension_devlist_lock);
1885 EXPORT_SYMBOL(em28xx_register_extension);
1887 void em28xx_unregister_extension(struct em28xx_ops *ops)
1889 struct em28xx *dev = NULL;
1891 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1896 mutex_lock(&em28xx_extension_devlist_lock);
1897 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1898 list_del(&ops->next);
1899 mutex_unlock(&em28xx_extension_devlist_lock);
1901 EXPORT_SYMBOL(em28xx_unregister_extension);
1903 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1904 const struct video_device *template,
1905 const char *type_name)
1907 struct video_device *vfd;
1909 vfd = video_device_alloc();
1914 vfd->parent = &dev->udev->dev;
1915 vfd->release = video_device_release;
1916 vfd->debug = video_debug;
1918 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1919 dev->name, type_name);
1927 * allocates and inits the device structs, registers i2c bus and v4l device
1929 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
1932 struct em28xx_ops *ops = NULL;
1933 struct em28xx *dev = *devhandle;
1934 int retval = -ENOMEM;
1936 unsigned int maxh, maxw;
1939 mutex_init(&dev->lock);
1940 spin_lock_init(&dev->slock);
1941 init_waitqueue_head(&dev->open);
1942 init_waitqueue_head(&dev->wait_frame);
1943 init_waitqueue_head(&dev->wait_stream);
1945 dev->em28xx_write_regs = em28xx_write_regs;
1946 dev->em28xx_read_reg = em28xx_read_reg;
1947 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1948 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1949 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1950 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
1952 em28xx_pre_card_setup(dev);
1954 errCode = em28xx_config(dev);
1956 em28xx_errdev("error configuring device\n");
1957 em28xx_devused &= ~(1<<dev->devno);
1962 /* register i2c bus */
1963 errCode = em28xx_i2c_register(dev);
1965 em28xx_errdev("%s: em28xx_i2c_register - errCode [%d]!\n",
1970 /* Do board specific init and eeprom reading */
1971 em28xx_card_setup(dev);
1973 /* Configure audio */
1974 errCode = em28xx_audio_analog_set(dev);
1976 em28xx_errdev("%s: em28xx_audio_analog_set - errCode [%d]!\n",
1981 /* configure the device */
1982 em28xx_config_i2c(dev);
1984 /* set default norm */
1985 dev->norm = em28xx_video_template.current_norm;
1987 maxw = norm_maxw(dev);
1988 maxh = norm_maxh(dev);
1990 /* set default image size */
1993 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1998 errCode = em28xx_config(dev);
2000 em28xx_errdev("%s: em28xx_config - errCode [%d]!\n",
2005 list_add_tail(&dev->devlist, &em28xx_devlist);
2007 /* allocate and fill video video_device struct */
2008 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2009 if (NULL == dev->vdev) {
2010 em28xx_errdev("cannot allocate video_device.\n");
2014 /* register v4l2 video video_device */
2015 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2016 video_nr[dev->devno]);
2018 em28xx_errdev("unable to register video device (error=%i).\n",
2023 /* Allocate and fill vbi video_device struct */
2024 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2025 /* register v4l2 vbi video_device */
2026 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2027 vbi_nr[dev->devno]) < 0) {
2028 em28xx_errdev("unable to register vbi device\n");
2033 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2034 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
2035 if (NULL == dev->radio_dev) {
2036 em28xx_errdev("cannot allocate video_device.\n");
2039 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2040 radio_nr[dev->devno]);
2042 em28xx_errdev("can't register radio device\n");
2045 em28xx_info("Registered radio device as /dev/radio%d\n",
2046 dev->radio_dev->minor & 0x1f);
2049 /* init video dma queues */
2050 INIT_LIST_HEAD(&dev->vidq.active);
2051 INIT_LIST_HEAD(&dev->vidq.queued);
2054 if (dev->has_msp34xx) {
2055 /* Send a reset to other chips via gpio */
2056 errCode = em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2058 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(1) failed! errCode [%d]\n",
2064 errCode = em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2066 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(2) failed! errCode [%d]\n",
2075 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2076 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2077 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
2079 mutex_lock(&em28xx_extension_devlist_lock);
2080 if (!list_empty(&em28xx_extension_devlist)) {
2081 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2086 mutex_unlock(&em28xx_extension_devlist_lock);
2091 em28xx_release_resources(dev);
2092 mutex_unlock(&dev->lock);
2097 #if defined(CONFIG_MODULES) && defined(MODULE)
2098 static void request_module_async(struct work_struct *work)
2100 struct em28xx *dev = container_of(work,
2101 struct em28xx, request_module_wk);
2103 if (dev->has_audio_class)
2104 request_module("snd-usb-audio");
2106 request_module("em28xx-alsa");
2109 request_module("em28xx-dvb");
2112 static void request_modules(struct em28xx *dev)
2114 INIT_WORK(&dev->request_module_wk, request_module_async);
2115 schedule_work(&dev->request_module_wk);
2118 #define request_modules(dev)
2119 #endif /* CONFIG_MODULES */
2122 * em28xx_usb_probe()
2123 * checks for supported devices
2125 static int em28xx_usb_probe(struct usb_interface *interface,
2126 const struct usb_device_id *id)
2128 const struct usb_endpoint_descriptor *endpoint;
2129 struct usb_device *udev;
2130 struct usb_interface *uif;
2131 struct em28xx *dev = NULL;
2132 int retval = -ENODEV;
2135 udev = usb_get_dev(interface_to_usbdev(interface));
2136 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2138 /* Check to see next free device and mark as used */
2139 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2140 em28xx_devused |= 1<<nr;
2142 /* Don't register audio interfaces */
2143 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2144 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
2145 udev->descriptor.idVendor,
2146 udev->descriptor.idProduct,
2148 interface->altsetting[0].desc.bInterfaceClass);
2150 em28xx_devused &= ~(1<<nr);
2154 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
2155 udev->descriptor.idVendor,
2156 udev->descriptor.idProduct,
2158 interface->altsetting[0].desc.bInterfaceClass);
2160 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2162 /* check if the device has the iso in endpoint at the correct place */
2163 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2164 USB_ENDPOINT_XFER_ISOC) {
2165 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
2166 em28xx_devused &= ~(1<<nr);
2169 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
2170 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
2171 em28xx_devused &= ~(1<<nr);
2175 if (nr >= EM28XX_MAXBOARDS) {
2176 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2178 em28xx_devused &= ~(1<<nr);
2182 /* allocate memory for our device state and initialize it */
2183 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2185 em28xx_err(DRIVER_NAME ": out of memory!\n");
2186 em28xx_devused &= ~(1<<nr);
2190 snprintf(dev->name, 29, "em28xx #%d", nr);
2192 dev->model = id->driver_info;
2195 /* Checks if audio is provided by some interface */
2196 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2197 uif = udev->config->interface[i];
2198 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2199 dev->has_audio_class = 1;
2204 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2205 dev->has_audio_class ? "Has" : "Doesn't have");
2207 /* compute alternate max packet sizes */
2208 uif = udev->actconfig->interface[0];
2210 dev->num_alt = uif->num_altsetting;
2211 em28xx_info("Alternate settings: %i\n", dev->num_alt);
2212 /* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2213 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2215 if (dev->alt_max_pkt_size == NULL) {
2216 em28xx_errdev("out of memory!\n");
2217 em28xx_devused &= ~(1<<nr);
2222 for (i = 0; i < dev->num_alt ; i++) {
2223 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2225 dev->alt_max_pkt_size[i] =
2226 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
2227 em28xx_info("Alternate setting %i, max size= %i\n", i,
2228 dev->alt_max_pkt_size[i]);
2231 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
2232 dev->model = card[nr];
2234 /* allocate device struct */
2235 retval = em28xx_init_dev(&dev, udev, nr);
2239 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
2241 /* save our data pointer in this interface device */
2242 usb_set_intfdata(interface, dev);
2244 request_modules(dev);
2250 * em28xx_usb_disconnect()
2251 * called when the device gets diconencted
2252 * video device will be unregistered on v4l2_close in case it is still open
2254 static void em28xx_usb_disconnect(struct usb_interface *interface)
2257 struct em28xx_ops *ops = NULL;
2259 dev = usb_get_intfdata(interface);
2260 usb_set_intfdata(interface, NULL);
2265 em28xx_info("disconnecting %s\n", dev->vdev->name);
2267 /* wait until all current v4l2 io is finished then deallocate
2269 mutex_lock(&dev->lock);
2271 wake_up_interruptible_all(&dev->open);
2275 ("device /dev/video%d is open! Deregistration and memory "
2276 "deallocation are deferred on close.\n",
2277 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2279 dev->state |= DEV_MISCONFIGURED;
2280 em28xx_uninit_isoc(dev);
2281 dev->state |= DEV_DISCONNECTED;
2282 wake_up_interruptible(&dev->wait_frame);
2283 wake_up_interruptible(&dev->wait_stream);
2285 dev->state |= DEV_DISCONNECTED;
2286 em28xx_release_resources(dev);
2288 mutex_unlock(&dev->lock);
2290 mutex_lock(&em28xx_extension_devlist_lock);
2291 if (!list_empty(&em28xx_extension_devlist)) {
2292 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2296 mutex_unlock(&em28xx_extension_devlist_lock);
2299 kfree(dev->alt_max_pkt_size);
2304 static struct usb_driver em28xx_usb_driver = {
2306 .probe = em28xx_usb_probe,
2307 .disconnect = em28xx_usb_disconnect,
2308 .id_table = em28xx_id_table,
2311 static int __init em28xx_module_init(void)
2315 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
2316 (EM28XX_VERSION_CODE >> 16) & 0xff,
2317 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2319 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2320 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2323 /* register this driver with the USB subsystem */
2324 result = usb_register(&em28xx_usb_driver);
2326 em28xx_err(DRIVER_NAME
2327 " usb_register failed. Error number %d.\n", result);
2332 static void __exit em28xx_module_exit(void)
2334 /* deregister this driver with the USB subsystem */
2335 usb_deregister(&em28xx_usb_driver);
2338 module_init(em28xx_module_init);
2339 module_exit(em28xx_module_exit);