Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / media / video / em28xx / em28xx-video.c
blob5fe8ad2898ccd712237f1f06fe5f27fe56a93554
1 /*
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>
35 #include <linux/mm.h>
36 #include <linux/mutex.h>
38 #include "em28xx.h"
39 #include <media/v4l2-common.h>
40 #include <media/msp3400.h>
41 #include <media/tuner.h>
43 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
44 "Markus Rechberger <mrechberger@gmail.com>, " \
45 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
46 "Sascha Sommer <saschasommer@freenet.de>"
48 #define DRIVER_NAME "em28xx"
49 #define DRIVER_DESC "Empia em28xx based USB video device driver"
50 #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
52 #define em28xx_videodbg(fmt, arg...) do {\
53 if (video_debug) \
54 printk(KERN_INFO "%s %s :"fmt, \
55 dev->name, __FUNCTION__ , ##arg); } while (0)
57 MODULE_AUTHOR(DRIVER_AUTHOR);
58 MODULE_DESCRIPTION(DRIVER_DESC);
59 MODULE_LICENSE("GPL");
61 static LIST_HEAD(em28xx_devlist);
63 static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
64 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
65 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
66 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
68 module_param_array(card, int, NULL, 0444);
69 module_param_array(video_nr, int, NULL, 0444);
70 module_param_array(vbi_nr, int, NULL, 0444);
71 module_param_array(radio_nr, int, NULL, 0444);
72 MODULE_PARM_DESC(card, "card type");
73 MODULE_PARM_DESC(video_nr, "video device numbers");
74 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
75 MODULE_PARM_DESC(radio_nr, "radio device numbers");
77 static unsigned int video_debug = 0;
78 module_param(video_debug,int,0644);
79 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
81 /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
82 static unsigned long em28xx_devused;
84 /* supported controls */
85 /* Common to all boards */
86 static struct v4l2_queryctrl em28xx_qctrl[] = {
88 .id = V4L2_CID_AUDIO_VOLUME,
89 .type = V4L2_CTRL_TYPE_INTEGER,
90 .name = "Volume",
91 .minimum = 0x0,
92 .maximum = 0x1f,
93 .step = 0x1,
94 .default_value = 0x1f,
95 .flags = 0,
96 },{
97 .id = V4L2_CID_AUDIO_MUTE,
98 .type = V4L2_CTRL_TYPE_BOOLEAN,
99 .name = "Mute",
100 .minimum = 0,
101 .maximum = 1,
102 .step = 1,
103 .default_value = 1,
104 .flags = 0,
108 static struct usb_driver em28xx_usb_driver;
111 /********************* v4l2 interface ******************************************/
114 * em28xx_config()
115 * inits registers with sane defaults
117 static int em28xx_config(struct em28xx *dev)
120 /* Sets I2C speed to 100 KHz */
121 if (!dev->is_em2800)
122 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
124 /* enable vbi capturing */
126 /* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
127 /* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
128 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
130 dev->mute = 1; /* maybe not the right place... */
131 dev->volume = 0x1f;
133 em28xx_outfmt_set_yuv422(dev);
134 em28xx_colorlevels_set_default(dev);
135 em28xx_compression_disable(dev);
137 return 0;
141 * em28xx_config_i2c()
142 * configure i2c attached devices
144 static void em28xx_config_i2c(struct em28xx *dev)
146 struct v4l2_routing route;
148 route.input = INPUT(dev->ctl_input)->vmux;
149 route.output = 0;
150 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
151 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
152 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
156 * em28xx_empty_framequeues()
157 * prepare queues for incoming and outgoing frames
159 static void em28xx_empty_framequeues(struct em28xx *dev)
161 u32 i;
163 INIT_LIST_HEAD(&dev->inqueue);
164 INIT_LIST_HEAD(&dev->outqueue);
166 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
167 dev->frame[i].state = F_UNUSED;
168 dev->frame[i].buf.bytesused = 0;
172 static void video_mux(struct em28xx *dev, int index)
174 struct v4l2_routing route;
176 route.input = INPUT(index)->vmux;
177 route.output = 0;
178 dev->ctl_input = index;
179 dev->ctl_ainput = INPUT(index)->amux;
181 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
183 if (dev->has_msp34xx) {
184 if (dev->i2s_speed)
185 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
186 route.input = dev->ctl_ainput;
187 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
188 /* Note: this is msp3400 specific */
189 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
192 <<<<<<< HEAD:drivers/media/video/em28xx/em28xx-video.c
193 em28xx_set_audio_source(dev);
194 =======
195 em28xx_audio_analog_set(dev);
196 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/em28xx/em28xx-video.c
199 /* Usage lock check functions */
200 static int res_get(struct em28xx_fh *fh)
202 struct em28xx *dev = fh->dev;
203 int rc = 0;
205 /* This instance already has stream_on */
206 if (fh->stream_on)
207 return rc;
209 mutex_lock(&dev->lock);
211 if (dev->stream_on)
212 rc = -EINVAL;
213 else {
214 dev->stream_on = 1;
215 fh->stream_on = 1;
218 mutex_unlock(&dev->lock);
219 return rc;
222 static int res_check(struct em28xx_fh *fh)
224 return (fh->stream_on);
227 static void res_free(struct em28xx_fh *fh)
229 struct em28xx *dev = fh->dev;
231 mutex_lock(&dev->lock);
232 fh->stream_on = 0;
233 dev->stream_on = 0;
234 mutex_unlock(&dev->lock);
238 * em28xx_vm_open()
240 static void em28xx_vm_open(struct vm_area_struct *vma)
242 struct em28xx_frame_t *f = vma->vm_private_data;
243 f->vma_use_count++;
247 * em28xx_vm_close()
249 static void em28xx_vm_close(struct vm_area_struct *vma)
251 /* NOTE: buffers are not freed here */
252 struct em28xx_frame_t *f = vma->vm_private_data;
254 if (f->vma_use_count)
255 f->vma_use_count--;
258 static struct vm_operations_struct em28xx_vm_ops = {
259 .open = em28xx_vm_open,
260 .close = em28xx_vm_close,
265 * em28xx_get_ctrl()
266 * return the current saturation, brightness or contrast, mute state
268 static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
270 switch (ctrl->id) {
271 case V4L2_CID_AUDIO_MUTE:
272 ctrl->value = dev->mute;
273 return 0;
274 case V4L2_CID_AUDIO_VOLUME:
275 ctrl->value = dev->volume;
276 return 0;
277 default:
278 return -EINVAL;
283 * em28xx_set_ctrl()
284 * mute or set new saturation, brightness or contrast
286 static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
288 switch (ctrl->id) {
289 case V4L2_CID_AUDIO_MUTE:
290 if (ctrl->value != dev->mute) {
291 dev->mute = ctrl->value;
292 return em28xx_audio_analog_set(dev);
294 return 0;
295 case V4L2_CID_AUDIO_VOLUME:
296 dev->volume = ctrl->value;
297 return em28xx_audio_analog_set(dev);
298 default:
299 return -EINVAL;
304 * em28xx_stream_interrupt()
305 * stops streaming
307 static int em28xx_stream_interrupt(struct em28xx *dev)
309 int rc = 0;
311 /* stop reading from the device */
313 dev->stream = STREAM_INTERRUPT;
314 rc = wait_event_timeout(dev->wait_stream,
315 (dev->stream == STREAM_OFF) ||
316 (dev->state & DEV_DISCONNECTED),
317 EM28XX_URB_TIMEOUT);
319 if (rc) {
320 dev->state |= DEV_MISCONFIGURED;
321 em28xx_videodbg("device is misconfigured; close and "
322 "open /dev/video%d again\n",
323 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
324 return rc;
327 return 0;
331 static int check_dev(struct em28xx *dev)
333 if (dev->state & DEV_DISCONNECTED) {
334 em28xx_errdev("v4l2 ioctl: device not present\n");
335 return -ENODEV;
338 if (dev->state & DEV_MISCONFIGURED) {
339 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
340 "close and open it again\n");
341 return -EIO;
343 return 0;
346 static void get_scale(struct em28xx *dev,
347 unsigned int width, unsigned int height,
348 unsigned int *hscale, unsigned int *vscale)
350 unsigned int maxw = norm_maxw(dev);
351 unsigned int maxh = norm_maxh(dev);
353 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
354 if (*hscale >= 0x4000)
355 *hscale = 0x3fff;
357 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
358 if (*vscale >= 0x4000)
359 *vscale = 0x3fff;
362 /* ------------------------------------------------------------------
363 IOCTL vidioc handling
364 ------------------------------------------------------------------*/
366 static int vidioc_g_fmt_cap(struct file *file, void *priv,
367 struct v4l2_format *f)
369 struct em28xx_fh *fh = priv;
370 struct em28xx *dev = fh->dev;
372 mutex_lock(&dev->lock);
374 f->fmt.pix.width = dev->width;
375 f->fmt.pix.height = dev->height;
376 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
377 f->fmt.pix.bytesperline = dev->bytesperline;
378 f->fmt.pix.sizeimage = dev->frame_size;
379 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
381 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
382 f->fmt.pix.field = dev->interlaced ?
383 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
385 mutex_unlock(&dev->lock);
386 return 0;
389 static int vidioc_try_fmt_cap(struct file *file, void *priv,
390 struct v4l2_format *f)
392 struct em28xx_fh *fh = priv;
393 struct em28xx *dev = fh->dev;
394 int width = f->fmt.pix.width;
395 int height = f->fmt.pix.height;
396 unsigned int maxw = norm_maxw(dev);
397 unsigned int maxh = norm_maxh(dev);
398 unsigned int hscale, vscale;
400 /* width must even because of the YUYV format
401 height must be even because of interlacing */
402 height &= 0xfffe;
403 width &= 0xfffe;
405 if (height < 32)
406 height = 32;
407 if (height > maxh)
408 height = maxh;
409 if (width < 48)
410 width = 48;
411 if (width > maxw)
412 width = maxw;
414 mutex_lock(&dev->lock);
416 if (dev->is_em2800) {
417 /* the em2800 can only scale down to 50% */
418 if (height % (maxh / 2))
419 height = maxh;
420 if (width % (maxw / 2))
421 width = maxw;
422 /* according to empiatech support */
423 /* the MaxPacketSize is to small to support */
424 /* framesizes larger than 640x480 @ 30 fps */
425 /* or 640x576 @ 25 fps. As this would cut */
426 /* of a part of the image we prefer */
427 /* 360x576 or 360x480 for now */
428 if (width == maxw && height == maxh)
429 width /= 2;
432 get_scale(dev, width, height, &hscale, &vscale);
434 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
435 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
437 f->fmt.pix.width = width;
438 f->fmt.pix.height = height;
439 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
440 f->fmt.pix.bytesperline = width * 2;
441 f->fmt.pix.sizeimage = width * 2 * height;
442 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
443 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
445 mutex_unlock(&dev->lock);
446 return 0;
449 static int vidioc_s_fmt_cap(struct file *file, void *priv,
450 struct v4l2_format *f)
452 struct em28xx_fh *fh = priv;
453 struct em28xx *dev = fh->dev;
454 int rc, i;
456 rc = check_dev(dev);
457 if (rc < 0)
458 return rc;
460 vidioc_try_fmt_cap(file, priv, f);
462 mutex_lock(&dev->lock);
464 for (i = 0; i < dev->num_frames; i++)
465 if (dev->frame[i].vma_use_count) {
466 em28xx_videodbg("VIDIOC_S_FMT failed. "
467 "Unmap the buffers first.\n");
468 rc = -EINVAL;
469 goto err;
472 /* stop io in case it is already in progress */
473 if (dev->stream == STREAM_ON) {
474 em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n");
475 rc = em28xx_stream_interrupt(dev);
476 if (rc < 0)
477 goto err;
480 em28xx_release_buffers(dev);
481 dev->io = IO_NONE;
483 /* set new image size */
484 dev->width = f->fmt.pix.width;
485 dev->height = f->fmt.pix.height;
486 dev->frame_size = dev->width * dev->height * 2;
487 dev->field_size = dev->frame_size >> 1;
488 dev->bytesperline = dev->width * 2;
489 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
491 /* FIXME: This is really weird! Why capture is starting with
492 this ioctl ???
494 em28xx_uninit_isoc(dev);
495 em28xx_set_alternate(dev);
496 em28xx_capture_start(dev, 1);
497 em28xx_resolution_set(dev);
498 em28xx_init_isoc(dev);
499 rc = 0;
501 err:
502 mutex_unlock(&dev->lock);
503 return rc;
506 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
508 struct em28xx_fh *fh = priv;
509 struct em28xx *dev = fh->dev;
510 struct v4l2_format f;
511 int rc;
513 rc = check_dev(dev);
514 if (rc < 0)
515 return rc;
517 mutex_lock(&dev->lock);
518 dev->norm = *norm;
519 mutex_unlock(&dev->lock);
521 /* Adjusts width/height, if needed */
522 f.fmt.pix.width = dev->width;
523 f.fmt.pix.height = dev->height;
524 vidioc_try_fmt_cap(file, priv, &f);
526 mutex_lock(&dev->lock);
528 /* set new image size */
529 dev->width = f.fmt.pix.width;
530 dev->height = f.fmt.pix.height;
531 dev->frame_size = dev->width * dev->height * 2;
532 dev->field_size = dev->frame_size >> 1;
533 dev->bytesperline = dev->width * 2;
534 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
536 em28xx_resolution_set(dev);
537 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
539 mutex_unlock(&dev->lock);
540 return 0;
543 static const char *iname[] = {
544 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
545 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
546 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
547 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
548 [EM28XX_VMUX_SVIDEO] = "S-Video",
549 [EM28XX_VMUX_TELEVISION] = "Television",
550 [EM28XX_VMUX_CABLE] = "Cable TV",
551 [EM28XX_VMUX_DVB] = "DVB",
552 [EM28XX_VMUX_DEBUG] = "for debug only",
555 static int vidioc_enum_input(struct file *file, void *priv,
556 struct v4l2_input *i)
558 struct em28xx_fh *fh = priv;
559 struct em28xx *dev = fh->dev;
560 unsigned int n;
562 n = i->index;
563 if (n >= MAX_EM28XX_INPUT)
564 return -EINVAL;
565 if (0 == INPUT(n)->type)
566 return -EINVAL;
568 i->index = n;
569 i->type = V4L2_INPUT_TYPE_CAMERA;
571 strcpy(i->name, iname[INPUT(n)->type]);
573 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
574 (EM28XX_VMUX_CABLE == INPUT(n)->type))
575 i->type = V4L2_INPUT_TYPE_TUNER;
577 i->std = dev->vdev->tvnorms;
579 return 0;
582 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
584 struct em28xx_fh *fh = priv;
585 struct em28xx *dev = fh->dev;
587 *i = dev->ctl_input;
589 return 0;
592 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
594 struct em28xx_fh *fh = priv;
595 struct em28xx *dev = fh->dev;
596 int rc;
598 rc = check_dev(dev);
599 if (rc < 0)
600 return rc;
602 if (i >= MAX_EM28XX_INPUT)
603 return -EINVAL;
604 if (0 == INPUT(i)->type)
605 return -EINVAL;
607 mutex_lock(&dev->lock);
609 video_mux(dev, i);
611 mutex_unlock(&dev->lock);
612 return 0;
615 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
617 struct em28xx_fh *fh = priv;
618 struct em28xx *dev = fh->dev;
619 unsigned int index = a->index;
621 if (a->index > 1)
622 return -EINVAL;
624 index = dev->ctl_ainput;
626 if (index == 0) {
627 strcpy(a->name, "Television");
628 } else {
629 strcpy(a->name, "Line In");
631 a->capability = V4L2_AUDCAP_STEREO;
632 a->index = index;
634 return 0;
637 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
639 struct em28xx_fh *fh = priv;
640 struct em28xx *dev = fh->dev;
642 if (a->index != dev->ctl_ainput)
643 return -EINVAL;
645 return 0;
648 static int vidioc_queryctrl(struct file *file, void *priv,
649 struct v4l2_queryctrl *qc)
651 struct em28xx_fh *fh = priv;
652 struct em28xx *dev = fh->dev;
653 int id = qc->id;
654 int i;
655 int rc;
657 rc = check_dev(dev);
658 if (rc < 0)
659 return rc;
661 memset(qc, 0, sizeof(*qc));
663 qc->id = id;
665 if (!dev->has_msp34xx) {
666 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
667 if (qc->id && qc->id == em28xx_qctrl[i].id) {
668 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
669 return 0;
673 mutex_lock(&dev->lock);
674 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
675 mutex_unlock(&dev->lock);
677 if (qc->type)
678 return 0;
679 else
680 return -EINVAL;
683 static int vidioc_g_ctrl(struct file *file, void *priv,
684 struct v4l2_control *ctrl)
686 struct em28xx_fh *fh = priv;
687 struct em28xx *dev = fh->dev;
688 int rc;
690 rc = check_dev(dev);
691 if (rc < 0)
692 return rc;
693 mutex_lock(&dev->lock);
695 if (!dev->has_msp34xx)
696 rc = em28xx_get_ctrl(dev, ctrl);
697 else
698 rc = -EINVAL;
700 if (rc == -EINVAL) {
701 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
702 rc = 0;
705 mutex_unlock(&dev->lock);
706 return rc;
709 static int vidioc_s_ctrl(struct file *file, void *priv,
710 struct v4l2_control *ctrl)
712 struct em28xx_fh *fh = priv;
713 struct em28xx *dev = fh->dev;
714 u8 i;
715 int rc;
717 rc = check_dev(dev);
718 if (rc < 0)
719 return rc;
721 mutex_lock(&dev->lock);
723 if (dev->has_msp34xx)
724 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
725 else {
726 rc = 1;
727 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
728 if (ctrl->id == em28xx_qctrl[i].id) {
729 if (ctrl->value < em28xx_qctrl[i].minimum ||
730 ctrl->value > em28xx_qctrl[i].maximum) {
731 rc = -ERANGE;
732 break;
735 rc = em28xx_set_ctrl(dev, ctrl);
736 break;
741 /* Control not found - try to send it to the attached devices */
742 if (rc == 1) {
743 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
744 rc = 0;
747 mutex_unlock(&dev->lock);
748 return rc;
751 static int vidioc_g_tuner(struct file *file, void *priv,
752 struct v4l2_tuner *t)
754 struct em28xx_fh *fh = priv;
755 struct em28xx *dev = fh->dev;
756 int rc;
758 rc = check_dev(dev);
759 if (rc < 0)
760 return rc;
762 if (0 != t->index)
763 return -EINVAL;
765 strcpy(t->name, "Tuner");
767 mutex_lock(&dev->lock);
769 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
771 mutex_unlock(&dev->lock);
772 return 0;
775 static int vidioc_s_tuner(struct file *file, void *priv,
776 struct v4l2_tuner *t)
778 struct em28xx_fh *fh = priv;
779 struct em28xx *dev = fh->dev;
780 int rc;
782 rc = check_dev(dev);
783 if (rc < 0)
784 return rc;
786 if (0 != t->index)
787 return -EINVAL;
789 mutex_lock(&dev->lock);
791 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
793 mutex_unlock(&dev->lock);
794 return 0;
797 static int vidioc_g_frequency(struct file *file, void *priv,
798 struct v4l2_frequency *f)
800 struct em28xx_fh *fh = priv;
801 struct em28xx *dev = fh->dev;
803 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
804 f->frequency = dev->ctl_freq;
806 return 0;
809 static int vidioc_s_frequency(struct file *file, void *priv,
810 struct v4l2_frequency *f)
812 struct em28xx_fh *fh = priv;
813 struct em28xx *dev = fh->dev;
814 int rc;
816 rc = check_dev(dev);
817 if (rc < 0)
818 return rc;
820 if (0 != f->tuner)
821 return -EINVAL;
823 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
824 return -EINVAL;
825 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
826 return -EINVAL;
828 mutex_lock(&dev->lock);
830 dev->ctl_freq = f->frequency;
831 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
833 mutex_unlock(&dev->lock);
834 return 0;
837 <<<<<<< HEAD:drivers/media/video/em28xx/em28xx-video.c
838 =======
839 #ifdef CONFIG_VIDEO_ADV_DEBUG
840 static int em28xx_reg_len(int reg)
842 switch (reg) {
843 case AC97LSB_REG:
844 case HSCALELOW_REG:
845 case VSCALELOW_REG:
846 return 2;
847 default:
848 return 1;
852 static int vidioc_g_register(struct file *file, void *priv,
853 struct v4l2_register *reg)
855 struct em28xx_fh *fh = priv;
856 struct em28xx *dev = fh->dev;
857 int ret;
859 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
860 return -EINVAL;
862 if (em28xx_reg_len(reg->reg) == 1) {
863 ret = em28xx_read_reg(dev, reg->reg);
864 if (ret < 0)
865 return ret;
867 reg->val = ret;
868 } else {
869 u64 val = 0;
870 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
871 reg->reg, (char *)&val, 2);
872 if (ret < 0)
873 return ret;
875 reg->val = cpu_to_le64((__u64)val);
878 return 0;
881 static int vidioc_s_register(struct file *file, void *priv,
882 struct v4l2_register *reg)
884 struct em28xx_fh *fh = priv;
885 struct em28xx *dev = fh->dev;
886 u64 buf;
888 buf = le64_to_cpu((__u64)reg->val);
890 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
891 em28xx_reg_len(reg->reg));
893 #endif
896 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/em28xx/em28xx-video.c
897 static int vidioc_cropcap(struct file *file, void *priv,
898 struct v4l2_cropcap *cc)
900 struct em28xx_fh *fh = priv;
901 struct em28xx *dev = fh->dev;
903 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
904 return -EINVAL;
906 cc->bounds.left = 0;
907 cc->bounds.top = 0;
908 cc->bounds.width = dev->width;
909 cc->bounds.height = dev->height;
910 cc->defrect = cc->bounds;
911 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
912 cc->pixelaspect.denominator = 59;
914 return 0;
917 static int vidioc_streamon(struct file *file, void *priv,
918 enum v4l2_buf_type type)
920 struct em28xx_fh *fh = priv;
921 struct em28xx *dev = fh->dev;
922 int rc;
924 rc = check_dev(dev);
925 if (rc < 0)
926 return rc;
928 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
929 return -EINVAL;
931 if (list_empty(&dev->inqueue))
932 return -EINVAL;
934 mutex_lock(&dev->lock);
936 if (unlikely(res_get(fh) < 0)) {
937 mutex_unlock(&dev->lock);
938 return -EBUSY;
941 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
943 mutex_unlock(&dev->lock);
944 return 0;
947 static int vidioc_streamoff(struct file *file, void *priv,
948 enum v4l2_buf_type type)
950 struct em28xx_fh *fh = priv;
951 struct em28xx *dev = fh->dev;
952 int rc;
954 rc = check_dev(dev);
955 if (rc < 0)
956 return rc;
958 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
959 return -EINVAL;
961 mutex_lock(&dev->lock);
963 if (dev->stream == STREAM_ON) {
964 em28xx_videodbg("VIDIOC_STREAMOFF: interrupting stream\n");
965 rc = em28xx_stream_interrupt(dev);
966 if (rc < 0) {
967 mutex_unlock(&dev->lock);
968 return rc;
972 em28xx_empty_framequeues(dev);
974 mutex_unlock(&dev->lock);
975 return 0;
978 static int vidioc_querycap(struct file *file, void *priv,
979 struct v4l2_capability *cap)
981 struct em28xx_fh *fh = priv;
982 struct em28xx *dev = fh->dev;
984 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
985 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
986 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
988 cap->version = EM28XX_VERSION_CODE;
990 cap->capabilities =
991 V4L2_CAP_SLICED_VBI_CAPTURE |
992 V4L2_CAP_VIDEO_CAPTURE |
993 V4L2_CAP_AUDIO |
994 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
996 if (dev->tuner_type != TUNER_ABSENT)
997 cap->capabilities |= V4L2_CAP_TUNER;
999 return 0;
1002 static int vidioc_enum_fmt_cap(struct file *file, void *priv,
1003 struct v4l2_fmtdesc *fmtd)
1005 if (fmtd->index != 0)
1006 return -EINVAL;
1008 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1009 strcpy(fmtd->description, "Packed YUY2");
1010 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1011 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1013 return 0;
1016 /* Sliced VBI ioctls */
1017 static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
1018 struct v4l2_format *f)
1020 struct em28xx_fh *fh = priv;
1021 struct em28xx *dev = fh->dev;
1022 int rc;
1024 rc = check_dev(dev);
1025 if (rc < 0)
1026 return rc;
1028 mutex_lock(&dev->lock);
1030 f->fmt.sliced.service_set = 0;
1032 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1034 if (f->fmt.sliced.service_set == 0)
1035 rc = -EINVAL;
1037 mutex_unlock(&dev->lock);
1038 return rc;
1041 static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
1042 struct v4l2_format *f)
1044 struct em28xx_fh *fh = priv;
1045 struct em28xx *dev = fh->dev;
1046 int rc;
1048 rc = check_dev(dev);
1049 if (rc < 0)
1050 return rc;
1052 mutex_lock(&dev->lock);
1053 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1054 mutex_unlock(&dev->lock);
1056 if (f->fmt.sliced.service_set == 0)
1057 return -EINVAL;
1059 return 0;
1063 static int vidioc_reqbufs(struct file *file, void *priv,
1064 struct v4l2_requestbuffers *rb)
1066 struct em28xx_fh *fh = priv;
1067 struct em28xx *dev = fh->dev;
1068 u32 i;
1069 int rc;
1071 rc = check_dev(dev);
1072 if (rc < 0)
1073 return rc;
1075 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1076 rb->memory != V4L2_MEMORY_MMAP)
1077 return -EINVAL;
1079 if (dev->io == IO_READ) {
1080 em28xx_videodbg("method is set to read;"
1081 " close and open the device again to"
1082 " choose the mmap I/O method\n");
1083 return -EINVAL;
1086 for (i = 0; i < dev->num_frames; i++)
1087 if (dev->frame[i].vma_use_count) {
1088 em28xx_videodbg("VIDIOC_REQBUFS failed; "
1089 "previous buffers are still mapped\n");
1090 return -EINVAL;
1093 mutex_lock(&dev->lock);
1095 if (dev->stream == STREAM_ON) {
1096 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1097 rc = em28xx_stream_interrupt(dev);
1098 if (rc < 0) {
1099 mutex_unlock(&dev->lock);
1100 return rc;
1104 em28xx_empty_framequeues(dev);
1106 em28xx_release_buffers(dev);
1107 if (rb->count)
1108 rb->count = em28xx_request_buffers(dev, rb->count);
1110 dev->frame_current = NULL;
1111 dev->io = rb->count ? IO_MMAP : IO_NONE;
1113 mutex_unlock(&dev->lock);
1114 return 0;
1117 static int vidioc_querybuf(struct file *file, void *priv,
1118 struct v4l2_buffer *b)
1120 struct em28xx_fh *fh = priv;
1121 struct em28xx *dev = fh->dev;
1122 int rc;
1124 rc = check_dev(dev);
1125 if (rc < 0)
1126 return rc;
1128 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1129 b->index >= dev->num_frames || dev->io != IO_MMAP)
1130 return -EINVAL;
1132 mutex_lock(&dev->lock);
1134 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1136 if (dev->frame[b->index].vma_use_count)
1137 b->flags |= V4L2_BUF_FLAG_MAPPED;
1139 if (dev->frame[b->index].state == F_DONE)
1140 b->flags |= V4L2_BUF_FLAG_DONE;
1141 else if (dev->frame[b->index].state != F_UNUSED)
1142 b->flags |= V4L2_BUF_FLAG_QUEUED;
1144 mutex_unlock(&dev->lock);
1145 return 0;
1148 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1150 struct em28xx_fh *fh = priv;
1151 struct em28xx *dev = fh->dev;
1152 unsigned long lock_flags;
1153 int rc;
1155 rc = check_dev(dev);
1156 if (rc < 0)
1157 return rc;
1159 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP ||
1160 b->index >= dev->num_frames)
1161 return -EINVAL;
1163 if (dev->frame[b->index].state != F_UNUSED)
1164 return -EAGAIN;
1166 dev->frame[b->index].state = F_QUEUED;
1168 /* add frame to fifo */
1169 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1170 list_add_tail(&dev->frame[b->index].frame, &dev->inqueue);
1171 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1173 return 0;
1176 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1178 struct em28xx_fh *fh = priv;
1179 struct em28xx *dev = fh->dev;
1180 int rc;
1181 struct em28xx_frame_t *f;
1182 unsigned long lock_flags;
1184 rc = check_dev(dev);
1185 if (rc < 0)
1186 return rc;
1188 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
1189 return -EINVAL;
1191 if (list_empty(&dev->outqueue)) {
1192 if (dev->stream == STREAM_OFF)
1193 return -EINVAL;
1195 if (file->f_flags & O_NONBLOCK)
1196 return -EAGAIN;
1198 rc = wait_event_interruptible(dev->wait_frame,
1199 (!list_empty(&dev->outqueue)) ||
1200 (dev->state & DEV_DISCONNECTED));
1201 if (rc)
1202 return rc;
1204 if (dev->state & DEV_DISCONNECTED)
1205 return -ENODEV;
1208 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1209 f = list_entry(dev->outqueue.next, struct em28xx_frame_t, frame);
1210 list_del(dev->outqueue.next);
1211 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1213 f->state = F_UNUSED;
1214 memcpy(b, &f->buf, sizeof(*b));
1216 if (f->vma_use_count)
1217 b->flags |= V4L2_BUF_FLAG_MAPPED;
1219 return 0;
1222 /* ----------------------------------------------------------- */
1223 /* RADIO ESPECIFIC IOCTLS */
1224 /* ----------------------------------------------------------- */
1226 static int radio_querycap(struct file *file, void *priv,
1227 struct v4l2_capability *cap)
1229 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1231 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1232 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1233 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1235 cap->version = EM28XX_VERSION_CODE;
1236 cap->capabilities = V4L2_CAP_TUNER;
1237 return 0;
1240 static int radio_g_tuner(struct file *file, void *priv,
1241 struct v4l2_tuner *t)
1243 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1245 if (unlikely(t->index > 0))
1246 return -EINVAL;
1248 strcpy(t->name, "Radio");
1249 t->type = V4L2_TUNER_RADIO;
1251 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1252 return 0;
1255 static int radio_enum_input(struct file *file, void *priv,
1256 struct v4l2_input *i)
1258 if (i->index != 0)
1259 return -EINVAL;
1260 strcpy(i->name, "Radio");
1261 i->type = V4L2_INPUT_TYPE_TUNER;
1263 return 0;
1266 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1268 if (unlikely(a->index))
1269 return -EINVAL;
1271 strcpy(a->name, "Radio");
1272 return 0;
1275 static int radio_s_tuner(struct file *file, void *priv,
1276 struct v4l2_tuner *t)
1278 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1280 if (0 != t->index)
1281 return -EINVAL;
1283 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1285 return 0;
1288 static int radio_s_audio(struct file *file, void *fh,
1289 struct v4l2_audio *a)
1291 return 0;
1294 static int radio_s_input(struct file *file, void *fh, unsigned int i)
1296 return 0;
1299 static int radio_queryctrl(struct file *file, void *priv,
1300 struct v4l2_queryctrl *qc)
1302 int i;
1304 if (qc->id < V4L2_CID_BASE ||
1305 qc->id >= V4L2_CID_LASTP1)
1306 return -EINVAL;
1308 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1309 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1310 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1311 return 0;
1315 return -EINVAL;
1319 * em28xx_v4l2_open()
1320 * inits the device and starts isoc transfer
1322 static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1324 int minor = iminor(inode);
1325 int errCode = 0, radio = 0;
1326 struct em28xx *h,*dev = NULL;
1327 struct em28xx_fh *fh;
1329 list_for_each_entry(h, &em28xx_devlist, devlist) {
1330 if (h->vdev->minor == minor) {
1331 dev = h;
1332 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1334 if (h->vbi_dev->minor == minor) {
1335 dev = h;
1336 dev->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1338 if (h->radio_dev &&
1339 h->radio_dev->minor == minor) {
1340 radio = 1;
1341 dev = h;
1344 if (NULL == dev)
1345 return -ENODEV;
1347 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1348 minor,v4l2_type_names[dev->type],dev->users);
1350 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1352 if (!fh) {
1353 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1354 return -ENOMEM;
1356 mutex_lock(&dev->lock);
1357 fh->dev = dev;
1358 fh->radio = radio;
1359 filp->private_data = fh;
1361 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1362 <<<<<<< HEAD:drivers/media/video/em28xx/em28xx-video.c
1363 em28xx_set_alternate(dev);
1365 =======
1366 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/em28xx/em28xx-video.c
1367 dev->width = norm_maxw(dev);
1368 dev->height = norm_maxh(dev);
1369 dev->frame_size = dev->width * dev->height * 2;
1370 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1371 dev->bytesperline = dev->width * 2;
1372 dev->hscale = 0;
1373 dev->vscale = 0;
1375 <<<<<<< HEAD:drivers/media/video/em28xx/em28xx-video.c
1376 =======
1377 em28xx_set_alternate(dev);
1378 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/em28xx/em28xx-video.c
1379 em28xx_capture_start(dev, 1);
1380 em28xx_resolution_set(dev);
1383 /* start the transfer */
1384 errCode = em28xx_init_isoc(dev);
1385 if (errCode)
1386 goto err;
1388 em28xx_empty_framequeues(dev);
1390 if (fh->radio) {
1391 em28xx_videodbg("video_open: setting radio device\n");
1392 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1395 dev->users++;
1397 err:
1398 mutex_unlock(&dev->lock);
1399 return errCode;
1403 * em28xx_realease_resources()
1404 * unregisters the v4l2,i2c and usb devices
1405 * called when the device gets disconected or at module unload
1407 static void em28xx_release_resources(struct em28xx *dev)
1410 /*FIXME: I2C IR should be disconnected */
1412 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1413 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1414 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
1415 list_del(&dev->devlist);
1416 if (dev->radio_dev) {
1417 if (-1 != dev->radio_dev->minor)
1418 video_unregister_device(dev->radio_dev);
1419 else
1420 video_device_release(dev->radio_dev);
1421 dev->radio_dev = NULL;
1423 if (dev->vbi_dev) {
1424 if (-1 != dev->vbi_dev->minor)
1425 video_unregister_device(dev->vbi_dev);
1426 else
1427 video_device_release(dev->vbi_dev);
1428 dev->vbi_dev = NULL;
1430 if (dev->vdev) {
1431 if (-1 != dev->vdev->minor)
1432 video_unregister_device(dev->vdev);
1433 else
1434 video_device_release(dev->vdev);
1435 dev->vdev = NULL;
1437 em28xx_i2c_unregister(dev);
1438 usb_put_dev(dev->udev);
1440 /* Mark device as unused */
1441 em28xx_devused&=~(1<<dev->devno);
1445 * em28xx_v4l2_close()
1446 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1448 static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1450 struct em28xx_fh *fh = filp->private_data;
1451 struct em28xx *dev = fh->dev;
1452 int errCode;
1454 em28xx_videodbg("users=%d\n", dev->users);
1457 if (res_check(fh))
1458 res_free(fh);
1460 mutex_lock(&dev->lock);
1462 if (dev->users == 1) {
1463 em28xx_uninit_isoc(dev);
1464 em28xx_release_buffers(dev);
1465 dev->io = IO_NONE;
1467 /* the device is already disconnect,
1468 free the remaining resources */
1469 if (dev->state & DEV_DISCONNECTED) {
1470 em28xx_release_resources(dev);
1471 mutex_unlock(&dev->lock);
1472 kfree(dev);
1473 return 0;
1476 /* set alternate 0 */
1477 dev->alt = 0;
1478 em28xx_videodbg("setting alternate 0\n");
1479 errCode = usb_set_interface(dev->udev, 0, 0);
1480 if (errCode < 0) {
1481 em28xx_errdev("cannot change alternate number to "
1482 "0 (error=%i)\n", errCode);
1485 kfree(fh);
1486 dev->users--;
1487 wake_up_interruptible_nr(&dev->open, 1);
1488 mutex_unlock(&dev->lock);
1489 return 0;
1493 * em28xx_v4l2_read()
1494 * will allocate buffers when called for the first time
1496 static ssize_t
1497 em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
1498 loff_t * f_pos)
1500 struct em28xx_frame_t *f, *i;
1501 unsigned long lock_flags;
1502 int ret = 0;
1503 struct em28xx_fh *fh = filp->private_data;
1504 struct em28xx *dev = fh->dev;
1506 /* FIXME: read() is not prepared to allow changing the video
1507 resolution while streaming. Seems a bug at em28xx_set_fmt
1510 if (unlikely(res_get(fh) < 0))
1511 return -EBUSY;
1513 mutex_lock(&dev->lock);
1515 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1516 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
1518 if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1519 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
1520 em28xx_videodbg("not supported yet! ...\n");
1521 if (copy_to_user(buf, "", 1)) {
1522 mutex_unlock(&dev->lock);
1523 return -EFAULT;
1525 mutex_unlock(&dev->lock);
1526 return (1);
1528 if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1529 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
1530 em28xx_videodbg("not supported yet! ...\n");
1531 if (copy_to_user(buf, "", 1)) {
1532 mutex_unlock(&dev->lock);
1533 return -EFAULT;
1535 mutex_unlock(&dev->lock);
1536 return (1);
1539 if (dev->state & DEV_DISCONNECTED) {
1540 em28xx_videodbg("device not present\n");
1541 mutex_unlock(&dev->lock);
1542 return -ENODEV;
1545 if (dev->state & DEV_MISCONFIGURED) {
1546 em28xx_videodbg("device misconfigured; close and open it again\n");
1547 mutex_unlock(&dev->lock);
1548 return -EIO;
1551 if (dev->io == IO_MMAP) {
1552 em28xx_videodbg ("IO method is set to mmap; close and open"
1553 " the device again to choose the read method\n");
1554 mutex_unlock(&dev->lock);
1555 return -EINVAL;
1558 if (dev->io == IO_NONE) {
1559 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
1560 em28xx_errdev("read failed, not enough memory\n");
1561 mutex_unlock(&dev->lock);
1562 return -ENOMEM;
1564 dev->io = IO_READ;
1565 dev->stream = STREAM_ON;
1566 em28xx_queue_unusedframes(dev);
1569 if (!count) {
1570 mutex_unlock(&dev->lock);
1571 return 0;
1574 if (list_empty(&dev->outqueue)) {
1575 if (filp->f_flags & O_NONBLOCK) {
1576 mutex_unlock(&dev->lock);
1577 return -EAGAIN;
1579 ret = wait_event_interruptible
1580 (dev->wait_frame,
1581 (!list_empty(&dev->outqueue)) ||
1582 (dev->state & DEV_DISCONNECTED));
1583 if (ret) {
1584 mutex_unlock(&dev->lock);
1585 return ret;
1587 if (dev->state & DEV_DISCONNECTED) {
1588 mutex_unlock(&dev->lock);
1589 return -ENODEV;
1591 dev->video_bytesread = 0;
1594 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
1596 em28xx_queue_unusedframes(dev);
1598 if (count > f->buf.length)
1599 count = f->buf.length;
1601 if ((dev->video_bytesread + count) > dev->frame_size)
1602 count = dev->frame_size - dev->video_bytesread;
1604 if (copy_to_user(buf, f->bufmem+dev->video_bytesread, count)) {
1605 em28xx_err("Error while copying to user\n");
1606 return -EFAULT;
1608 dev->video_bytesread += count;
1610 if (dev->video_bytesread == dev->frame_size) {
1611 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1612 list_for_each_entry(i, &dev->outqueue, frame)
1613 i->state = F_UNUSED;
1614 INIT_LIST_HEAD(&dev->outqueue);
1615 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1617 em28xx_queue_unusedframes(dev);
1618 dev->video_bytesread = 0;
1621 *f_pos += count;
1623 mutex_unlock(&dev->lock);
1625 return count;
1629 * em28xx_v4l2_poll()
1630 * will allocate buffers when called for the first time
1632 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
1634 unsigned int mask = 0;
1635 struct em28xx_fh *fh = filp->private_data;
1636 struct em28xx *dev = fh->dev;
1638 if (unlikely(res_get(fh) < 0))
1639 return POLLERR;
1641 mutex_lock(&dev->lock);
1643 if (dev->state & DEV_DISCONNECTED) {
1644 em28xx_videodbg("device not present\n");
1645 } else if (dev->state & DEV_MISCONFIGURED) {
1646 em28xx_videodbg("device is misconfigured; close and open it again\n");
1647 } else {
1648 if (dev->io == IO_NONE) {
1649 if (!em28xx_request_buffers
1650 (dev, EM28XX_NUM_READ_FRAMES)) {
1651 em28xx_warn
1652 ("poll() failed, not enough memory\n");
1653 } else {
1654 dev->io = IO_READ;
1655 dev->stream = STREAM_ON;
1659 if (dev->io == IO_READ) {
1660 em28xx_queue_unusedframes(dev);
1661 poll_wait(filp, &dev->wait_frame, wait);
1663 if (!list_empty(&dev->outqueue))
1664 mask |= POLLIN | POLLRDNORM;
1666 mutex_unlock(&dev->lock);
1668 return mask;
1672 mutex_unlock(&dev->lock);
1673 return POLLERR;
1677 * em28xx_v4l2_mmap()
1679 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1681 struct em28xx_fh *fh = filp->private_data;
1682 struct em28xx *dev = fh->dev;
1683 unsigned long size = vma->vm_end - vma->vm_start;
1684 unsigned long start = vma->vm_start;
1685 void *pos;
1686 u32 i;
1688 if (unlikely(res_get(fh) < 0))
1689 return -EBUSY;
1691 mutex_lock(&dev->lock);
1693 if (dev->state & DEV_DISCONNECTED) {
1694 em28xx_videodbg("mmap: device not present\n");
1695 mutex_unlock(&dev->lock);
1696 return -ENODEV;
1699 if (dev->state & DEV_MISCONFIGURED) {
1700 em28xx_videodbg ("mmap: Device is misconfigured; close and "
1701 "open it again\n");
1702 mutex_unlock(&dev->lock);
1703 return -EIO;
1706 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE)) {
1707 mutex_unlock(&dev->lock);
1708 return -EINVAL;
1711 if (size > PAGE_ALIGN(dev->frame[0].buf.length))
1712 size = PAGE_ALIGN(dev->frame[0].buf.length);
1714 for (i = 0; i < dev->num_frames; i++) {
1715 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1716 break;
1718 if (i == dev->num_frames) {
1719 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
1720 mutex_unlock(&dev->lock);
1721 return -EINVAL;
1724 /* VM_IO is eventually going to replace PageReserved altogether */
1725 vma->vm_flags |= VM_IO;
1726 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1728 pos = dev->frame[i].bufmem;
1729 while (size > 0) { /* size is page-aligned */
1730 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1731 em28xx_videodbg("mmap: vm_insert_page failed\n");
1732 mutex_unlock(&dev->lock);
1733 return -EAGAIN;
1735 start += PAGE_SIZE;
1736 pos += PAGE_SIZE;
1737 size -= PAGE_SIZE;
1740 vma->vm_ops = &em28xx_vm_ops;
1741 vma->vm_private_data = &dev->frame[i];
1743 em28xx_vm_open(vma);
1744 mutex_unlock(&dev->lock);
1745 return 0;
1748 static const struct file_operations em28xx_v4l_fops = {
1749 .owner = THIS_MODULE,
1750 .open = em28xx_v4l2_open,
1751 .release = em28xx_v4l2_close,
1752 .read = em28xx_v4l2_read,
1753 .poll = em28xx_v4l2_poll,
1754 .mmap = em28xx_v4l2_mmap,
1755 .ioctl = video_ioctl2,
1756 .llseek = no_llseek,
1757 .compat_ioctl = v4l_compat_ioctl32,
1760 static const struct file_operations radio_fops = {
1761 .owner = THIS_MODULE,
1762 .open = em28xx_v4l2_open,
1763 .release = em28xx_v4l2_close,
1764 .ioctl = video_ioctl2,
1765 .compat_ioctl = v4l_compat_ioctl32,
1766 .llseek = no_llseek,
1769 static const struct video_device em28xx_video_template = {
1770 .fops = &em28xx_v4l_fops,
1771 .release = video_device_release,
1773 .minor = -1,
1774 .vidioc_querycap = vidioc_querycap,
1775 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1776 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1777 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1778 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1779 .vidioc_g_audio = vidioc_g_audio,
1780 .vidioc_s_audio = vidioc_s_audio,
1781 .vidioc_cropcap = vidioc_cropcap,
1783 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1784 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1785 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1787 .vidioc_reqbufs = vidioc_reqbufs,
1788 .vidioc_querybuf = vidioc_querybuf,
1789 .vidioc_qbuf = vidioc_qbuf,
1790 .vidioc_dqbuf = vidioc_dqbuf,
1791 .vidioc_s_std = vidioc_s_std,
1792 .vidioc_enum_input = vidioc_enum_input,
1793 .vidioc_g_input = vidioc_g_input,
1794 .vidioc_s_input = vidioc_s_input,
1795 .vidioc_queryctrl = vidioc_queryctrl,
1796 .vidioc_g_ctrl = vidioc_g_ctrl,
1797 .vidioc_s_ctrl = vidioc_s_ctrl,
1798 .vidioc_streamon = vidioc_streamon,
1799 .vidioc_streamoff = vidioc_streamoff,
1800 .vidioc_g_tuner = vidioc_g_tuner,
1801 .vidioc_s_tuner = vidioc_s_tuner,
1802 .vidioc_g_frequency = vidioc_g_frequency,
1803 .vidioc_s_frequency = vidioc_s_frequency,
1804 <<<<<<< HEAD:drivers/media/video/em28xx/em28xx-video.c
1805 =======
1806 #ifdef CONFIG_VIDEO_ADV_DEBUG
1807 .vidioc_g_register = vidioc_g_register,
1808 .vidioc_s_register = vidioc_s_register,
1809 #endif
1810 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/em28xx/em28xx-video.c
1812 .tvnorms = V4L2_STD_ALL,
1813 .current_norm = V4L2_STD_PAL,
1816 static struct video_device em28xx_radio_template = {
1817 .name = "em28xx-radio",
1818 .type = VID_TYPE_TUNER,
1819 .fops = &radio_fops,
1820 .minor = -1,
1821 .vidioc_querycap = radio_querycap,
1822 .vidioc_g_tuner = radio_g_tuner,
1823 .vidioc_enum_input = radio_enum_input,
1824 .vidioc_g_audio = radio_g_audio,
1825 .vidioc_s_tuner = radio_s_tuner,
1826 .vidioc_s_audio = radio_s_audio,
1827 .vidioc_s_input = radio_s_input,
1828 .vidioc_queryctrl = radio_queryctrl,
1829 .vidioc_g_ctrl = vidioc_g_ctrl,
1830 .vidioc_s_ctrl = vidioc_s_ctrl,
1831 .vidioc_g_frequency = vidioc_g_frequency,
1832 .vidioc_s_frequency = vidioc_s_frequency,
1833 <<<<<<< HEAD:drivers/media/video/em28xx/em28xx-video.c
1834 =======
1835 #ifdef CONFIG_VIDEO_ADV_DEBUG
1836 .vidioc_g_register = vidioc_g_register,
1837 .vidioc_s_register = vidioc_s_register,
1838 #endif
1839 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/em28xx/em28xx-video.c
1842 /******************************** usb interface *****************************************/
1845 static LIST_HEAD(em28xx_extension_devlist);
1846 static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1848 int em28xx_register_extension(struct em28xx_ops *ops)
1850 struct em28xx *h, *dev = NULL;
1852 list_for_each_entry(h, &em28xx_devlist, devlist)
1853 dev = h;
1855 mutex_lock(&em28xx_extension_devlist_lock);
1856 list_add_tail(&ops->next, &em28xx_extension_devlist);
1857 if (dev)
1858 ops->init(dev);
1860 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1861 mutex_unlock(&em28xx_extension_devlist_lock);
1863 return 0;
1865 EXPORT_SYMBOL(em28xx_register_extension);
1867 void em28xx_unregister_extension(struct em28xx_ops *ops)
1869 struct em28xx *h, *dev = NULL;
1871 list_for_each_entry(h, &em28xx_devlist, devlist)
1872 dev = h;
1874 if (dev)
1875 ops->fini(dev);
1877 mutex_lock(&em28xx_extension_devlist_lock);
1878 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1879 list_del(&ops->next);
1880 mutex_unlock(&em28xx_extension_devlist_lock);
1882 EXPORT_SYMBOL(em28xx_unregister_extension);
1884 <<<<<<< HEAD:drivers/media/video/em28xx/em28xx-video.c
1885 struct video_device *em28xx_vdev_init(struct em28xx *dev,
1886 const struct video_device *template,
1887 const int type,
1888 const char *type_name)
1889 =======
1890 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1891 const struct video_device *template,
1892 const int type,
1893 const char *type_name)
1894 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/em28xx/em28xx-video.c
1896 struct video_device *vfd;
1898 vfd = video_device_alloc();
1899 if (NULL == vfd)
1900 return NULL;
1901 *vfd = *template;
1902 vfd->minor = -1;
1903 vfd->dev = &dev->udev->dev;
1904 vfd->release = video_device_release;
1905 vfd->type = type;
1907 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1908 dev->name, type_name);
1910 return vfd;
1915 * em28xx_init_dev()
1916 * allocates and inits the device structs, registers i2c bus and v4l device
1918 static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
1919 int minor)
1921 struct em28xx_ops *ops = NULL;
1922 struct em28xx *dev = *devhandle;
1923 int retval = -ENOMEM;
1924 int errCode;
1925 unsigned int maxh, maxw;
1927 dev->udev = udev;
1928 mutex_init(&dev->lock);
1929 spin_lock_init(&dev->queue_lock);
1930 init_waitqueue_head(&dev->open);
1931 init_waitqueue_head(&dev->wait_frame);
1932 init_waitqueue_head(&dev->wait_stream);
1934 dev->em28xx_write_regs = em28xx_write_regs;
1935 dev->em28xx_read_reg = em28xx_read_reg;
1936 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1937 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1938 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1939 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
1941 errCode = em28xx_read_reg(dev, CHIPID_REG);
1942 if (errCode >= 0)
1943 em28xx_info("em28xx chip ID = %d\n", errCode);
1945 em28xx_pre_card_setup(dev);
1947 errCode = em28xx_config(dev);
1948 if (errCode) {
1949 em28xx_errdev("error configuring device\n");
1950 em28xx_devused &= ~(1<<dev->devno);
1951 kfree(dev);
1952 return -ENOMEM;
1955 /* register i2c bus */
1956 em28xx_i2c_register(dev);
1958 /* Do board specific init and eeprom reading */
1959 em28xx_card_setup(dev);
1961 /* Configure audio */
1962 em28xx_audio_analog_set(dev);
1964 /* configure the device */
1965 em28xx_config_i2c(dev);
1967 /* set default norm */
1968 dev->norm = em28xx_video_template.current_norm;
1970 maxw = norm_maxw(dev);
1971 maxh = norm_maxh(dev);
1973 /* set default image size */
1974 dev->width = maxw;
1975 dev->height = maxh;
1976 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1977 dev->field_size = dev->width * dev->height;
1978 dev->frame_size =
1979 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1980 dev->bytesperline = dev->width * 2;
1981 dev->hscale = 0;
1982 dev->vscale = 0;
1983 dev->ctl_input = 2;
1985 errCode = em28xx_config(dev);
1987 list_add_tail(&dev->devlist, &em28xx_devlist);
1989 /* allocate and fill video video_device struct */
1990 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
1991 VID_TYPE_CAPTURE, "video");
1992 if (NULL == dev->vdev) {
1993 em28xx_errdev("cannot allocate video_device.\n");
1994 goto fail_unreg;
1996 if (dev->tuner_type != TUNER_ABSENT)
1997 dev->vdev->type |= VID_TYPE_TUNER;
1999 /* register v4l2 video video_device */
2000 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2001 video_nr[dev->devno]);
2002 if (retval) {
2003 em28xx_errdev("unable to register video device (error=%i).\n",
2004 retval);
2005 goto fail_unreg;
2008 /* Allocate and fill vbi video_device struct */
2009 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2010 VFL_TYPE_VBI, "vbi");
2011 /* register v4l2 vbi video_device */
2012 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2013 vbi_nr[dev->devno]) < 0) {
2014 em28xx_errdev("unable to register vbi device\n");
2015 retval = -ENODEV;
2016 goto fail_unreg;
2019 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2020 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2021 VFL_TYPE_RADIO, "radio");
2022 if (NULL == dev->radio_dev) {
2023 em28xx_errdev("cannot allocate video_device.\n");
2024 goto fail_unreg;
2026 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2027 radio_nr[dev->devno]);
2028 if (retval < 0) {
2029 em28xx_errdev("can't register radio device\n");
2030 goto fail_unreg;
2032 em28xx_info("Registered radio device as /dev/radio%d\n",
2033 dev->radio_dev->minor & 0x1f);
2037 if (dev->has_msp34xx) {
2038 /* Send a reset to other chips via gpio */
2039 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2040 msleep(3);
2041 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2042 msleep(3);
2045 video_mux(dev, 0);
2047 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2048 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2049 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
2051 mutex_lock(&em28xx_extension_devlist_lock);
2052 if (!list_empty(&em28xx_extension_devlist)) {
2053 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2054 if (ops->id)
2055 ops->init(dev);
2058 mutex_unlock(&em28xx_extension_devlist_lock);
2060 return 0;
2062 fail_unreg:
2063 em28xx_release_resources(dev);
2064 mutex_unlock(&dev->lock);
2065 kfree(dev);
2066 return retval;
2069 #if defined(CONFIG_MODULES) && defined(MODULE)
2070 static void request_module_async(struct work_struct *work)
2072 struct em28xx *dev = container_of(work,
2073 struct em28xx, request_module_wk);
2075 if (dev->has_audio_class)
2076 request_module("snd-usb-audio");
2077 else
2078 request_module("em28xx-alsa");
2081 static void request_modules(struct em28xx *dev)
2083 INIT_WORK(&dev->request_module_wk, request_module_async);
2084 schedule_work(&dev->request_module_wk);
2086 #else
2087 #define request_modules(dev)
2088 #endif /* CONFIG_MODULES */
2091 * em28xx_usb_probe()
2092 * checks for supported devices
2094 static int em28xx_usb_probe(struct usb_interface *interface,
2095 const struct usb_device_id *id)
2097 const struct usb_endpoint_descriptor *endpoint;
2098 struct usb_device *udev;
2099 struct usb_interface *uif;
2100 struct em28xx *dev = NULL;
2101 int retval = -ENODEV;
2102 int i, nr, ifnum;
2104 udev = usb_get_dev(interface_to_usbdev(interface));
2105 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2107 /* Check to see next free device and mark as used */
2108 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
2109 em28xx_devused|=1<<nr;
2111 /* Don't register audio interfaces */
2112 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2113 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
2114 udev->descriptor.idVendor,udev->descriptor.idProduct,
2115 ifnum,
2116 interface->altsetting[0].desc.bInterfaceClass);
2118 em28xx_devused&=~(1<<nr);
2119 return -ENODEV;
2122 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
2123 udev->descriptor.idVendor,udev->descriptor.idProduct,
2124 ifnum,
2125 interface->altsetting[0].desc.bInterfaceClass);
2127 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2129 /* check if the device has the iso in endpoint at the correct place */
2130 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2131 USB_ENDPOINT_XFER_ISOC) {
2132 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
2133 em28xx_devused&=~(1<<nr);
2134 return -ENODEV;
2136 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
2137 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
2138 em28xx_devused&=~(1<<nr);
2139 return -ENODEV;
2142 if (nr >= EM28XX_MAXBOARDS) {
2143 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
2144 em28xx_devused&=~(1<<nr);
2145 return -ENOMEM;
2148 /* allocate memory for our device state and initialize it */
2149 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2150 if (dev == NULL) {
2151 em28xx_err(DRIVER_NAME ": out of memory!\n");
2152 em28xx_devused&=~(1<<nr);
2153 return -ENOMEM;
2156 snprintf(dev->name, 29, "em28xx #%d", nr);
2157 dev->devno = nr;
2158 dev->model = id->driver_info;
2159 <<<<<<< HEAD:drivers/media/video/em28xx/em28xx-video.c
2160 =======
2161 dev->alt = -1;
2162 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/em28xx/em28xx-video.c
2164 /* Checks if audio is provided by some interface */
2165 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2166 uif = udev->config->interface[i];
2167 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2168 dev->has_audio_class = 1;
2169 break;
2173 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2174 dev->has_audio_class ? "Has" : "Doesn't have");
2176 /* compute alternate max packet sizes */
2177 uif = udev->actconfig->interface[0];
2179 dev->num_alt=uif->num_altsetting;
2180 em28xx_info("Alternate settings: %i\n",dev->num_alt);
2181 // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
2182 dev->alt_max_pkt_size = kmalloc(32*
2183 dev->num_alt,GFP_KERNEL);
2184 if (dev->alt_max_pkt_size == NULL) {
2185 em28xx_errdev("out of memory!\n");
2186 em28xx_devused&=~(1<<nr);
2187 kfree(dev);
2188 return -ENOMEM;
2191 for (i = 0; i < dev->num_alt ; i++) {
2192 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2193 wMaxPacketSize);
2194 dev->alt_max_pkt_size[i] =
2195 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
2196 em28xx_info("Alternate setting %i, max size= %i\n",i,
2197 dev->alt_max_pkt_size[i]);
2200 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
2201 dev->model = card[nr];
2203 /* allocate device struct */
2204 retval = em28xx_init_dev(&dev, udev, nr);
2205 if (retval)
2206 return retval;
2208 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
2210 /* save our data pointer in this interface device */
2211 usb_set_intfdata(interface, dev);
2213 request_modules(dev);
2215 return 0;
2219 * em28xx_usb_disconnect()
2220 * called when the device gets diconencted
2221 * video device will be unregistered on v4l2_close in case it is still open
2223 static void em28xx_usb_disconnect(struct usb_interface *interface)
2225 struct em28xx *dev;
2226 struct em28xx_ops *ops = NULL;
2228 dev = usb_get_intfdata(interface);
2229 usb_set_intfdata(interface, NULL);
2231 if (!dev)
2232 return;
2234 em28xx_info("disconnecting %s\n", dev->vdev->name);
2236 /* wait until all current v4l2 io is finished then deallocate resources */
2237 mutex_lock(&dev->lock);
2239 wake_up_interruptible_all(&dev->open);
2241 if (dev->users) {
2242 em28xx_warn
2243 ("device /dev/video%d is open! Deregistration and memory "
2244 "deallocation are deferred on close.\n",
2245 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2247 dev->state |= DEV_MISCONFIGURED;
2248 em28xx_uninit_isoc(dev);
2249 dev->state |= DEV_DISCONNECTED;
2250 wake_up_interruptible(&dev->wait_frame);
2251 wake_up_interruptible(&dev->wait_stream);
2252 } else {
2253 dev->state |= DEV_DISCONNECTED;
2254 em28xx_release_resources(dev);
2256 mutex_unlock(&dev->lock);
2258 mutex_lock(&em28xx_extension_devlist_lock);
2259 if (!list_empty(&em28xx_extension_devlist)) {
2260 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2261 ops->fini(dev);
2264 mutex_unlock(&em28xx_extension_devlist_lock);
2266 if (!dev->users) {
2267 kfree(dev->alt_max_pkt_size);
2268 kfree(dev);
2272 static struct usb_driver em28xx_usb_driver = {
2273 .name = "em28xx",
2274 .probe = em28xx_usb_probe,
2275 .disconnect = em28xx_usb_disconnect,
2276 .id_table = em28xx_id_table,
2279 static int __init em28xx_module_init(void)
2281 int result;
2283 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
2284 (EM28XX_VERSION_CODE >> 16) & 0xff,
2285 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2286 #ifdef SNAPSHOT
2287 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2288 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2289 #endif
2291 /* register this driver with the USB subsystem */
2292 result = usb_register(&em28xx_usb_driver);
2293 if (result)
2294 em28xx_err(DRIVER_NAME
2295 " usb_register failed. Error number %d.\n", result);
2297 return result;
2300 static void __exit em28xx_module_exit(void)
2302 /* deregister this driver with the USB subsystem */
2303 usb_deregister(&em28xx_usb_driver);
2306 module_init(em28xx_module_init);
2307 module_exit(em28xx_module_exit);