2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
4 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
6 Mauro Carvalho Chehab <mchehab@infradead.org>
7 Sascha Sommer <saschasommer@freenet.de>
9 Some parts based on SN9C10x PC Camera Controllers GPL driver made
10 by Luca Risolia <luca.risolia@studio.unibo.it>
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/init.h>
28 #include <linux/list.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/bitmap.h>
32 #include <linux/usb.h>
33 #include <linux/i2c.h>
34 #include <linux/version.h>
36 #include <linux/mutex.h>
39 #include <media/v4l2-common.h>
40 #include <media/msp3400.h>
42 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
43 "Markus Rechberger <mrechberger@gmail.com>, " \
44 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
45 "Sascha Sommer <saschasommer@freenet.de>"
47 #define DRIVER_NAME "em28xx"
48 #define DRIVER_DESC "Empia em28xx based USB video device driver"
49 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
51 #define em28xx_videodbg(fmt, arg...) do {\
53 printk(KERN_INFO "%s %s :"fmt, \
54 dev->name, __FUNCTION__ , ##arg); } while (0)
56 MODULE_AUTHOR(DRIVER_AUTHOR);
57 MODULE_DESCRIPTION(DRIVER_DESC);
58 MODULE_LICENSE("GPL");
60 static LIST_HEAD(em28xx_devlist);
62 static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
63 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
64 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
65 module_param_array(card, int, NULL, 0444);
66 module_param_array(video_nr, int, NULL, 0444);
67 module_param_array(vbi_nr, int, NULL, 0444);
68 MODULE_PARM_DESC(card,"card type");
69 MODULE_PARM_DESC(video_nr,"video device numbers");
70 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
72 static unsigned int video_debug = 0;
73 module_param(video_debug,int,0644);
74 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
76 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
77 static unsigned long em28xx_devused;
79 /* supported controls */
80 /* Common to all boards */
81 static struct v4l2_queryctrl em28xx_qctrl[] = {
83 .id = V4L2_CID_AUDIO_VOLUME,
84 .type = V4L2_CTRL_TYPE_INTEGER,
89 .default_value = 0x1f,
92 .id = V4L2_CID_AUDIO_MUTE,
93 .type = V4L2_CTRL_TYPE_BOOLEAN,
103 static struct usb_driver em28xx_usb_driver;
106 /********************* v4l2 interface ******************************************/
110 * inits registers with sane defaults
112 static int em28xx_config(struct em28xx *dev)
115 /* Sets I2C speed to 100 KHz */
117 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
119 /* enable vbi capturing */
121 /* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
122 /* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
123 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
125 dev->mute = 1; /* maybe not the right place... */
128 em28xx_outfmt_set_yuv422(dev);
129 em28xx_colorlevels_set_default(dev);
130 em28xx_compression_disable(dev);
136 * em28xx_config_i2c()
137 * configure i2c attached devices
139 static void em28xx_config_i2c(struct em28xx *dev)
141 struct v4l2_routing route;
143 route.input = INPUT(dev->ctl_input)->vmux;
145 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
146 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
147 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
151 * em28xx_empty_framequeues()
152 * prepare queues for incoming and outgoing frames
154 static void em28xx_empty_framequeues(struct em28xx *dev)
158 INIT_LIST_HEAD(&dev->inqueue);
159 INIT_LIST_HEAD(&dev->outqueue);
161 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
162 dev->frame[i].state = F_UNUSED;
163 dev->frame[i].buf.bytesused = 0;
167 static void video_mux(struct em28xx *dev, int index)
169 struct v4l2_routing route;
171 route.input = INPUT(index)->vmux;
173 dev->ctl_input = index;
174 dev->ctl_ainput = INPUT(index)->amux;
176 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
178 if (dev->has_msp34xx) {
180 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
181 route.input = dev->ctl_ainput;
182 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
183 /* Note: this is msp3400 specific */
184 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
187 em28xx_set_audio_source(dev);
190 /* Usage lock check functions */
191 static int res_get(struct em28xx_fh *fh)
193 struct em28xx *dev = fh->dev;
196 /* This instance already has stream_on */
200 mutex_lock(&dev->lock);
209 mutex_unlock(&dev->lock);
213 static int res_check(struct em28xx_fh *fh)
215 return (fh->stream_on);
218 static void res_free(struct em28xx_fh *fh)
220 struct em28xx *dev = fh->dev;
222 mutex_lock(&dev->lock);
225 mutex_unlock(&dev->lock);
231 static void em28xx_vm_open(struct vm_area_struct *vma)
233 struct em28xx_frame_t *f = vma->vm_private_data;
240 static void em28xx_vm_close(struct vm_area_struct *vma)
242 /* NOTE: buffers are not freed here */
243 struct em28xx_frame_t *f = vma->vm_private_data;
245 if (f->vma_use_count)
249 static struct vm_operations_struct em28xx_vm_ops = {
250 .open = em28xx_vm_open,
251 .close = em28xx_vm_close,
257 * return the current saturation, brightness or contrast, mute state
259 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
262 case V4L2_CID_AUDIO_MUTE:
263 ctrl->value = dev->mute;
265 case V4L2_CID_AUDIO_VOLUME:
266 ctrl->value = dev->volume;
275 * mute or set new saturation, brightness or contrast
277 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
280 case V4L2_CID_AUDIO_MUTE:
281 if (ctrl->value != dev->mute) {
282 dev->mute = ctrl->value;
283 return em28xx_audio_analog_set(dev);
286 case V4L2_CID_AUDIO_VOLUME:
287 dev->volume = ctrl->value;
288 return em28xx_audio_analog_set(dev);
295 * em28xx_stream_interrupt()
298 static int em28xx_stream_interrupt(struct em28xx *dev)
302 /* stop reading from the device */
304 dev->stream = STREAM_INTERRUPT;
305 rc = wait_event_timeout(dev->wait_stream,
306 (dev->stream == STREAM_OFF) ||
307 (dev->state & DEV_DISCONNECTED),
311 dev->state |= DEV_MISCONFIGURED;
312 em28xx_videodbg("device is misconfigured; close and "
313 "open /dev/video%d again\n",
314 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
322 static int check_dev(struct em28xx *dev)
324 if (dev->state & DEV_DISCONNECTED) {
325 em28xx_errdev("v4l2 ioctl: device not present\n");
329 if (dev->state & DEV_MISCONFIGURED) {
330 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
331 "close and open it again\n");
337 static void get_scale(struct em28xx *dev,
338 unsigned int width, unsigned int height,
339 unsigned int *hscale, unsigned int *vscale)
341 unsigned int maxw = norm_maxw(dev);
342 unsigned int maxh = norm_maxh(dev);
344 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
345 if (*hscale >= 0x4000)
348 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
349 if (*vscale >= 0x4000)
353 /* ------------------------------------------------------------------
354 IOCTL vidioc handling
355 ------------------------------------------------------------------*/
357 static int vidioc_g_fmt_cap(struct file *file, void *priv,
358 struct v4l2_format *f)
360 struct em28xx_fh *fh = priv;
361 struct em28xx *dev = fh->dev;
363 mutex_lock(&dev->lock);
365 f->fmt.pix.width = dev->width;
366 f->fmt.pix.height = dev->height;
367 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
368 f->fmt.pix.bytesperline = dev->bytesperline;
369 f->fmt.pix.sizeimage = dev->frame_size;
370 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
372 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
373 f->fmt.pix.field = dev->interlaced ?
374 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
376 mutex_unlock(&dev->lock);
380 static int vidioc_try_fmt_cap(struct file *file, void *priv,
381 struct v4l2_format *f)
383 struct em28xx_fh *fh = priv;
384 struct em28xx *dev = fh->dev;
385 int width = f->fmt.pix.width;
386 int height = f->fmt.pix.height;
387 unsigned int maxw = norm_maxw(dev);
388 unsigned int maxh = norm_maxh(dev);
389 unsigned int hscale, vscale;
391 /* width must even because of the YUYV format
392 height must be even because of interlacing */
405 mutex_lock(&dev->lock);
407 if (dev->is_em2800) {
408 /* the em2800 can only scale down to 50% */
409 if (height % (maxh / 2))
411 if (width % (maxw / 2))
413 /* according to empiatech support */
414 /* the MaxPacketSize is to small to support */
415 /* framesizes larger than 640x480 @ 30 fps */
416 /* or 640x576 @ 25 fps. As this would cut */
417 /* of a part of the image we prefer */
418 /* 360x576 or 360x480 for now */
419 if (width == maxw && height == maxh)
423 get_scale(dev, width, height, &hscale, &vscale);
425 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
426 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
428 f->fmt.pix.width = width;
429 f->fmt.pix.height = height;
430 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
431 f->fmt.pix.bytesperline = width * 2;
432 f->fmt.pix.sizeimage = width * 2 * height;
433 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
434 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
436 mutex_unlock(&dev->lock);
440 static int vidioc_s_fmt_cap(struct file *file, void *priv,
441 struct v4l2_format *f)
443 struct em28xx_fh *fh = priv;
444 struct em28xx *dev = fh->dev;
451 vidioc_try_fmt_cap(file, priv, f);
453 mutex_lock(&dev->lock);
455 for (i = 0; i < dev->num_frames; i++)
456 if (dev->frame[i].vma_use_count) {
457 em28xx_videodbg("VIDIOC_S_FMT failed. "
458 "Unmap the buffers first.\n");
463 /* stop io in case it is already in progress */
464 if (dev->stream == STREAM_ON) {
465 em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n");
466 rc = em28xx_stream_interrupt(dev);
471 em28xx_release_buffers(dev);
474 /* set new image size */
475 dev->width = f->fmt.pix.width;
476 dev->height = f->fmt.pix.height;
477 dev->frame_size = dev->width * dev->height * 2;
478 dev->field_size = dev->frame_size >> 1;
479 dev->bytesperline = dev->width * 2;
480 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
482 /* FIXME: This is really weird! Why capture is starting with
485 em28xx_uninit_isoc(dev);
486 em28xx_set_alternate(dev);
487 em28xx_capture_start(dev, 1);
488 em28xx_resolution_set(dev);
489 em28xx_init_isoc(dev);
493 mutex_unlock(&dev->lock);
497 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
499 struct em28xx_fh *fh = priv;
500 struct em28xx *dev = fh->dev;
501 struct v4l2_format f;
508 mutex_lock(&dev->lock);
510 mutex_unlock(&dev->lock);
512 /* Adjusts width/height, if needed */
513 f.fmt.pix.width = dev->width;
514 f.fmt.pix.height = dev->height;
515 vidioc_try_fmt_cap(file, priv, &f);
517 mutex_lock(&dev->lock);
519 /* set new image size */
520 dev->width = f.fmt.pix.width;
521 dev->height = f.fmt.pix.height;
522 dev->frame_size = dev->width * dev->height * 2;
523 dev->field_size = dev->frame_size >> 1;
524 dev->bytesperline = dev->width * 2;
525 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
527 em28xx_resolution_set(dev);
528 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
530 mutex_unlock(&dev->lock);
534 static const char *iname[] = {
535 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
536 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
537 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
538 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
539 [EM28XX_VMUX_SVIDEO] = "S-Video",
540 [EM28XX_VMUX_TELEVISION] = "Television",
541 [EM28XX_VMUX_CABLE] = "Cable TV",
542 [EM28XX_VMUX_DVB] = "DVB",
543 [EM28XX_VMUX_DEBUG] = "for debug only",
546 static int vidioc_enum_input(struct file *file, void *priv,
547 struct v4l2_input *i)
549 struct em28xx_fh *fh = priv;
550 struct em28xx *dev = fh->dev;
554 if (n >= MAX_EM28XX_INPUT)
556 if (0 == INPUT(n)->type)
560 i->type = V4L2_INPUT_TYPE_CAMERA;
562 strcpy(i->name, iname[INPUT(n)->type]);
564 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
565 (EM28XX_VMUX_CABLE == INPUT(n)->type))
566 i->type = V4L2_INPUT_TYPE_TUNER;
568 i->std = dev->vdev->tvnorms;
573 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
575 struct em28xx_fh *fh = priv;
576 struct em28xx *dev = fh->dev;
583 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
585 struct em28xx_fh *fh = priv;
586 struct em28xx *dev = fh->dev;
593 if (i >= MAX_EM28XX_INPUT)
595 if (0 == INPUT(i)->type)
598 mutex_lock(&dev->lock);
602 mutex_unlock(&dev->lock);
606 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
608 struct em28xx_fh *fh = priv;
609 struct em28xx *dev = fh->dev;
610 unsigned int index = a->index;
615 index = dev->ctl_ainput;
618 strcpy(a->name, "Television");
620 strcpy(a->name, "Line In");
622 a->capability = V4L2_AUDCAP_STEREO;
628 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
630 struct em28xx_fh *fh = priv;
631 struct em28xx *dev = fh->dev;
633 if (a->index != dev->ctl_ainput)
639 static int vidioc_queryctrl(struct file *file, void *priv,
640 struct v4l2_queryctrl *qc)
642 struct em28xx_fh *fh = priv;
643 struct em28xx *dev = fh->dev;
652 memset(qc, 0, sizeof(*qc));
656 if (!dev->has_msp34xx) {
657 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
658 if (qc->id && qc->id == em28xx_qctrl[i].id) {
659 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
664 mutex_lock(&dev->lock);
665 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
666 mutex_unlock(&dev->lock);
674 static int vidioc_g_ctrl(struct file *file, void *priv,
675 struct v4l2_control *ctrl)
677 struct em28xx_fh *fh = priv;
678 struct em28xx *dev = fh->dev;
684 mutex_lock(&dev->lock);
686 if (!dev->has_msp34xx)
687 rc = em28xx_get_ctrl(dev, ctrl);
692 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
696 mutex_unlock(&dev->lock);
700 static int vidioc_s_ctrl(struct file *file, void *priv,
701 struct v4l2_control *ctrl)
703 struct em28xx_fh *fh = priv;
704 struct em28xx *dev = fh->dev;
712 mutex_lock(&dev->lock);
714 if (dev->has_msp34xx)
715 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
718 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
719 if (ctrl->id == em28xx_qctrl[i].id) {
720 if (ctrl->value < em28xx_qctrl[i].minimum ||
721 ctrl->value > em28xx_qctrl[i].maximum) {
726 rc = em28xx_set_ctrl(dev, ctrl);
732 /* Control not found - try to send it to the attached devices */
734 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
738 mutex_unlock(&dev->lock);
742 static int vidioc_g_tuner(struct file *file, void *priv,
743 struct v4l2_tuner *t)
745 struct em28xx_fh *fh = priv;
746 struct em28xx *dev = fh->dev;
756 strcpy(t->name, "Tuner");
758 mutex_lock(&dev->lock);
760 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
762 mutex_unlock(&dev->lock);
766 static int vidioc_s_tuner(struct file *file, void *priv,
767 struct v4l2_tuner *t)
769 struct em28xx_fh *fh = priv;
770 struct em28xx *dev = fh->dev;
780 mutex_lock(&dev->lock);
782 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
784 mutex_unlock(&dev->lock);
788 static int vidioc_g_frequency(struct file *file, void *priv,
789 struct v4l2_frequency *f)
791 struct em28xx_fh *fh = priv;
792 struct em28xx *dev = fh->dev;
794 f->type = V4L2_TUNER_ANALOG_TV;
795 f->frequency = dev->ctl_freq;
800 static int vidioc_s_frequency(struct file *file, void *priv,
801 struct v4l2_frequency *f)
803 struct em28xx_fh *fh = priv;
804 struct em28xx *dev = fh->dev;
814 if (V4L2_TUNER_ANALOG_TV != f->type)
817 mutex_lock(&dev->lock);
819 dev->ctl_freq = f->frequency;
820 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
822 mutex_unlock(&dev->lock);
826 static int vidioc_cropcap(struct file *file, void *priv,
827 struct v4l2_cropcap *cc)
829 struct em28xx_fh *fh = priv;
830 struct em28xx *dev = fh->dev;
832 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
837 cc->bounds.width = dev->width;
838 cc->bounds.height = dev->height;
839 cc->defrect = cc->bounds;
840 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
841 cc->pixelaspect.denominator = 59;
846 static int vidioc_streamon(struct file *file, void *priv,
847 enum v4l2_buf_type type)
849 struct em28xx_fh *fh = priv;
850 struct em28xx *dev = fh->dev;
857 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
860 if (list_empty(&dev->inqueue))
863 mutex_lock(&dev->lock);
865 if (unlikely(res_get(fh) < 0)) {
866 mutex_unlock(&dev->lock);
870 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
872 mutex_unlock(&dev->lock);
876 static int vidioc_streamoff(struct file *file, void *priv,
877 enum v4l2_buf_type type)
879 struct em28xx_fh *fh = priv;
880 struct em28xx *dev = fh->dev;
887 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
890 mutex_lock(&dev->lock);
892 if (dev->stream == STREAM_ON) {
893 em28xx_videodbg("VIDIOC_STREAMOFF: interrupting stream\n");
894 rc = em28xx_stream_interrupt(dev);
896 mutex_unlock(&dev->lock);
901 em28xx_empty_framequeues(dev);
903 mutex_unlock(&dev->lock);
907 static int vidioc_querycap(struct file *file, void *priv,
908 struct v4l2_capability *cap)
910 struct em28xx_fh *fh = priv;
911 struct em28xx *dev = fh->dev;
913 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
914 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
915 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
917 cap->version = EM28XX_VERSION_CODE;
920 V4L2_CAP_SLICED_VBI_CAPTURE |
921 V4L2_CAP_VIDEO_CAPTURE |
923 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
926 cap->capabilities |= V4L2_CAP_TUNER;
931 static int vidioc_enum_fmt_cap(struct file *file, void *priv,
932 struct v4l2_fmtdesc *fmtd)
934 if (fmtd->index != 0)
937 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
938 strcpy(fmtd->description, "Packed YUY2");
939 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
940 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
945 /* Sliced VBI ioctls */
946 static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
947 struct v4l2_format *f)
949 struct em28xx_fh *fh = priv;
950 struct em28xx *dev = fh->dev;
957 mutex_lock(&dev->lock);
959 f->fmt.sliced.service_set = 0;
961 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
963 if (f->fmt.sliced.service_set == 0)
966 mutex_unlock(&dev->lock);
970 static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
971 struct v4l2_format *f)
973 struct em28xx_fh *fh = priv;
974 struct em28xx *dev = fh->dev;
981 mutex_lock(&dev->lock);
982 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
983 mutex_unlock(&dev->lock);
985 if (f->fmt.sliced.service_set == 0)
992 static int vidioc_reqbufs(struct file *file, void *priv,
993 struct v4l2_requestbuffers *rb)
995 struct em28xx_fh *fh = priv;
996 struct em28xx *dev = fh->dev;
1000 rc = check_dev(dev);
1004 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1005 rb->memory != V4L2_MEMORY_MMAP)
1008 if (dev->io == IO_READ) {
1009 em28xx_videodbg("method is set to read;"
1010 " close and open the device again to"
1011 " choose the mmap I/O method\n");
1015 for (i = 0; i < dev->num_frames; i++)
1016 if (dev->frame[i].vma_use_count) {
1017 em28xx_videodbg("VIDIOC_REQBUFS failed; "
1018 "previous buffers are still mapped\n");
1022 mutex_lock(&dev->lock);
1024 if (dev->stream == STREAM_ON) {
1025 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1026 rc = em28xx_stream_interrupt(dev);
1028 mutex_unlock(&dev->lock);
1033 em28xx_empty_framequeues(dev);
1035 em28xx_release_buffers(dev);
1037 rb->count = em28xx_request_buffers(dev, rb->count);
1039 dev->frame_current = NULL;
1040 dev->io = rb->count ? IO_MMAP : IO_NONE;
1042 mutex_unlock(&dev->lock);
1046 static int vidioc_querybuf(struct file *file, void *priv,
1047 struct v4l2_buffer *b)
1049 struct em28xx_fh *fh = priv;
1050 struct em28xx *dev = fh->dev;
1053 rc = check_dev(dev);
1057 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1058 b->index >= dev->num_frames || dev->io != IO_MMAP)
1061 mutex_lock(&dev->lock);
1063 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1065 if (dev->frame[b->index].vma_use_count)
1066 b->flags |= V4L2_BUF_FLAG_MAPPED;
1068 if (dev->frame[b->index].state == F_DONE)
1069 b->flags |= V4L2_BUF_FLAG_DONE;
1070 else if (dev->frame[b->index].state != F_UNUSED)
1071 b->flags |= V4L2_BUF_FLAG_QUEUED;
1073 mutex_unlock(&dev->lock);
1077 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1079 struct em28xx_fh *fh = priv;
1080 struct em28xx *dev = fh->dev;
1081 unsigned long lock_flags;
1084 rc = check_dev(dev);
1088 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP ||
1089 b->index >= dev->num_frames)
1092 if (dev->frame[b->index].state != F_UNUSED)
1095 dev->frame[b->index].state = F_QUEUED;
1097 /* add frame to fifo */
1098 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1099 list_add_tail(&dev->frame[b->index].frame, &dev->inqueue);
1100 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1105 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1107 struct em28xx_fh *fh = priv;
1108 struct em28xx *dev = fh->dev;
1110 struct em28xx_frame_t *f;
1111 unsigned long lock_flags;
1113 rc = check_dev(dev);
1117 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
1120 if (list_empty(&dev->outqueue)) {
1121 if (dev->stream == STREAM_OFF)
1124 if (file->f_flags & O_NONBLOCK)
1127 rc = wait_event_interruptible(dev->wait_frame,
1128 (!list_empty(&dev->outqueue)) ||
1129 (dev->state & DEV_DISCONNECTED));
1133 if (dev->state & DEV_DISCONNECTED)
1137 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1138 f = list_entry(dev->outqueue.next, struct em28xx_frame_t, frame);
1139 list_del(dev->outqueue.next);
1140 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1142 f->state = F_UNUSED;
1143 memcpy(b, &f->buf, sizeof(*b));
1145 if (f->vma_use_count)
1146 b->flags |= V4L2_BUF_FLAG_MAPPED;
1152 * em28xx_v4l2_open()
1153 * inits the device and starts isoc transfer
1155 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1157 int minor = iminor(inode);
1159 struct em28xx *h,*dev = NULL;
1160 struct em28xx_fh *fh;
1162 list_for_each_entry(h, &em28xx_devlist, devlist) {
1163 if (h->vdev->minor == minor) {
1165 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1167 if (h->vbi_dev->minor == minor) {
1169 dev->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1175 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1176 minor,v4l2_type_names[dev->type],dev->users);
1178 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1181 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1184 mutex_lock(&dev->lock);
1186 filp->private_data = fh;
1188 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1189 em28xx_set_alternate(dev);
1191 dev->width = norm_maxw(dev);
1192 dev->height = norm_maxh(dev);
1193 dev->frame_size = dev->width * dev->height * 2;
1194 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1195 dev->bytesperline = dev->width * 2;
1199 em28xx_capture_start(dev, 1);
1200 em28xx_resolution_set(dev);
1203 /* start the transfer */
1204 errCode = em28xx_init_isoc(dev);
1208 em28xx_empty_framequeues(dev);
1214 mutex_unlock(&dev->lock);
1219 * em28xx_realease_resources()
1220 * unregisters the v4l2,i2c and usb devices
1221 * called when the device gets disconected or at module unload
1223 static void em28xx_release_resources(struct em28xx *dev)
1226 /*FIXME: I2C IR should be disconnected */
1228 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1229 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1230 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
1231 list_del(&dev->devlist);
1232 video_unregister_device(dev->vdev);
1233 video_unregister_device(dev->vbi_dev);
1234 em28xx_i2c_unregister(dev);
1235 usb_put_dev(dev->udev);
1238 /* Mark device as unused */
1239 em28xx_devused&=~(1<<dev->devno);
1243 * em28xx_v4l2_close()
1244 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1246 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1248 struct em28xx_fh *fh = filp->private_data;
1249 struct em28xx *dev = fh->dev;
1252 em28xx_videodbg("users=%d\n", dev->users);
1258 mutex_lock(&dev->lock);
1260 if (dev->users == 1) {
1261 em28xx_uninit_isoc(dev);
1262 em28xx_release_buffers(dev);
1265 /* the device is already disconnect,
1266 free the remaining resources */
1267 if (dev->state & DEV_DISCONNECTED) {
1268 em28xx_release_resources(dev);
1269 mutex_unlock(&dev->lock);
1274 /* set alternate 0 */
1276 em28xx_videodbg("setting alternate 0\n");
1277 errCode = usb_set_interface(dev->udev, 0, 0);
1279 em28xx_errdev("cannot change alternate number to "
1280 "0 (error=%i)\n", errCode);
1285 wake_up_interruptible_nr(&dev->open, 1);
1286 mutex_unlock(&dev->lock);
1291 * em28xx_v4l2_read()
1292 * will allocate buffers when called for the first time
1295 em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
1298 struct em28xx_frame_t *f, *i;
1299 unsigned long lock_flags;
1301 struct em28xx_fh *fh = filp->private_data;
1302 struct em28xx *dev = fh->dev;
1304 /* FIXME: read() is not prepared to allow changing the video
1305 resolution while streaming. Seems a bug at em28xx_set_fmt
1308 if (unlikely(res_get(fh) < 0))
1311 mutex_lock(&dev->lock);
1313 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1314 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
1316 if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1317 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
1318 em28xx_videodbg("not supported yet! ...\n");
1319 if (copy_to_user(buf, "", 1)) {
1320 mutex_unlock(&dev->lock);
1323 mutex_unlock(&dev->lock);
1326 if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1327 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
1328 em28xx_videodbg("not supported yet! ...\n");
1329 if (copy_to_user(buf, "", 1)) {
1330 mutex_unlock(&dev->lock);
1333 mutex_unlock(&dev->lock);
1337 if (dev->state & DEV_DISCONNECTED) {
1338 em28xx_videodbg("device not present\n");
1339 mutex_unlock(&dev->lock);
1343 if (dev->state & DEV_MISCONFIGURED) {
1344 em28xx_videodbg("device misconfigured; close and open it again\n");
1345 mutex_unlock(&dev->lock);
1349 if (dev->io == IO_MMAP) {
1350 em28xx_videodbg ("IO method is set to mmap; close and open"
1351 " the device again to choose the read method\n");
1352 mutex_unlock(&dev->lock);
1356 if (dev->io == IO_NONE) {
1357 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
1358 em28xx_errdev("read failed, not enough memory\n");
1359 mutex_unlock(&dev->lock);
1363 dev->stream = STREAM_ON;
1364 em28xx_queue_unusedframes(dev);
1368 mutex_unlock(&dev->lock);
1372 if (list_empty(&dev->outqueue)) {
1373 if (filp->f_flags & O_NONBLOCK) {
1374 mutex_unlock(&dev->lock);
1377 ret = wait_event_interruptible
1379 (!list_empty(&dev->outqueue)) ||
1380 (dev->state & DEV_DISCONNECTED));
1382 mutex_unlock(&dev->lock);
1385 if (dev->state & DEV_DISCONNECTED) {
1386 mutex_unlock(&dev->lock);
1389 dev->video_bytesread = 0;
1392 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
1394 em28xx_queue_unusedframes(dev);
1396 if (count > f->buf.length)
1397 count = f->buf.length;
1399 if ((dev->video_bytesread + count) > dev->frame_size)
1400 count = dev->frame_size - dev->video_bytesread;
1402 if (copy_to_user(buf, f->bufmem+dev->video_bytesread, count)) {
1403 em28xx_err("Error while copying to user\n");
1406 dev->video_bytesread += count;
1408 if (dev->video_bytesread == dev->frame_size) {
1409 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1410 list_for_each_entry(i, &dev->outqueue, frame)
1411 i->state = F_UNUSED;
1412 INIT_LIST_HEAD(&dev->outqueue);
1413 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1415 em28xx_queue_unusedframes(dev);
1416 dev->video_bytesread = 0;
1421 mutex_unlock(&dev->lock);
1427 * em28xx_v4l2_poll()
1428 * will allocate buffers when called for the first time
1430 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
1432 unsigned int mask = 0;
1433 struct em28xx_fh *fh = filp->private_data;
1434 struct em28xx *dev = fh->dev;
1436 if (unlikely(res_get(fh) < 0))
1439 mutex_lock(&dev->lock);
1441 if (dev->state & DEV_DISCONNECTED) {
1442 em28xx_videodbg("device not present\n");
1443 } else if (dev->state & DEV_MISCONFIGURED) {
1444 em28xx_videodbg("device is misconfigured; close and open it again\n");
1446 if (dev->io == IO_NONE) {
1447 if (!em28xx_request_buffers
1448 (dev, EM28XX_NUM_READ_FRAMES)) {
1450 ("poll() failed, not enough memory\n");
1453 dev->stream = STREAM_ON;
1457 if (dev->io == IO_READ) {
1458 em28xx_queue_unusedframes(dev);
1459 poll_wait(filp, &dev->wait_frame, wait);
1461 if (!list_empty(&dev->outqueue))
1462 mask |= POLLIN | POLLRDNORM;
1464 mutex_unlock(&dev->lock);
1470 mutex_unlock(&dev->lock);
1475 * em28xx_v4l2_mmap()
1477 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1479 struct em28xx_fh *fh = filp->private_data;
1480 struct em28xx *dev = fh->dev;
1481 unsigned long size = vma->vm_end - vma->vm_start;
1482 unsigned long start = vma->vm_start;
1486 if (unlikely(res_get(fh) < 0))
1489 mutex_lock(&dev->lock);
1491 if (dev->state & DEV_DISCONNECTED) {
1492 em28xx_videodbg("mmap: device not present\n");
1493 mutex_unlock(&dev->lock);
1497 if (dev->state & DEV_MISCONFIGURED) {
1498 em28xx_videodbg ("mmap: Device is misconfigured; close and "
1500 mutex_unlock(&dev->lock);
1504 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE)) {
1505 mutex_unlock(&dev->lock);
1509 if (size > PAGE_ALIGN(dev->frame[0].buf.length))
1510 size = PAGE_ALIGN(dev->frame[0].buf.length);
1512 for (i = 0; i < dev->num_frames; i++) {
1513 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1516 if (i == dev->num_frames) {
1517 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
1518 mutex_unlock(&dev->lock);
1522 /* VM_IO is eventually going to replace PageReserved altogether */
1523 vma->vm_flags |= VM_IO;
1524 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1526 pos = dev->frame[i].bufmem;
1527 while (size > 0) { /* size is page-aligned */
1528 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1529 em28xx_videodbg("mmap: vm_insert_page failed\n");
1530 mutex_unlock(&dev->lock);
1538 vma->vm_ops = &em28xx_vm_ops;
1539 vma->vm_private_data = &dev->frame[i];
1541 em28xx_vm_open(vma);
1542 mutex_unlock(&dev->lock);
1546 static const struct file_operations em28xx_v4l_fops = {
1547 .owner = THIS_MODULE,
1548 .open = em28xx_v4l2_open,
1549 .release = em28xx_v4l2_close,
1550 .read = em28xx_v4l2_read,
1551 .poll = em28xx_v4l2_poll,
1552 .mmap = em28xx_v4l2_mmap,
1553 .ioctl = video_ioctl2,
1554 .llseek = no_llseek,
1555 .compat_ioctl = v4l_compat_ioctl32,
1558 static const struct video_device em28xx_video_template = {
1559 .fops = &em28xx_v4l_fops,
1560 .release = video_device_release,
1563 .vidioc_querycap = vidioc_querycap,
1564 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1565 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1566 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1567 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1568 .vidioc_g_audio = vidioc_g_audio,
1569 .vidioc_s_audio = vidioc_s_audio,
1570 .vidioc_cropcap = vidioc_cropcap,
1572 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1573 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1574 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1576 .vidioc_reqbufs = vidioc_reqbufs,
1577 .vidioc_querybuf = vidioc_querybuf,
1578 .vidioc_qbuf = vidioc_qbuf,
1579 .vidioc_dqbuf = vidioc_dqbuf,
1580 .vidioc_s_std = vidioc_s_std,
1581 .vidioc_enum_input = vidioc_enum_input,
1582 .vidioc_g_input = vidioc_g_input,
1583 .vidioc_s_input = vidioc_s_input,
1584 .vidioc_queryctrl = vidioc_queryctrl,
1585 .vidioc_g_ctrl = vidioc_g_ctrl,
1586 .vidioc_s_ctrl = vidioc_s_ctrl,
1587 .vidioc_streamon = vidioc_streamon,
1588 .vidioc_streamoff = vidioc_streamoff,
1589 .vidioc_g_tuner = vidioc_g_tuner,
1590 .vidioc_s_tuner = vidioc_s_tuner,
1591 .vidioc_g_frequency = vidioc_g_frequency,
1592 .vidioc_s_frequency = vidioc_s_frequency,
1594 .tvnorms = V4L2_STD_ALL,
1595 .current_norm = V4L2_STD_PAL,
1598 /******************************** usb interface *****************************************/
1601 static LIST_HEAD(em28xx_extension_devlist);
1602 static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1604 int em28xx_register_extension(struct em28xx_ops *ops)
1606 struct em28xx *h, *dev = NULL;
1608 list_for_each_entry(h, &em28xx_devlist, devlist)
1611 mutex_lock(&em28xx_extension_devlist_lock);
1612 list_add_tail(&ops->next, &em28xx_extension_devlist);
1616 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1617 mutex_unlock(&em28xx_extension_devlist_lock);
1621 EXPORT_SYMBOL(em28xx_register_extension);
1623 void em28xx_unregister_extension(struct em28xx_ops *ops)
1625 struct em28xx *h, *dev = NULL;
1627 list_for_each_entry(h, &em28xx_devlist, devlist)
1633 mutex_lock(&em28xx_extension_devlist_lock);
1634 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1635 list_del(&ops->next);
1636 mutex_unlock(&em28xx_extension_devlist_lock);
1638 EXPORT_SYMBOL(em28xx_unregister_extension);
1642 * allocates and inits the device structs, registers i2c bus and v4l device
1644 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
1647 struct em28xx_ops *ops = NULL;
1648 struct em28xx *dev = *devhandle;
1649 int retval = -ENOMEM;
1651 unsigned int maxh, maxw;
1654 mutex_init(&dev->lock);
1655 spin_lock_init(&dev->queue_lock);
1656 init_waitqueue_head(&dev->open);
1657 init_waitqueue_head(&dev->wait_frame);
1658 init_waitqueue_head(&dev->wait_stream);
1660 dev->em28xx_write_regs = em28xx_write_regs;
1661 dev->em28xx_read_reg = em28xx_read_reg;
1662 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1663 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1664 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1665 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
1667 errCode = em28xx_read_reg(dev, CHIPID_REG);
1669 em28xx_info("em28xx chip ID = %d\n", errCode);
1671 em28xx_pre_card_setup(dev);
1673 errCode = em28xx_config(dev);
1675 em28xx_errdev("error configuring device\n");
1676 em28xx_devused &= ~(1<<dev->devno);
1681 /* register i2c bus */
1682 em28xx_i2c_register(dev);
1684 /* Do board specific init and eeprom reading */
1685 em28xx_card_setup(dev);
1687 /* Configure audio */
1688 em28xx_audio_analog_set(dev);
1690 /* configure the device */
1691 em28xx_config_i2c(dev);
1693 /* set default norm */
1694 dev->norm = em28xx_video_template.current_norm;
1696 maxw = norm_maxw(dev);
1697 maxh = norm_maxh(dev);
1699 /* set default image size */
1702 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1703 dev->field_size = dev->width * dev->height;
1705 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1706 dev->bytesperline = dev->width * 2;
1711 errCode = em28xx_config(dev);
1713 /* allocate and fill video video_device struct */
1714 dev->vdev = video_device_alloc();
1715 if (NULL == dev->vdev) {
1716 em28xx_errdev("cannot allocate video_device.\n");
1717 em28xx_devused&=~(1<<dev->devno);
1721 memcpy(dev->vdev, &em28xx_video_template,
1722 sizeof(em28xx_video_template));
1723 dev->vdev->type = VID_TYPE_CAPTURE;
1725 dev->vdev->type |= VID_TYPE_TUNER;
1726 dev->vdev->dev = &dev->udev->dev;
1727 snprintf(dev->vdev->name, sizeof(dev->vbi_dev->name),
1728 "%s#%d %s", "em28xx", dev->devno, "video");
1730 /* Allocate and fill vbi video_device struct */
1731 dev->vbi_dev = video_device_alloc();
1732 if (NULL == dev->vbi_dev) {
1733 em28xx_errdev("cannot allocate video_device.\n");
1735 em28xx_devused&=~(1<<dev->devno);
1739 memcpy(dev->vbi_dev, &em28xx_video_template,
1740 sizeof(em28xx_video_template));
1741 dev->vbi_dev->type = VFL_TYPE_VBI;
1742 dev->vbi_dev->dev = &dev->udev->dev;
1743 snprintf(dev->vbi_dev->name, sizeof(dev->vbi_dev->name),
1744 "%s#%d %s", "em28xx", dev->devno, "vbi");
1746 list_add_tail(&dev->devlist,&em28xx_devlist);
1748 if (dev->has_msp34xx) {
1749 /* Send a reset to other chips via gpio */
1750 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1752 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1758 /* register v4l2 video video_device */
1759 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1760 video_nr[dev->devno]))) {
1761 em28xx_errdev("unable to register video device (error=%i).\n",
1763 mutex_unlock(&dev->lock);
1764 list_del(&dev->devlist);
1765 video_device_release(dev->vdev);
1766 em28xx_devused&=~(1<<dev->devno);
1771 /* register v4l2 vbi video_device */
1772 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1773 vbi_nr[dev->devno]) < 0) {
1774 printk("unable to register vbi device\n");
1775 mutex_unlock(&dev->lock);
1776 list_del(&dev->devlist);
1777 video_device_release(dev->vbi_dev);
1778 video_device_release(dev->vdev);
1779 em28xx_devused&=~(1<<dev->devno);
1784 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1785 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1786 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
1788 mutex_lock(&em28xx_extension_devlist_lock);
1789 if (!list_empty(&em28xx_extension_devlist)) {
1790 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1795 mutex_unlock(&em28xx_extension_devlist_lock);
1800 #if defined(CONFIG_MODULES) && defined(MODULE)
1801 static void request_module_async(struct work_struct *work)
1803 struct em28xx *dev = container_of(work,
1804 struct em28xx, request_module_wk);
1806 if (!dev->has_audio_class)
1807 request_module("em28xx-alsa");
1810 static void request_modules(struct em28xx *dev)
1812 INIT_WORK(&dev->request_module_wk, request_module_async);
1813 schedule_work(&dev->request_module_wk);
1816 #define request_modules(dev)
1817 #endif /* CONFIG_MODULES */
1820 * em28xx_usb_probe()
1821 * checks for supported devices
1823 static int em28xx_usb_probe(struct usb_interface *interface,
1824 const struct usb_device_id *id)
1826 const struct usb_endpoint_descriptor *endpoint;
1827 struct usb_device *udev;
1828 struct usb_interface *uif;
1829 struct em28xx *dev = NULL;
1830 int retval = -ENODEV;
1833 udev = usb_get_dev(interface_to_usbdev(interface));
1834 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1836 /* Check to see next free device and mark as used */
1837 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
1838 em28xx_devused|=1<<nr;
1840 /* Don't register audio interfaces */
1841 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
1842 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
1843 udev->descriptor.idVendor,udev->descriptor.idProduct,
1845 interface->altsetting[0].desc.bInterfaceClass);
1847 em28xx_devused&=~(1<<nr);
1851 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
1852 udev->descriptor.idVendor,udev->descriptor.idProduct,
1854 interface->altsetting[0].desc.bInterfaceClass);
1856 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1858 /* check if the device has the iso in endpoint at the correct place */
1859 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1860 USB_ENDPOINT_XFER_ISOC) {
1861 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
1862 em28xx_devused&=~(1<<nr);
1865 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1866 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
1867 em28xx_devused&=~(1<<nr);
1871 if (nr >= EM28XX_MAXBOARDS) {
1872 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
1873 em28xx_devused&=~(1<<nr);
1877 /* allocate memory for our device state and initialize it */
1878 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1880 em28xx_err(DRIVER_NAME ": out of memory!\n");
1881 em28xx_devused&=~(1<<nr);
1885 snprintf(dev->name, 29, "em28xx #%d", nr);
1887 dev->model = id->driver_info;
1889 /* Checks if audio is provided by some interface */
1890 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
1891 uif = udev->config->interface[i];
1892 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
1893 dev->has_audio_class = 1;
1898 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
1899 dev->has_audio_class ? "Has" : "Doesn't have");
1901 /* compute alternate max packet sizes */
1902 uif = udev->actconfig->interface[0];
1904 dev->num_alt=uif->num_altsetting;
1905 em28xx_info("Alternate settings: %i\n",dev->num_alt);
1906 // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1907 dev->alt_max_pkt_size = kmalloc(32*
1908 dev->num_alt,GFP_KERNEL);
1909 if (dev->alt_max_pkt_size == NULL) {
1910 em28xx_errdev("out of memory!\n");
1911 em28xx_devused&=~(1<<nr);
1916 for (i = 0; i < dev->num_alt ; i++) {
1917 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1919 dev->alt_max_pkt_size[i] =
1920 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1921 em28xx_info("Alternate setting %i, max size= %i\n",i,
1922 dev->alt_max_pkt_size[i]);
1925 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
1926 dev->model = card[nr];
1928 /* allocate device struct */
1929 retval = em28xx_init_dev(&dev, udev, nr);
1933 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
1935 /* save our data pointer in this interface device */
1936 usb_set_intfdata(interface, dev);
1938 request_modules(dev);
1944 * em28xx_usb_disconnect()
1945 * called when the device gets diconencted
1946 * video device will be unregistered on v4l2_close in case it is still open
1948 static void em28xx_usb_disconnect(struct usb_interface *interface)
1951 struct em28xx_ops *ops = NULL;
1953 dev = usb_get_intfdata(interface);
1954 usb_set_intfdata(interface, NULL);
1959 em28xx_info("disconnecting %s\n", dev->vdev->name);
1961 /* wait until all current v4l2 io is finished then deallocate resources */
1962 mutex_lock(&dev->lock);
1964 wake_up_interruptible_all(&dev->open);
1968 ("device /dev/video%d is open! Deregistration and memory "
1969 "deallocation are deferred on close.\n",
1970 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
1972 dev->state |= DEV_MISCONFIGURED;
1973 em28xx_uninit_isoc(dev);
1974 dev->state |= DEV_DISCONNECTED;
1975 wake_up_interruptible(&dev->wait_frame);
1976 wake_up_interruptible(&dev->wait_stream);
1978 dev->state |= DEV_DISCONNECTED;
1979 em28xx_release_resources(dev);
1981 mutex_unlock(&dev->lock);
1983 mutex_lock(&em28xx_extension_devlist_lock);
1984 if (!list_empty(&em28xx_extension_devlist)) {
1985 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1989 mutex_unlock(&em28xx_extension_devlist_lock);
1992 kfree(dev->alt_max_pkt_size);
1997 static struct usb_driver em28xx_usb_driver = {
1999 .probe = em28xx_usb_probe,
2000 .disconnect = em28xx_usb_disconnect,
2001 .id_table = em28xx_id_table,
2004 static int __init em28xx_module_init(void)
2008 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
2009 (EM28XX_VERSION_CODE >> 16) & 0xff,
2010 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2012 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2013 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2016 /* register this driver with the USB subsystem */
2017 result = usb_register(&em28xx_usb_driver);
2019 em28xx_err(DRIVER_NAME
2020 " usb_register failed. Error number %d.\n", result);
2025 static void __exit em28xx_module_exit(void)
2027 /* deregister this driver with the USB subsystem */
2028 usb_deregister(&em28xx_usb_driver);
2031 module_init(em28xx_module_init);
2032 module_exit(em28xx_module_exit);