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/msp3400.h>
42 #include <media/tuner.h>
44 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
45 "Markus Rechberger <mrechberger@gmail.com>, " \
46 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
47 "Sascha Sommer <saschasommer@freenet.de>"
49 #define DRIVER_NAME "em28xx"
50 #define DRIVER_DESC "Empia em28xx based USB video device driver"
51 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
53 #define em28xx_videodbg(fmt, arg...) do {\
55 printk(KERN_INFO "%s %s :"fmt, \
56 dev->name, __func__ , ##arg); } while (0)
58 static unsigned int isoc_debug;
59 module_param(isoc_debug, int, 0644);
60 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
62 #define em28xx_isocdbg(fmt, arg...) \
65 printk(KERN_INFO "%s %s :"fmt, \
66 dev->name, __func__ , ##arg); \
70 MODULE_AUTHOR(DRIVER_AUTHOR);
71 MODULE_DESCRIPTION(DRIVER_DESC);
72 MODULE_LICENSE("GPL");
74 static LIST_HEAD(em28xx_devlist);
76 static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
77 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
78 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81 module_param_array(card, int, NULL, 0444);
82 module_param_array(video_nr, int, NULL, 0444);
83 module_param_array(vbi_nr, int, NULL, 0444);
84 module_param_array(radio_nr, int, NULL, 0444);
85 MODULE_PARM_DESC(card, "card type");
86 MODULE_PARM_DESC(video_nr, "video device numbers");
87 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
88 MODULE_PARM_DESC(radio_nr, "radio device numbers");
90 static unsigned int video_debug;
91 module_param(video_debug, int, 0644);
92 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
94 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
95 static unsigned long em28xx_devused;
97 /* supported controls */
98 /* Common to all boards */
99 static struct v4l2_queryctrl em28xx_qctrl[] = {
101 .id = V4L2_CID_AUDIO_VOLUME,
102 .type = V4L2_CTRL_TYPE_INTEGER,
107 .default_value = 0x1f,
110 .id = V4L2_CID_AUDIO_MUTE,
111 .type = V4L2_CTRL_TYPE_BOOLEAN,
121 static struct usb_driver em28xx_usb_driver;
123 /* ------------------------------------------------------------------
124 DMA and thread functions
125 ------------------------------------------------------------------*/
128 * Announces that a buffer were filled and request the next
130 static inline void buffer_filled(struct em28xx *dev,
131 struct em28xx_dmaqueue *dma_q,
132 struct em28xx_buffer *buf)
134 /* Advice that buffer was filled */
135 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
136 buf->vb.state = VIDEOBUF_DONE;
137 buf->vb.field_count++;
138 do_gettimeofday(&buf->vb.ts);
140 dev->isoc_ctl.buf = NULL;
142 list_del(&buf->vb.queue);
143 wake_up(&buf->vb.done);
147 * Identify the buffer header type and properly handles
149 static void em28xx_copy_video(struct em28xx *dev,
150 struct em28xx_dmaqueue *dma_q,
151 struct em28xx_buffer *buf,
153 unsigned char *outp, unsigned long len)
155 void *fieldstart, *startwrite, *startread;
156 int linesdone, currlinedone, offset, lencopy, remain;
157 int bytesperline = dev->width << 1;
159 if (dma_q->pos + len > buf->vb.size)
160 len = buf->vb.size - dma_q->pos;
162 if (p[0] != 0x88 && p[0] != 0x22) {
163 em28xx_isocdbg("frame is not complete\n");
171 /* Interlaces frame */
175 fieldstart = outp + bytesperline;
177 linesdone = dma_q->pos / bytesperline;
178 currlinedone = dma_q->pos % bytesperline;
179 offset = linesdone * bytesperline * 2 + currlinedone;
180 startwrite = fieldstart + offset;
181 lencopy = bytesperline - currlinedone;
182 lencopy = lencopy > remain ? remain : lencopy;
184 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
185 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
186 ((char *)startwrite + lencopy) -
187 ((char *)outp + buf->vb.size));
188 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
192 memcpy(startwrite, startread, lencopy);
197 startwrite += lencopy + bytesperline;
198 startread += lencopy;
199 if (bytesperline > remain)
202 lencopy = bytesperline;
204 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
205 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
206 ((char *)startwrite + lencopy) -
207 ((char *)outp + buf->vb.size));
208 lencopy = remain = (char *)outp + buf->vb.size -
214 memcpy(startwrite, startread, lencopy);
222 static inline void print_err_status(struct em28xx *dev,
223 int packet, int status)
225 char *errmsg = "Unknown";
229 errmsg = "unlinked synchronuously";
232 errmsg = "unlinked asynchronuously";
235 errmsg = "Buffer error (overrun)";
238 errmsg = "Stalled (device not responding)";
241 errmsg = "Babble (bad cable?)";
244 errmsg = "Bit-stuff error (bad cable?)";
247 errmsg = "CRC/Timeout (could be anything)";
250 errmsg = "Device does not respond";
254 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
256 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
257 packet, status, errmsg);
262 * video-buf generic routine to get the next available buffer
264 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
265 struct em28xx_buffer **buf)
267 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
270 if (list_empty(&dma_q->active)) {
271 em28xx_isocdbg("No active queue to serve\n");
272 dev->isoc_ctl.buf = NULL;
277 /* Get the next buffer */
278 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
280 /* Cleans up buffer - Usefull for testing for frame/URB loss */
281 outp = videobuf_to_vmalloc(&(*buf)->vb);
282 memset(outp, 0, (*buf)->vb.size);
284 dev->isoc_ctl.buf = *buf;
290 * Controls the isoc copy of each urb packet
292 static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
294 struct em28xx_buffer *buf;
295 struct em28xx_dmaqueue *dma_q = urb->context;
296 unsigned char *outp = NULL;
297 int i, len = 0, rc = 1;
303 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
306 if (urb->status < 0) {
307 print_err_status(dev, -1, urb->status);
308 if (urb->status == -ENOENT)
312 buf = dev->isoc_ctl.buf;
314 outp = videobuf_to_vmalloc(&buf->vb);
316 for (i = 0; i < urb->number_of_packets; i++) {
317 int status = urb->iso_frame_desc[i].status;
320 print_err_status(dev, i, status);
321 if (urb->iso_frame_desc[i].status != -EPROTO)
325 len = urb->iso_frame_desc[i].actual_length - 4;
327 if (urb->iso_frame_desc[i].actual_length <= 0) {
328 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
331 if (urb->iso_frame_desc[i].actual_length >
333 em28xx_isocdbg("packet bigger than packet size");
337 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
339 /* FIXME: incomplete buffer checks where removed to make
340 logic simpler. Impacts of those changes should be evaluated
342 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
343 em28xx_isocdbg("VBI HEADER!!!\n");
344 /* FIXME: Should add vbi copy */
347 if (p[0] == 0x22 && p[1] == 0x5a) {
348 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
349 len, (p[2] & 1)? "odd" : "even");
353 buffer_filled(dev, dma_q, buf);
354 get_next_buf(dma_q, &buf);
358 outp = videobuf_to_vmalloc(&buf->vb);
371 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
376 /* ------------------------------------------------------------------
378 ------------------------------------------------------------------*/
381 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
383 struct em28xx_fh *fh = vq->priv_data;
384 struct em28xx *dev = fh->dev;
385 struct v4l2_frequency f;
387 *size = 16 * fh->dev->width * fh->dev->height >> 3;
389 *count = EM28XX_DEF_BUF;
391 if (*count < EM28XX_MIN_BUF)
392 *count = EM28XX_MIN_BUF;
394 /* Ask tuner to go to analog mode */
395 memset(&f, 0, sizeof(f));
396 f.frequency = dev->ctl_freq;
398 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
403 /* This is called *without* dev->slock held; please keep it that way */
404 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
406 struct em28xx_fh *fh = vq->priv_data;
407 struct em28xx *dev = fh->dev;
408 unsigned long flags = 0;
412 /* We used to wait for the buffer to finish here, but this didn't work
413 because, as we were keeping the state as VIDEOBUF_QUEUED,
414 videobuf_queue_cancel marked it as finished for us.
415 (Also, it could wedge forever if the hardware was misconfigured.)
417 This should be safe; by the time we get here, the buffer isn't
418 queued anymore. If we ever start marking the buffers as
419 VIDEOBUF_ACTIVE, it won't be, though.
421 spin_lock_irqsave(&dev->slock, flags);
422 if (dev->isoc_ctl.buf == buf)
423 dev->isoc_ctl.buf = NULL;
424 spin_unlock_irqrestore(&dev->slock, flags);
426 videobuf_vmalloc_free(&buf->vb);
427 buf->vb.state = VIDEOBUF_NEEDS_INIT;
431 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
432 enum v4l2_field field)
434 struct em28xx_fh *fh = vq->priv_data;
435 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
436 struct em28xx *dev = fh->dev;
437 int rc = 0, urb_init = 0;
439 /* FIXME: It assumes depth = 16 */
440 /* The only currently supported format is 16 bits/pixel */
441 buf->vb.size = 16 * dev->width * dev->height >> 3;
443 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
446 buf->vb.width = dev->width;
447 buf->vb.height = dev->height;
448 buf->vb.field = field;
450 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
451 rc = videobuf_iolock(vq, &buf->vb, NULL);
456 if (!dev->isoc_ctl.num_bufs)
460 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
461 EM28XX_NUM_BUFS, dev->max_pkt_size,
467 buf->vb.state = VIDEOBUF_PREPARED;
471 free_buffer(vq, buf);
476 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
478 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
479 struct em28xx_fh *fh = vq->priv_data;
480 struct em28xx *dev = fh->dev;
481 struct em28xx_dmaqueue *vidq = &dev->vidq;
483 buf->vb.state = VIDEOBUF_QUEUED;
484 list_add_tail(&buf->vb.queue, &vidq->active);
488 static void buffer_release(struct videobuf_queue *vq,
489 struct videobuf_buffer *vb)
491 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
492 struct em28xx_fh *fh = vq->priv_data;
493 struct em28xx *dev = (struct em28xx *)fh->dev;
495 em28xx_isocdbg("em28xx: called buffer_release\n");
497 free_buffer(vq, buf);
500 static struct videobuf_queue_ops em28xx_video_qops = {
501 .buf_setup = buffer_setup,
502 .buf_prepare = buffer_prepare,
503 .buf_queue = buffer_queue,
504 .buf_release = buffer_release,
507 /********************* v4l2 interface **************************************/
511 * inits registers with sane defaults
513 static int em28xx_config(struct em28xx *dev)
516 /* Sets I2C speed to 100 KHz */
518 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
520 /* enable vbi capturing */
522 /* em28xx_write_regs_req(dev, 0x00, 0x0e, "\xC0", 1); audio register */
523 /* em28xx_write_regs_req(dev, 0x00, 0x0f, "\x80", 1); clk register */
524 em28xx_write_regs_req(dev, 0x00, 0x11, "\x51", 1);
526 dev->mute = 1; /* maybe not the right place... */
529 em28xx_outfmt_set_yuv422(dev);
530 em28xx_colorlevels_set_default(dev);
531 em28xx_compression_disable(dev);
537 * em28xx_config_i2c()
538 * configure i2c attached devices
540 static void em28xx_config_i2c(struct em28xx *dev)
542 struct v4l2_routing route;
544 route.input = INPUT(dev->ctl_input)->vmux;
546 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
547 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
548 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
551 static void video_mux(struct em28xx *dev, int index)
553 struct v4l2_routing route;
555 route.input = INPUT(index)->vmux;
557 dev->ctl_input = index;
558 dev->ctl_ainput = INPUT(index)->amux;
560 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
562 if (dev->has_msp34xx) {
563 if (dev->i2s_speed) {
564 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
567 route.input = dev->ctl_ainput;
568 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
569 /* Note: this is msp3400 specific */
570 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
574 em28xx_audio_analog_set(dev);
577 /* Usage lock check functions */
578 static int res_get(struct em28xx_fh *fh)
580 struct em28xx *dev = fh->dev;
583 /* This instance already has stream_on */
590 mutex_lock(&dev->lock);
593 mutex_unlock(&dev->lock);
597 static int res_check(struct em28xx_fh *fh)
599 return (fh->stream_on);
602 static void res_free(struct em28xx_fh *fh)
604 struct em28xx *dev = fh->dev;
606 mutex_lock(&dev->lock);
609 mutex_unlock(&dev->lock);
614 * return the current saturation, brightness or contrast, mute state
616 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
619 case V4L2_CID_AUDIO_MUTE:
620 ctrl->value = dev->mute;
622 case V4L2_CID_AUDIO_VOLUME:
623 ctrl->value = dev->volume;
632 * mute or set new saturation, brightness or contrast
634 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
637 case V4L2_CID_AUDIO_MUTE:
638 if (ctrl->value != dev->mute) {
639 dev->mute = ctrl->value;
640 return em28xx_audio_analog_set(dev);
643 case V4L2_CID_AUDIO_VOLUME:
644 dev->volume = ctrl->value;
645 return em28xx_audio_analog_set(dev);
651 static int check_dev(struct em28xx *dev)
653 if (dev->state & DEV_DISCONNECTED) {
654 em28xx_errdev("v4l2 ioctl: device not present\n");
658 if (dev->state & DEV_MISCONFIGURED) {
659 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
660 "close and open it again\n");
666 static void get_scale(struct em28xx *dev,
667 unsigned int width, unsigned int height,
668 unsigned int *hscale, unsigned int *vscale)
670 unsigned int maxw = norm_maxw(dev);
671 unsigned int maxh = norm_maxh(dev);
673 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
674 if (*hscale >= 0x4000)
677 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
678 if (*vscale >= 0x4000)
682 /* ------------------------------------------------------------------
683 IOCTL vidioc handling
684 ------------------------------------------------------------------*/
686 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
687 struct v4l2_format *f)
689 struct em28xx_fh *fh = priv;
690 struct em28xx *dev = fh->dev;
692 mutex_lock(&dev->lock);
694 f->fmt.pix.width = dev->width;
695 f->fmt.pix.height = dev->height;
696 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
697 f->fmt.pix.bytesperline = dev->width * 2;
698 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
699 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
701 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
702 f->fmt.pix.field = dev->interlaced ?
703 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
705 mutex_unlock(&dev->lock);
709 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
710 struct v4l2_format *f)
712 struct em28xx_fh *fh = priv;
713 struct em28xx *dev = fh->dev;
714 int width = f->fmt.pix.width;
715 int height = f->fmt.pix.height;
716 unsigned int maxw = norm_maxw(dev);
717 unsigned int maxh = norm_maxh(dev);
718 unsigned int hscale, vscale;
720 /* width must even because of the YUYV format
721 height must be even because of interlacing */
734 mutex_lock(&dev->lock);
736 if (dev->is_em2800) {
737 /* the em2800 can only scale down to 50% */
738 if (height % (maxh / 2))
740 if (width % (maxw / 2))
742 /* according to empiatech support */
743 /* the MaxPacketSize is to small to support */
744 /* framesizes larger than 640x480 @ 30 fps */
745 /* or 640x576 @ 25 fps. As this would cut */
746 /* of a part of the image we prefer */
747 /* 360x576 or 360x480 for now */
748 if (width == maxw && height == maxh)
752 get_scale(dev, width, height, &hscale, &vscale);
754 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
755 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
757 f->fmt.pix.width = width;
758 f->fmt.pix.height = height;
759 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
760 f->fmt.pix.bytesperline = width * 2;
761 f->fmt.pix.sizeimage = width * 2 * height;
762 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
763 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
765 mutex_unlock(&dev->lock);
769 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
770 struct v4l2_format *f)
772 struct em28xx_fh *fh = priv;
773 struct em28xx *dev = fh->dev;
780 vidioc_try_fmt_vid_cap(file, priv, f);
782 mutex_lock(&dev->lock);
784 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
785 em28xx_errdev("%s queue busy\n", __func__);
790 if (dev->stream_on && !fh->stream_on) {
791 em28xx_errdev("%s device in use by another fh\n", __func__);
796 /* set new image size */
797 dev->width = f->fmt.pix.width;
798 dev->height = f->fmt.pix.height;
799 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
801 em28xx_set_alternate(dev);
802 em28xx_resolution_set(dev);
807 mutex_unlock(&dev->lock);
811 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
813 struct em28xx_fh *fh = priv;
814 struct em28xx *dev = fh->dev;
815 struct v4l2_format f;
822 mutex_lock(&dev->lock);
824 mutex_unlock(&dev->lock);
826 /* Adjusts width/height, if needed */
827 f.fmt.pix.width = dev->width;
828 f.fmt.pix.height = dev->height;
829 vidioc_try_fmt_vid_cap(file, priv, &f);
831 mutex_lock(&dev->lock);
833 /* set new image size */
834 dev->width = f.fmt.pix.width;
835 dev->height = f.fmt.pix.height;
836 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
838 em28xx_resolution_set(dev);
839 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
841 mutex_unlock(&dev->lock);
845 static const char *iname[] = {
846 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
847 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
848 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
849 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
850 [EM28XX_VMUX_SVIDEO] = "S-Video",
851 [EM28XX_VMUX_TELEVISION] = "Television",
852 [EM28XX_VMUX_CABLE] = "Cable TV",
853 [EM28XX_VMUX_DVB] = "DVB",
854 [EM28XX_VMUX_DEBUG] = "for debug only",
857 static int vidioc_enum_input(struct file *file, void *priv,
858 struct v4l2_input *i)
860 struct em28xx_fh *fh = priv;
861 struct em28xx *dev = fh->dev;
865 if (n >= MAX_EM28XX_INPUT)
867 if (0 == INPUT(n)->type)
871 i->type = V4L2_INPUT_TYPE_CAMERA;
873 strcpy(i->name, iname[INPUT(n)->type]);
875 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
876 (EM28XX_VMUX_CABLE == INPUT(n)->type))
877 i->type = V4L2_INPUT_TYPE_TUNER;
879 i->std = dev->vdev->tvnorms;
884 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
886 struct em28xx_fh *fh = priv;
887 struct em28xx *dev = fh->dev;
894 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
896 struct em28xx_fh *fh = priv;
897 struct em28xx *dev = fh->dev;
904 if (i >= MAX_EM28XX_INPUT)
906 if (0 == INPUT(i)->type)
909 mutex_lock(&dev->lock);
913 mutex_unlock(&dev->lock);
917 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
919 struct em28xx_fh *fh = priv;
920 struct em28xx *dev = fh->dev;
921 unsigned int index = a->index;
926 index = dev->ctl_ainput;
929 strcpy(a->name, "Television");
931 strcpy(a->name, "Line In");
933 a->capability = V4L2_AUDCAP_STEREO;
939 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
941 struct em28xx_fh *fh = priv;
942 struct em28xx *dev = fh->dev;
944 if (a->index != dev->ctl_ainput)
950 static int vidioc_queryctrl(struct file *file, void *priv,
951 struct v4l2_queryctrl *qc)
953 struct em28xx_fh *fh = priv;
954 struct em28xx *dev = fh->dev;
963 memset(qc, 0, sizeof(*qc));
967 if (!dev->has_msp34xx) {
968 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
969 if (qc->id && qc->id == em28xx_qctrl[i].id) {
970 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
975 mutex_lock(&dev->lock);
976 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
977 mutex_unlock(&dev->lock);
985 static int vidioc_g_ctrl(struct file *file, void *priv,
986 struct v4l2_control *ctrl)
988 struct em28xx_fh *fh = priv;
989 struct em28xx *dev = fh->dev;
995 mutex_lock(&dev->lock);
997 if (!dev->has_msp34xx)
998 rc = em28xx_get_ctrl(dev, ctrl);
1002 if (rc == -EINVAL) {
1003 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1007 mutex_unlock(&dev->lock);
1011 static int vidioc_s_ctrl(struct file *file, void *priv,
1012 struct v4l2_control *ctrl)
1014 struct em28xx_fh *fh = priv;
1015 struct em28xx *dev = fh->dev;
1019 rc = check_dev(dev);
1023 mutex_lock(&dev->lock);
1025 if (dev->has_msp34xx)
1026 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1029 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1030 if (ctrl->id == em28xx_qctrl[i].id) {
1031 if (ctrl->value < em28xx_qctrl[i].minimum ||
1032 ctrl->value > em28xx_qctrl[i].maximum) {
1037 rc = em28xx_set_ctrl(dev, ctrl);
1043 /* Control not found - try to send it to the attached devices */
1045 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1049 mutex_unlock(&dev->lock);
1053 static int vidioc_g_tuner(struct file *file, void *priv,
1054 struct v4l2_tuner *t)
1056 struct em28xx_fh *fh = priv;
1057 struct em28xx *dev = fh->dev;
1060 rc = check_dev(dev);
1067 strcpy(t->name, "Tuner");
1069 mutex_lock(&dev->lock);
1071 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1073 mutex_unlock(&dev->lock);
1077 static int vidioc_s_tuner(struct file *file, void *priv,
1078 struct v4l2_tuner *t)
1080 struct em28xx_fh *fh = priv;
1081 struct em28xx *dev = fh->dev;
1084 rc = check_dev(dev);
1091 mutex_lock(&dev->lock);
1093 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1095 mutex_unlock(&dev->lock);
1099 static int vidioc_g_frequency(struct file *file, void *priv,
1100 struct v4l2_frequency *f)
1102 struct em28xx_fh *fh = priv;
1103 struct em28xx *dev = fh->dev;
1105 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1106 f->frequency = dev->ctl_freq;
1111 static int vidioc_s_frequency(struct file *file, void *priv,
1112 struct v4l2_frequency *f)
1114 struct em28xx_fh *fh = priv;
1115 struct em28xx *dev = fh->dev;
1118 rc = check_dev(dev);
1125 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1127 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1130 mutex_lock(&dev->lock);
1132 dev->ctl_freq = f->frequency;
1133 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1135 mutex_unlock(&dev->lock);
1139 #ifdef CONFIG_VIDEO_ADV_DEBUG
1140 static int em28xx_reg_len(int reg)
1143 case EM28XX_R40_AC97LSB:
1144 case EM28XX_R30_HSCALELOW:
1145 case EM28XX_R32_VSCALELOW:
1152 static int vidioc_g_register(struct file *file, void *priv,
1153 struct v4l2_register *reg)
1155 struct em28xx_fh *fh = priv;
1156 struct em28xx *dev = fh->dev;
1159 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1162 if (em28xx_reg_len(reg->reg) == 1) {
1163 ret = em28xx_read_reg(dev, reg->reg);
1170 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1171 reg->reg, (char *)&val, 2);
1175 reg->val = le64_to_cpu(val);
1181 static int vidioc_s_register(struct file *file, void *priv,
1182 struct v4l2_register *reg)
1184 struct em28xx_fh *fh = priv;
1185 struct em28xx *dev = fh->dev;
1188 buf = cpu_to_le64(reg->val);
1190 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1191 em28xx_reg_len(reg->reg));
1196 static int vidioc_cropcap(struct file *file, void *priv,
1197 struct v4l2_cropcap *cc)
1199 struct em28xx_fh *fh = priv;
1200 struct em28xx *dev = fh->dev;
1202 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1205 cc->bounds.left = 0;
1207 cc->bounds.width = dev->width;
1208 cc->bounds.height = dev->height;
1209 cc->defrect = cc->bounds;
1210 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1211 cc->pixelaspect.denominator = 59;
1216 static int vidioc_streamon(struct file *file, void *priv,
1217 enum v4l2_buf_type type)
1219 struct em28xx_fh *fh = priv;
1220 struct em28xx *dev = fh->dev;
1223 rc = check_dev(dev);
1228 if (unlikely(res_get(fh) < 0))
1231 return (videobuf_streamon(&fh->vb_vidq));
1234 static int vidioc_streamoff(struct file *file, void *priv,
1235 enum v4l2_buf_type type)
1237 struct em28xx_fh *fh = priv;
1238 struct em28xx *dev = fh->dev;
1241 rc = check_dev(dev);
1245 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1247 if (type != fh->type)
1250 videobuf_streamoff(&fh->vb_vidq);
1256 static int vidioc_querycap(struct file *file, void *priv,
1257 struct v4l2_capability *cap)
1259 struct em28xx_fh *fh = priv;
1260 struct em28xx *dev = fh->dev;
1262 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1263 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1264 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1266 cap->version = EM28XX_VERSION_CODE;
1269 V4L2_CAP_SLICED_VBI_CAPTURE |
1270 V4L2_CAP_VIDEO_CAPTURE |
1272 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1274 if (dev->tuner_type != TUNER_ABSENT)
1275 cap->capabilities |= V4L2_CAP_TUNER;
1280 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1281 struct v4l2_fmtdesc *fmtd)
1283 if (fmtd->index != 0)
1286 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1287 strcpy(fmtd->description, "Packed YUY2");
1288 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1289 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1294 /* Sliced VBI ioctls */
1295 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1296 struct v4l2_format *f)
1298 struct em28xx_fh *fh = priv;
1299 struct em28xx *dev = fh->dev;
1302 rc = check_dev(dev);
1306 mutex_lock(&dev->lock);
1308 f->fmt.sliced.service_set = 0;
1310 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1312 if (f->fmt.sliced.service_set == 0)
1315 mutex_unlock(&dev->lock);
1319 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1320 struct v4l2_format *f)
1322 struct em28xx_fh *fh = priv;
1323 struct em28xx *dev = fh->dev;
1326 rc = check_dev(dev);
1330 mutex_lock(&dev->lock);
1331 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1332 mutex_unlock(&dev->lock);
1334 if (f->fmt.sliced.service_set == 0)
1341 static int vidioc_reqbufs(struct file *file, void *priv,
1342 struct v4l2_requestbuffers *rb)
1344 struct em28xx_fh *fh = priv;
1345 struct em28xx *dev = fh->dev;
1348 rc = check_dev(dev);
1352 return (videobuf_reqbufs(&fh->vb_vidq, rb));
1355 static int vidioc_querybuf(struct file *file, void *priv,
1356 struct v4l2_buffer *b)
1358 struct em28xx_fh *fh = priv;
1359 struct em28xx *dev = fh->dev;
1362 rc = check_dev(dev);
1366 return (videobuf_querybuf(&fh->vb_vidq, b));
1369 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1371 struct em28xx_fh *fh = priv;
1372 struct em28xx *dev = fh->dev;
1375 rc = check_dev(dev);
1379 return (videobuf_qbuf(&fh->vb_vidq, b));
1382 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1384 struct em28xx_fh *fh = priv;
1385 struct em28xx *dev = fh->dev;
1388 rc = check_dev(dev);
1392 return (videobuf_dqbuf(&fh->vb_vidq, b,
1393 file->f_flags & O_NONBLOCK));
1396 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1397 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1399 struct em28xx_fh *fh = priv;
1401 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1406 /* ----------------------------------------------------------- */
1407 /* RADIO ESPECIFIC IOCTLS */
1408 /* ----------------------------------------------------------- */
1410 static int radio_querycap(struct file *file, void *priv,
1411 struct v4l2_capability *cap)
1413 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1415 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1416 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1417 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1419 cap->version = EM28XX_VERSION_CODE;
1420 cap->capabilities = V4L2_CAP_TUNER;
1424 static int radio_g_tuner(struct file *file, void *priv,
1425 struct v4l2_tuner *t)
1427 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1429 if (unlikely(t->index > 0))
1432 strcpy(t->name, "Radio");
1433 t->type = V4L2_TUNER_RADIO;
1435 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1439 static int radio_enum_input(struct file *file, void *priv,
1440 struct v4l2_input *i)
1444 strcpy(i->name, "Radio");
1445 i->type = V4L2_INPUT_TYPE_TUNER;
1450 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1452 if (unlikely(a->index))
1455 strcpy(a->name, "Radio");
1459 static int radio_s_tuner(struct file *file, void *priv,
1460 struct v4l2_tuner *t)
1462 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1467 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1472 static int radio_s_audio(struct file *file, void *fh,
1473 struct v4l2_audio *a)
1478 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1483 static int radio_queryctrl(struct file *file, void *priv,
1484 struct v4l2_queryctrl *qc)
1488 if (qc->id < V4L2_CID_BASE ||
1489 qc->id >= V4L2_CID_LASTP1)
1492 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1493 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1494 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1503 * em28xx_v4l2_open()
1504 * inits the device and starts isoc transfer
1506 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1508 int minor = iminor(inode);
1509 int errCode = 0, radio = 0;
1510 struct em28xx *h, *dev = NULL;
1511 struct em28xx_fh *fh;
1512 enum v4l2_buf_type fh_type = 0;
1514 list_for_each_entry(h, &em28xx_devlist, devlist) {
1515 if (h->vdev->minor == minor) {
1517 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1519 if (h->vbi_dev->minor == minor) {
1521 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1524 h->radio_dev->minor == minor) {
1532 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1533 minor, v4l2_type_names[fh_type], dev->users);
1536 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1538 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1541 mutex_lock(&dev->lock);
1545 filp->private_data = fh;
1547 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1548 dev->width = norm_maxw(dev);
1549 dev->height = norm_maxh(dev);
1553 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
1554 em28xx_set_alternate(dev);
1555 em28xx_resolution_set(dev);
1557 /* Needed, since GPIO might have disabled power of
1560 em28xx_config_i2c(dev);
1564 em28xx_videodbg("video_open: setting radio device\n");
1565 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1570 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1571 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1572 sizeof(struct em28xx_buffer), fh);
1574 mutex_unlock(&dev->lock);
1580 * em28xx_realease_resources()
1581 * unregisters the v4l2,i2c and usb devices
1582 * called when the device gets disconected or at module unload
1584 static void em28xx_release_resources(struct em28xx *dev)
1587 /*FIXME: I2C IR should be disconnected */
1589 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1590 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1591 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
1592 list_del(&dev->devlist);
1593 if (dev->sbutton_input_dev)
1594 em28xx_deregister_snapshot_button(dev);
1595 if (dev->radio_dev) {
1596 if (-1 != dev->radio_dev->minor)
1597 video_unregister_device(dev->radio_dev);
1599 video_device_release(dev->radio_dev);
1600 dev->radio_dev = NULL;
1603 if (-1 != dev->vbi_dev->minor)
1604 video_unregister_device(dev->vbi_dev);
1606 video_device_release(dev->vbi_dev);
1607 dev->vbi_dev = NULL;
1610 if (-1 != dev->vdev->minor)
1611 video_unregister_device(dev->vdev);
1613 video_device_release(dev->vdev);
1616 em28xx_i2c_unregister(dev);
1617 usb_put_dev(dev->udev);
1619 /* Mark device as unused */
1620 em28xx_devused &= ~(1<<dev->devno);
1624 * em28xx_v4l2_close()
1625 * stops streaming and deallocates all resources allocated by the v4l2
1628 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1630 struct em28xx_fh *fh = filp->private_data;
1631 struct em28xx *dev = fh->dev;
1634 em28xx_videodbg("users=%d\n", dev->users);
1640 mutex_lock(&dev->lock);
1642 if (dev->users == 1) {
1643 videobuf_stop(&fh->vb_vidq);
1644 videobuf_mmap_free(&fh->vb_vidq);
1646 /* the device is already disconnect,
1647 free the remaining resources */
1648 if (dev->state & DEV_DISCONNECTED) {
1649 em28xx_release_resources(dev);
1650 mutex_unlock(&dev->lock);
1655 /* do this before setting alternate! */
1656 em28xx_uninit_isoc(dev);
1657 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
1659 /* set alternate 0 */
1661 em28xx_videodbg("setting alternate 0\n");
1662 errCode = usb_set_interface(dev->udev, 0, 0);
1664 em28xx_errdev("cannot change alternate number to "
1665 "0 (error=%i)\n", errCode);
1670 wake_up_interruptible_nr(&dev->open, 1);
1671 mutex_unlock(&dev->lock);
1676 * em28xx_v4l2_read()
1677 * will allocate buffers when called for the first time
1680 em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1683 struct em28xx_fh *fh = filp->private_data;
1684 struct em28xx *dev = fh->dev;
1687 rc = check_dev(dev);
1691 /* FIXME: read() is not prepared to allow changing the video
1692 resolution while streaming. Seems a bug at em28xx_set_fmt
1695 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1696 if (unlikely(res_get(fh)))
1699 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1700 filp->f_flags & O_NONBLOCK);
1706 * em28xx_v4l2_poll()
1707 * will allocate buffers when called for the first time
1709 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
1711 struct em28xx_fh *fh = filp->private_data;
1712 struct em28xx *dev = fh->dev;
1715 rc = check_dev(dev);
1719 if (unlikely(res_get(fh) < 0))
1722 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1725 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1729 * em28xx_v4l2_mmap()
1731 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1733 struct em28xx_fh *fh = filp->private_data;
1734 struct em28xx *dev = fh->dev;
1737 if (unlikely(res_get(fh) < 0))
1740 rc = check_dev(dev);
1744 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1746 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1747 (unsigned long)vma->vm_start,
1748 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1754 static const struct file_operations em28xx_v4l_fops = {
1755 .owner = THIS_MODULE,
1756 .open = em28xx_v4l2_open,
1757 .release = em28xx_v4l2_close,
1758 .read = em28xx_v4l2_read,
1759 .poll = em28xx_v4l2_poll,
1760 .mmap = em28xx_v4l2_mmap,
1761 .ioctl = video_ioctl2,
1762 .llseek = no_llseek,
1763 .compat_ioctl = v4l_compat_ioctl32,
1766 static const struct file_operations radio_fops = {
1767 .owner = THIS_MODULE,
1768 .open = em28xx_v4l2_open,
1769 .release = em28xx_v4l2_close,
1770 .ioctl = video_ioctl2,
1771 .compat_ioctl = v4l_compat_ioctl32,
1772 .llseek = no_llseek,
1775 static const struct video_device em28xx_video_template = {
1776 .fops = &em28xx_v4l_fops,
1777 .release = video_device_release,
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,
1818 .tvnorms = V4L2_STD_ALL,
1819 .current_norm = V4L2_STD_PAL,
1822 static struct video_device em28xx_radio_template = {
1823 .name = "em28xx-radio",
1824 .type = VID_TYPE_TUNER,
1825 .fops = &radio_fops,
1827 .vidioc_querycap = radio_querycap,
1828 .vidioc_g_tuner = radio_g_tuner,
1829 .vidioc_enum_input = radio_enum_input,
1830 .vidioc_g_audio = radio_g_audio,
1831 .vidioc_s_tuner = radio_s_tuner,
1832 .vidioc_s_audio = radio_s_audio,
1833 .vidioc_s_input = radio_s_input,
1834 .vidioc_queryctrl = radio_queryctrl,
1835 .vidioc_g_ctrl = vidioc_g_ctrl,
1836 .vidioc_s_ctrl = vidioc_s_ctrl,
1837 .vidioc_g_frequency = vidioc_g_frequency,
1838 .vidioc_s_frequency = vidioc_s_frequency,
1839 #ifdef CONFIG_VIDEO_ADV_DEBUG
1840 .vidioc_g_register = vidioc_g_register,
1841 .vidioc_s_register = vidioc_s_register,
1845 /******************************** usb interface ******************************/
1848 static LIST_HEAD(em28xx_extension_devlist);
1849 static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1851 int em28xx_register_extension(struct em28xx_ops *ops)
1853 struct em28xx *dev = NULL;
1855 mutex_lock(&em28xx_extension_devlist_lock);
1856 list_add_tail(&ops->next, &em28xx_extension_devlist);
1857 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1861 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1862 mutex_unlock(&em28xx_extension_devlist_lock);
1865 EXPORT_SYMBOL(em28xx_register_extension);
1867 void em28xx_unregister_extension(struct em28xx_ops *ops)
1869 struct em28xx *dev = NULL;
1871 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1876 mutex_lock(&em28xx_extension_devlist_lock);
1877 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1878 list_del(&ops->next);
1879 mutex_unlock(&em28xx_extension_devlist_lock);
1881 EXPORT_SYMBOL(em28xx_unregister_extension);
1883 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1884 const struct video_device *template,
1886 const char *type_name)
1888 struct video_device *vfd;
1890 vfd = video_device_alloc();
1895 vfd->dev = &dev->udev->dev;
1896 vfd->release = video_device_release;
1898 vfd->debug = video_debug;
1900 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1901 dev->name, type_name);
1909 * allocates and inits the device structs, registers i2c bus and v4l device
1911 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
1914 struct em28xx_ops *ops = NULL;
1915 struct em28xx *dev = *devhandle;
1916 int retval = -ENOMEM;
1918 unsigned int maxh, maxw;
1921 mutex_init(&dev->lock);
1922 spin_lock_init(&dev->slock);
1923 init_waitqueue_head(&dev->open);
1924 init_waitqueue_head(&dev->wait_frame);
1925 init_waitqueue_head(&dev->wait_stream);
1927 dev->em28xx_write_regs = em28xx_write_regs;
1928 dev->em28xx_read_reg = em28xx_read_reg;
1929 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1930 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1931 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1932 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
1934 em28xx_pre_card_setup(dev);
1936 errCode = em28xx_config(dev);
1938 em28xx_errdev("error configuring device\n");
1939 em28xx_devused &= ~(1<<dev->devno);
1944 /* register i2c bus */
1945 em28xx_i2c_register(dev);
1947 /* Do board specific init and eeprom reading */
1948 em28xx_card_setup(dev);
1950 /* Configure audio */
1951 em28xx_audio_analog_set(dev);
1953 /* configure the device */
1954 em28xx_config_i2c(dev);
1956 /* set default norm */
1957 dev->norm = em28xx_video_template.current_norm;
1959 maxw = norm_maxw(dev);
1960 maxh = norm_maxh(dev);
1962 /* set default image size */
1965 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1970 errCode = em28xx_config(dev);
1972 list_add_tail(&dev->devlist, &em28xx_devlist);
1974 /* allocate and fill video video_device struct */
1975 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
1976 VID_TYPE_CAPTURE, "video");
1977 if (NULL == dev->vdev) {
1978 em28xx_errdev("cannot allocate video_device.\n");
1981 if (dev->tuner_type != TUNER_ABSENT)
1982 dev->vdev->type |= VID_TYPE_TUNER;
1984 /* register v4l2 video video_device */
1985 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1986 video_nr[dev->devno]);
1988 em28xx_errdev("unable to register video device (error=%i).\n",
1993 /* Allocate and fill vbi video_device struct */
1994 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
1995 VFL_TYPE_VBI, "vbi");
1996 /* register v4l2 vbi video_device */
1997 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1998 vbi_nr[dev->devno]) < 0) {
1999 em28xx_errdev("unable to register vbi device\n");
2004 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2005 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2006 VFL_TYPE_RADIO, "radio");
2007 if (NULL == dev->radio_dev) {
2008 em28xx_errdev("cannot allocate video_device.\n");
2011 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2012 radio_nr[dev->devno]);
2014 em28xx_errdev("can't register radio device\n");
2017 em28xx_info("Registered radio device as /dev/radio%d\n",
2018 dev->radio_dev->minor & 0x1f);
2021 /* init video dma queues */
2022 INIT_LIST_HEAD(&dev->vidq.active);
2023 INIT_LIST_HEAD(&dev->vidq.queued);
2026 if (dev->has_msp34xx) {
2027 /* Send a reset to other chips via gpio */
2028 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2030 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2036 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2037 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2038 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
2040 mutex_lock(&em28xx_extension_devlist_lock);
2041 if (!list_empty(&em28xx_extension_devlist)) {
2042 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2047 mutex_unlock(&em28xx_extension_devlist_lock);
2052 em28xx_release_resources(dev);
2053 mutex_unlock(&dev->lock);
2058 #if defined(CONFIG_MODULES) && defined(MODULE)
2059 static void request_module_async(struct work_struct *work)
2061 struct em28xx *dev = container_of(work,
2062 struct em28xx, request_module_wk);
2064 if (dev->has_audio_class)
2065 request_module("snd-usb-audio");
2067 request_module("em28xx-alsa");
2070 request_module("em28xx-dvb");
2073 static void request_modules(struct em28xx *dev)
2075 INIT_WORK(&dev->request_module_wk, request_module_async);
2076 schedule_work(&dev->request_module_wk);
2079 #define request_modules(dev)
2080 #endif /* CONFIG_MODULES */
2083 * em28xx_usb_probe()
2084 * checks for supported devices
2086 static int em28xx_usb_probe(struct usb_interface *interface,
2087 const struct usb_device_id *id)
2089 const struct usb_endpoint_descriptor *endpoint;
2090 struct usb_device *udev;
2091 struct usb_interface *uif;
2092 struct em28xx *dev = NULL;
2093 int retval = -ENODEV;
2096 udev = usb_get_dev(interface_to_usbdev(interface));
2097 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2099 /* Check to see next free device and mark as used */
2100 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2101 em28xx_devused |= 1<<nr;
2103 /* Don't register audio interfaces */
2104 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2105 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
2106 udev->descriptor.idVendor,
2107 udev->descriptor.idProduct,
2109 interface->altsetting[0].desc.bInterfaceClass);
2111 em28xx_devused &= ~(1<<nr);
2115 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
2116 udev->descriptor.idVendor,
2117 udev->descriptor.idProduct,
2119 interface->altsetting[0].desc.bInterfaceClass);
2121 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2123 /* check if the device has the iso in endpoint at the correct place */
2124 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2125 USB_ENDPOINT_XFER_ISOC) {
2126 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
2127 em28xx_devused &= ~(1<<nr);
2130 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
2131 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
2132 em28xx_devused &= ~(1<<nr);
2136 if (nr >= EM28XX_MAXBOARDS) {
2137 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2139 em28xx_devused &= ~(1<<nr);
2143 /* allocate memory for our device state and initialize it */
2144 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2146 em28xx_err(DRIVER_NAME ": out of memory!\n");
2147 em28xx_devused &= ~(1<<nr);
2151 snprintf(dev->name, 29, "em28xx #%d", nr);
2153 dev->model = id->driver_info;
2156 /* Checks if audio is provided by some interface */
2157 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2158 uif = udev->config->interface[i];
2159 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2160 dev->has_audio_class = 1;
2165 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2166 dev->has_audio_class ? "Has" : "Doesn't have");
2168 /* compute alternate max packet sizes */
2169 uif = udev->actconfig->interface[0];
2171 dev->num_alt = uif->num_altsetting;
2172 em28xx_info("Alternate settings: %i\n", dev->num_alt);
2173 /* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2174 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2176 if (dev->alt_max_pkt_size == NULL) {
2177 em28xx_errdev("out of memory!\n");
2178 em28xx_devused &= ~(1<<nr);
2183 for (i = 0; i < dev->num_alt ; i++) {
2184 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2186 dev->alt_max_pkt_size[i] =
2187 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
2188 em28xx_info("Alternate setting %i, max size= %i\n", i,
2189 dev->alt_max_pkt_size[i]);
2192 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
2193 dev->model = card[nr];
2195 /* allocate device struct */
2196 retval = em28xx_init_dev(&dev, udev, nr);
2200 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
2202 /* save our data pointer in this interface device */
2203 usb_set_intfdata(interface, dev);
2205 request_modules(dev);
2211 * em28xx_usb_disconnect()
2212 * called when the device gets diconencted
2213 * video device will be unregistered on v4l2_close in case it is still open
2215 static void em28xx_usb_disconnect(struct usb_interface *interface)
2218 struct em28xx_ops *ops = NULL;
2220 dev = usb_get_intfdata(interface);
2221 usb_set_intfdata(interface, NULL);
2226 em28xx_info("disconnecting %s\n", dev->vdev->name);
2228 /* wait until all current v4l2 io is finished then deallocate
2230 mutex_lock(&dev->lock);
2232 wake_up_interruptible_all(&dev->open);
2236 ("device /dev/video%d is open! Deregistration and memory "
2237 "deallocation are deferred on close.\n",
2238 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2240 dev->state |= DEV_MISCONFIGURED;
2241 em28xx_uninit_isoc(dev);
2242 dev->state |= DEV_DISCONNECTED;
2243 wake_up_interruptible(&dev->wait_frame);
2244 wake_up_interruptible(&dev->wait_stream);
2246 dev->state |= DEV_DISCONNECTED;
2247 em28xx_release_resources(dev);
2249 mutex_unlock(&dev->lock);
2251 mutex_lock(&em28xx_extension_devlist_lock);
2252 if (!list_empty(&em28xx_extension_devlist)) {
2253 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2257 mutex_unlock(&em28xx_extension_devlist_lock);
2260 kfree(dev->alt_max_pkt_size);
2265 static struct usb_driver em28xx_usb_driver = {
2267 .probe = em28xx_usb_probe,
2268 .disconnect = em28xx_usb_disconnect,
2269 .id_table = em28xx_id_table,
2272 static int __init em28xx_module_init(void)
2276 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
2277 (EM28XX_VERSION_CODE >> 16) & 0xff,
2278 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2280 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2281 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2284 /* register this driver with the USB subsystem */
2285 result = usb_register(&em28xx_usb_driver);
2287 em28xx_err(DRIVER_NAME
2288 " usb_register failed. Error number %d.\n", result);
2293 static void __exit em28xx_module_exit(void)
2295 /* deregister this driver with the USB subsystem */
2296 usb_deregister(&em28xx_usb_driver);
2299 module_init(em28xx_module_init);
2300 module_exit(em28xx_module_exit);